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

Please help! I want to Rank the second variable in this code.

data have;
input id region;
datalines;
10 US
10 CA
10 CA
23 MA
25 MA
45 Ks
50 US
50 SA
65 US
;

proc sort data = have;
by id;
run;

data want;
set have;
by id;
if first.id then rank =1;
else rank+1;
run;

 

Output needed:

 

ID  Rank1 Rank2

10   1         1

10   2        1

10   3         2

1 ACCEPTED SOLUTION

Accepted Solutions
4 REPLIES 4
ballardw
Super User

What is your rule for ranking a character variable?

Since you have US within 3 of the ID groups it isn't clear what that rule may be.

Also you might want to work the result for all of your example data and not remove the Region variable.

Kalai2008
Pyrite | Level 9

Thank you, My rule is to Rank a new column when the variable is US, Another Column to Rank, for CA separately and one column with no rules.(which I already got it from the prev query)

Reeza
Super User

https://stats.idre.ucla.edu/sas/faq/how-can-i-create-an-enumeration-variable-by-groups/

 

Instructions above for the various situations you're working with.

Kalai2008
Pyrite | Level 9

Thank you Reeza!.. Great article! It helps!

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1295 views
  • 0 likes
  • 3 in conversation