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!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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