DATA Step, Macro, Functions and more

Help in Rank function

Accepted Solution Solved
Reply
Contributor
Posts: 53
Accepted Solution

Help in Rank function

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


Accepted Solutions
Solution
3 weeks ago
Super User
Posts: 17,750

Re: Help in Rank function


All Replies
Super User
Posts: 10,466

Re: Help in Rank function

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.

Contributor
Posts: 53

Re: Help in Rank function

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)

Solution
3 weeks ago
Super User
Posts: 17,750

Re: Help in Rank function

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.

Contributor
Posts: 53

Re: Help in Rank function

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

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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