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
Diamond | Level 26
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;
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
  • 5 replies
  • 2289 views
  • 0 likes
  • 4 in conversation