BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
elwayfan446
Barite | Level 11

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 1Variable 2New Rank
101001
201002
202001
301001

 

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

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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.




View solution in original post

7 REPLIES 7
Jagadishkatam
Amethyst | Level 16

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
elwayfan446
Barite | Level 11

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_keysale_loan_numberNew Rank
101001
201002
202001
301001

 

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

 

sale_loan_keysale_loan_numberNew Rank
101001
201002
202003
301004

 

novinosrin
Tourmaline | Level 20
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;
elwayfan446
Barite | Level 11

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.

novinosrin
Tourmaline | Level 20

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

input Variable1	Variable2;*	NewRank;

 

Reeza
Super User

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.




elwayfan446
Barite | Level 11

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

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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