Barite | Level 11

## Using PROC RANK for Groups of the Same Variable

Today's hurdle:

I have 2 variables that are numeric.  The first variable can have duplicates, the second will not.  I am trying to use the PROC RANK function to assign a rank based on variable.  What I have found so far is how to rank all the observations.  This is the reference I have been working with: HERE.

Here is my example and the desired outcome for "New Rank" where the "New Rank" is in descending order:

 Variable 1 Variable 2 New Rank 10 100 1 20 100 2 20 200 1 30 100 1

I feel like this should be another simple one but I can't get my head wrapped around it.

1 ACCEPTED SOLUTION

Accepted Solutions
Super User

## Re: Using PROC RANK for Groups of the Same Variable

Reverse the sort, numerate and then resort. Simplest to code option.

``````proc sort data=have;
by var1 descending var2;
run;

data want;
set have;
by var1 descending var2;
if first.var1 then count=1;
else count+1;
run;

proc sort data=want;
by var2 var2;
run;``````

@elwayfan446 wrote:

Today's hurdle:

I have 2 variables that are numeric.  The first variable can have duplicates, the second will not.  I am trying to use the PROC RANK function to assign a rank based on variable.  What I have found so far is how to rank all the observations.  This is the reference I have been working with: HERE.

Here is my example and the desired outcome for "New Rank" where the "New Rank" is in descending order:

 Variable 1 Variable 2 New Rank 10 100 1 20 100 2 20 200 1 30 100 1

I feel like this should be another simple one but I can't get my head wrapped around it.

7 REPLIES 7
Amethyst | Level 16

## Re: Using PROC RANK for Groups of the Same Variable

please try the below code without the proc rank

``````data have;
input Variable1	Variable2	;
cards;
10 100
20 100
20 200
30 100
;

proc sort data=have;
by Variable1 Variable2;
run;

data want;
set have;
by Variable2 notsorted;
retain rank;
if first.Variable2 then rank=1;
else rank+1;
run;``````
Thanks,
Jag
Barite | Level 11

## Re: Using PROC RANK for Groups of the Same Variable

Here is my actual code:

``````data dwacq.data_sale;
set dwacq.data_sale;
by sale_loan_number_num notsorted;
retain rank;
if first.sale_loan_number then rank=1;
else rank+1;
run;``````

Here are the results I want:

 sale_loan_key sale_loan_number New Rank 10 100 1 20 100 2 20 200 1 30 100 1

Here are the results I am getting with your code (and all the code I was trying before I posted the question):

 sale_loan_key sale_loan_number New Rank 10 100 1 20 100 2 20 200 3 30 100 4

Tourmaline | Level 20

## Re: Using PROC RANK for Groups of the Same Variable

``````data have;
input Variable1	Variable2;*	NewRank;
cards;
10	100	1
20	100	2
20	200	1
30	100	1
;
proc rank data=have out=want descending ties=dense;
by variable1;
var variable2;
ranks newrank;
run;``````
Barite | Level 11

## Re: Using PROC RANK for Groups of the Same Variable

New Rank is not currently a variable in the dataset.  This is the new variable I want to create with the new rank based on var 1 and var2.

Tourmaline | Level 20

## Re: Using PROC RANK for Groups of the Same Variable

Yes you are right, albeit you perhaps missed to notice the newrank being commented in the input statement ?

``input Variable1	Variable2;*	NewRank;``

Super User

## Re: Using PROC RANK for Groups of the Same Variable

Reverse the sort, numerate and then resort. Simplest to code option.

``````proc sort data=have;
by var1 descending var2;
run;

data want;
set have;
by var1 descending var2;
if first.var1 then count=1;
else count+1;
run;

proc sort data=want;
by var2 var2;
run;``````

@elwayfan446 wrote:

Today's hurdle:

I have 2 variables that are numeric.  The first variable can have duplicates, the second will not.  I am trying to use the PROC RANK function to assign a rank based on variable.  What I have found so far is how to rank all the observations.  This is the reference I have been working with: HERE.

Here is my example and the desired outcome for "New Rank" where the "New Rank" is in descending order:

 Variable 1 Variable 2 New Rank 10 100 1 20 100 2 20 200 1 30 100 1

I feel like this should be another simple one but I can't get my head wrapped around it.

Barite | Level 11

## Re: Using PROC RANK for Groups of the Same Variable

This worked well and I always like as little code as possible! Thanks @Reeza and thanks @novinosrin and @Jagadishkatam for your help as well!

Discussion stats
• 7 replies
• 1150 views
• 0 likes
• 4 in conversation