Help using Base SAS procedures

Conditional Ranking

Accepted Solution Solved
Reply
Contributor
Posts: 47
Accepted Solution

Conditional Ranking

[ Edited ]

So using this dataset:

data swim;
   input Name $ 1-7 Gender $ 9 Back 11-14 Free 16-19 Count 21-25; 
   datalines;
Andrea  F 28.6 30.3 5
Carole  F 32.9 24.0 3
Clayton M 27.0 21.9 7
Curtis  M 29.0 22.6 1
Doug    M 27.3 22.4 12
Ellen   F 27.8 27.0 4
Jan     F 31.3 31.2 3
Jimmy   M 26.3 22.5 6
Karin   F 34.6 26.2 7
Mick    M 29.0 25.4 1
Richard M 29.7 30.2 2
Sam     M 27.2 24.1 8
Susan   F 35.1 36.1 10
;
proc rank data=swim_sort groups=3 out=ranked;
	var back;
	ranks rank;
	run;

I want to rank swimmers based on their backstroke time. But I also want to put them in class 3 if their count is less than 3, if that makes sense.

So instead of this: 

rank_snip.JPGWe get this:rank_snip2.JPGSo the last 3 swimmers in class 1 are now in class 2 because their count was less than 3.

 

Is there a way to do that with proc rank, or do I need something else?

 

Thanks.


Accepted Solutions
Solution
‎03-09-2018 12:40 AM
Esteemed Advisor
Posts: 5,523

Re: Conditional Ranking

Include your condition into your ranking criteria:

 

data swim;
   input Name $ 1-7 Gender $ 9 Back 11-14 Free 16-19 Count 21-25; 
   datalines;
Andrea  F 28.6 30.3 5
Carole  F 32.9 24.0 3
Clayton M 27.0 21.9 7
Curtis  M 29.0 22.6 1
Doug    M 27.3 22.4 12
Ellen   F 27.8 27.0 4
Jan     F 31.3 31.2 3
Jimmy   M 26.3 22.5 6
Karin   F 34.6 26.2 7
Mick    M 29.0 25.4 1
Richard M 29.7 30.2 2
Sam     M 27.2 24.1 8
Susan   F 35.1 36.1 10
;

data altSwim;
set swim;
altBack = back + 100*(count<3);
run;

proc sort data=altSwim; by altBack; run;

proc rank data=altSwim groups=3 out=ranked(drop=altBack);
	var altBack;
	ranks rank;
	run;

proc print data=ranked; run;
PG

View solution in original post


All Replies
Solution
‎03-09-2018 12:40 AM
Esteemed Advisor
Posts: 5,523

Re: Conditional Ranking

Include your condition into your ranking criteria:

 

data swim;
   input Name $ 1-7 Gender $ 9 Back 11-14 Free 16-19 Count 21-25; 
   datalines;
Andrea  F 28.6 30.3 5
Carole  F 32.9 24.0 3
Clayton M 27.0 21.9 7
Curtis  M 29.0 22.6 1
Doug    M 27.3 22.4 12
Ellen   F 27.8 27.0 4
Jan     F 31.3 31.2 3
Jimmy   M 26.3 22.5 6
Karin   F 34.6 26.2 7
Mick    M 29.0 25.4 1
Richard M 29.7 30.2 2
Sam     M 27.2 24.1 8
Susan   F 35.1 36.1 10
;

data altSwim;
set swim;
altBack = back + 100*(count<3);
run;

proc sort data=altSwim; by altBack; run;

proc rank data=altSwim groups=3 out=ranked(drop=altBack);
	var altBack;
	ranks rank;
	run;

proc print data=ranked; run;
PG
Contributor
Posts: 47

Re: Conditional Ranking

[ Edited ]

This looks perfect, I just want to double check that I understand what's going on.

So it's taking the original dataset, and if the count is less than 3, adding 100 to back in order to make it much larger, thus forcing it into rank 2.

Now if I wanted to make it do the opposite, and force them into rank 0, I could do something like altBack = back + -100*(count<3); ?

Esteemed Advisor
Posts: 5,523

Re: Conditional Ranking

Right.

 

Putting them in the middle group would be more difficult! Smiley Happy

PG
☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 184 views
  • 2 likes
  • 2 in conversation