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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 1004 views
  • 0 likes
  • 4 in conversation