DATA Step, Macro, Functions and more

Rank using multiple by variables

Accepted Solution Solved
Reply
Contributor
Posts: 34
Accepted Solution

Rank using multiple by variables

[ Edited ]

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; 

 


Accepted Solutions
Solution
2 weeks ago
PROC Star
Posts: 1,800

Re: Rank using multiple by variables

Posted in reply to UdayGuntupalli
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


All Replies
Super User
Posts: 6,767

Re: Rank using multiple by variables

Posted in reply to UdayGuntupalli

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.

Contributor
Posts: 34

Re: Rank using multiple by variables

Posted in reply to Astounding

@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. 

Solution
2 weeks ago
PROC Star
Posts: 1,800

Re: Rank using multiple by variables

Posted in reply to UdayGuntupalli
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;
Esteemed Advisor
Posts: 5,526

Re: Rank using multiple by variables

Posted in reply to UdayGuntupalli

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

PG
☑ This topic is solved.

Need further help from the community? Please ask a new question.

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