BookmarkSubscribeRSS Feed
SASPhile
Quartz | Level 8
ttype has two values 'MTH' and 'YTD'.if we want to display ranks of districts on these two values (grouping) such that we see how districts are ranked in a month and how they are ranked in year?


The below code gives ranks from 1 to 66 .instead we need rank 1 to 33 in mth and 1 to 33 in year to be shown in a single dataset
proc sql;

create table rank_dist_before as

select product, metric,lbl,ttype,dist,sum(sls) as sls

from tmp1

group by dist,ttype

order by sls descending;

quit;

data rank_dist_nation;
set rank_dist_before;
Rank+1;
run;
5 REPLIES 5
Cynthia_sas
SAS Super FREQ
Hi:
If you are assigning the RANK variable in a program, this would be a good point to investigate BY group processing and the use of FIRST.byvar logic (also LAST.byvar).

cynthia

ps...there is also a PROC RANK procedure that you might be able to use here -- I don't use it much, but perhaps someone else can help you with it.
SASPhile
Quartz | Level 8
I tried Proc rank,but it gives me the same rank for all rows.
Doc_Duke
Rhodochrosite | Level 12
modify the DATA step to include a BY statement and explicitly reset the retain'd variable.

data rank_dist_nation;
set rank_dist_before;
BY sls;
IF first.sls THEN rank=0;
RETAIN rank;
Rank+1;
run;
SASPhile
Quartz | Level 8
1 is assigned to all rows.
deleted_user
Not applicable
try this...

proc sql;

create table rank_dist_before as

select product, metric,lbl,ttype,dist,sum(sls) as sls

from tmp1

group by dist,ttype

order by dist,ttype,sls descending;

quit;

data rank_dist_nation;
set rank_dist_before;
by dist ttype descending sls ;
if first.dist then rank = 0;
Rank+1;
retain rank;
run;

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