How to tricky rank SAS?

微笑、不失礼 提交于 2021-01-29 15:09:02

问题


SO For example I have data like this,

KEY  first count

11Y    1
11Y    2
11N    3
11N    4
11Y    5
11N    6

I want out put like this

    KEY  first count  RANKS

    11Y    1          1
    11Y    2          1
    11N    3          2
    11N    4          2
    11Y    5          3
    11N    6          4

how can I do in SAS?

Thanks

I did this

proc sort data=step3;
by first_count key;
run;

data step4;
set step3;
by key;
if first.key THEN ranks=1;
else ranks+1;
run;

This causing error

ERROR: BY variables are not properly sorted on data set WORK.STEP3


回答1:


You need NOTSORTED.

data key;
   input KEY:$3. count;
   cards;
11Y    1
11Y    2
11N    3
11N    4
11Y    5
11N    6
;
run;
data key2;
   set key;
   by key NOTSORTED;
   if first.key then rank+1;
   run;
proc print;
   run;




回答2:


Based on DN's anwser, You can do it without first.var statement.

data key;
   input KEY:$3. count;
   cards;
11Y    1
11Y    2
11N    3
11N    4
11Y    5
11N    6
;
run;
data key2;
   set key;
   rank + (key ^= lag(key));
   run;
proc print;
   run; 


来源:https://stackoverflow.com/questions/60011740/how-to-tricky-rank-sas

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!