问题
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