BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
pywils
Fluorite | Level 6

Hello,

 

I am interested in writing code that calculates S from the Wilcoxon Signed Rank test (see documentation here). I have been written code that correctly calculates S when n > 20, however, I have not been successful when n is less than 20.

 

At the bottom of this post is an example. You will notice that the proc univariate procedure returns S = 7.5, but when I write code that attempts to perform what the Wilcoxon Signed Rank test documentation says it is doing, I get a different value for S.   

 

Any insight on what I am doing wrong, or how I should calculate S for small n?  

 

Thank you,

Peter

 

/*made up data (the data is scorelines from Celtic Football Club in Scotland, but that doesn't matter)*/
data test;
input v1
	  v2;
datalines;
5 1
1 1
6 0
1 0
1 2
3 0 
5 0 
2 1
3 2
1 0
3 0
1 0 
;

/*Run SAS' Wilcoxon signed rank test*/
proc univariate data = test;
	var v1 v2;
run; 
/*note that here S = 7.5*/

/*derive x_[i} (called diffVar) */
data test2;
	set test;
	diffVar = v1 - v2;
	diffVarAbs = abs(diffVar);  
run; 

/* discard observations that have x_{i} = \mu_{0} */
data test3; 
	set test2;
	WHERE diffVar ne 0 ; 
run; 

/*derive r_{i}*/
proc rank data = test3 out = test4;
	var diffVarAbs;
	ranks Finish;
run; 

/* filtering observations such that x_{i} > \mu_{0}*/
data test5;
	set test4; 
	WHERE diffVar > 0;
run; 

proc sql noprint; 
	select count(v1) into: constant2 TRIMMED
	from test3;
run; 

*constant3 is the sum of r_{i}s*/;
proc sql noprint;
	select sum(Finish) into: constant3 TRIMMED
	from test5;
quit; 

/*calculate S*/
proc sql; 
	select &constant3 - (&constant2 * (&constant2 + 1)) / 4 into: constant4 TRIMMED
	FROM test5  ;
quit; 
/*note that now S = 29.5*/

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

If you want to reference a document then write code it is a good idea to use variable names that at least come close to the ones the document uses so we can follow what you may be attempting.

 

First thing you may want to consider is this is incorrect:

/*Run SAS' Wilcoxon signed rank test*/
proc univariate data = test;
	var v1 v2;
run; 
/*note that here S = 7.5*/

Reread your output. S=7.5 for V2. For V1 S=39.

 

If you want to run a test on the Signed Rank of difference between V1 and V2 you would use Proc univariate on your set TEST2 and use that for your comparison for the result, not for the single variables. If you run

proc univariate data = test2;
	var diffvar;
run; 

The Signed rank test result is 29.5.

 

Convoluted code but matched when compared to the correct value.

View solution in original post

1 REPLY 1
ballardw
Super User

If you want to reference a document then write code it is a good idea to use variable names that at least come close to the ones the document uses so we can follow what you may be attempting.

 

First thing you may want to consider is this is incorrect:

/*Run SAS' Wilcoxon signed rank test*/
proc univariate data = test;
	var v1 v2;
run; 
/*note that here S = 7.5*/

Reread your output. S=7.5 for V2. For V1 S=39.

 

If you want to run a test on the Signed Rank of difference between V1 and V2 you would use Proc univariate on your set TEST2 and use that for your comparison for the result, not for the single variables. If you run

proc univariate data = test2;
	var diffvar;
run; 

The Signed rank test result is 29.5.

 

Convoluted code but matched when compared to the correct value.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 1668 views
  • 1 like
  • 2 in conversation