lord12
lord12

Reputation: 2927

Is there a way to put indices in a proc sort?

Suppose I sort a dataset as follows:

proc sort data = temp;
by type descending score;
run;

Is there an elegant way I can add a counter for each score within each type? I know I can use first and last statements but is there a way within the sort I add a counter?

TYPE SCORE   index
A   20        1      
A   10        2
A   5         3
B   90        1
B   80        2
B   70        3

Upvotes: 0

Views: 87

Answers (1)

Joe
Joe

Reputation: 63434

No, there is not such a way within a Proc Sort. You need to either use a datastep, or a datastep view, to do such.

proc sort data = temp;
by type descending score;
run;

data temp_v / view=temp_v;
set temp;
by type descending score;
if first.type then counter=0;
if first.score then counter+1;
run;

That effectively does what you ask for - it adds a counter without adding an additional pass through the data.

Upvotes: 1

Related Questions