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

All, 
     I am looking for some help in trying to rank a data set using multiple by-variables. I am attaching a sample script that I tried to put together, however, I am not sure where I am going wrong. I am trying to follow the example provided here (http://support.sas.com/documentation/cdl/en/proc/65145/HTML/default/viewer.htm#p12aek9f6xhl1zn1puuuo...) . Obviously, I am doing something wrong, however I am not familiar with Proc Rank or Proc Sort well enough to understand what I am doing wrong.  If Var1 is my first grouping variable, Var2 is my second grouping variable and TestVal is the value I want to compute ranks of my dataset, can some body please guide me and explain what I am doing wrong. 


 

data RankQuestion;
   input Var1 1-2 Var2 3-4 TestVal 5-9;
   datalines;
	1 1 0.42
	1 2 0.62
	1 3 0.22
	5 4 0.12
	5 6 0.11
	5 9 0.13
	8 7 0.99
	8 10 0.84
	8 11 0.85
	;
	
proc sort data = RankQuestion out = RawSort; 
	by Var1 Var2 TestVal; 
run; 

proc rank data = RawSort out = RankRes ties = low descending;
	by Var1 Var2; 
	var TestVal; 
	ranks newrank; 
run;

proc print data =  RankRes ; 
	by Var1 Var2 newrank; 
	title 'Testing Rank By Groups';
run; 

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
data RankQuestion;
   input Var1  Var2  TestVal ;
   datalines;
	1 1 0.42
	1 2 0.62
	1 3 0.22
	5 4 0.12
	5 6 0.11
	5 9 0.13
	8 7 0.99
	8 10 0.84
	8 11 0.85
	;
	
proc sort data = RankQuestion out = RawSort; 
	by Var1  testval; 
run; 
proc rank data = RawSort out = RankRes   ties=low; ;
	by Var1 ; 
	var   TestVal ; 
	ranks newrank1 ; 
run;

View solution in original post

4 REPLIES 4
Astounding
PROC Star

You'll have to show what you want the result to be.  It's too much of a leap of faith to show us code that doesn't work right, and hope we can figure out what "work right" means.

UdayGuntupalli
Quartz | Level 8

@Astounding,
                  Var1 Var2 TestVal Rank 

                     1      3       0.22     1
                     1      1       0.42     2

                     1      2       0.62     3 

                     5      6       0.11     1
                     5      4       0.12     2

                     5      9       0.13     3 

               

           Here is an example of the desired output. 

novinosrin
Tourmaline | Level 20
data RankQuestion;
   input Var1  Var2  TestVal ;
   datalines;
	1 1 0.42
	1 2 0.62
	1 3 0.22
	5 4 0.12
	5 6 0.11
	5 9 0.13
	8 7 0.99
	8 10 0.84
	8 11 0.85
	;
	
proc sort data = RankQuestion out = RawSort; 
	by Var1  testval; 
run; 
proc rank data = RawSort out = RankRes   ties=low; ;
	by Var1 ; 
	var   TestVal ; 
	ranks newrank1 ; 
run;
PGStats
Opal | Level 21

You seem to want to order by TestVal within Var1 groups. What should be the role of Var2?

PG

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 1398 views
  • 0 likes
  • 4 in conversation