Warm tip: This article is reproduced from stackoverflow.com, please click
sas

How to tricky rank SAS?

发布于 2020-04-08 23:37:51

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

Questioner
supercool djkazu
Viewed
36
data _null_ 2020-02-01 06:10

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;

enter image description here