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;
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;
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.
@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.
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;
You seem to want to order by TestVal within Var1 groups. What should be the role of Var2?
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.