Solved
Contributor
Posts: 47

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

We get this:So 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
Posts: 5,523

## Re: Conditional Ranking

``````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

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

## Re: Conditional Ranking

``````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); ?

Posts: 5,523

## Re: Conditional Ranking

Right.

Putting them in the middle group would be more difficult!

PG
☑ This topic is solved.