I would like to inspect 5 largest observations in each of 10 groups in my dataset. To do so, I made the below code, but I don't think it's very efficient, because I made a smaller subset that includes 5 observations in 10 groups. Would there be a better way to do so? I actually would like to use PROC REPORT, if I can.
proc sort data= input;
by group descending SIZE; run;
data input; set input;
by group;
retain no;
if first.group then no= 1;
else no= no+1; run;
data output; set input;
where no <= 5;
run;
proc print data= output;
by group;
var no group SIZE vars; run;
You could take a look at PROC RANK and then filter based on the rank
https://support.sas.com/documentation/cdl/en/proc/61895/HTML/default/viewer.htm#a002473611.htm
Hi @braam ,
You don't need 2 data steps after sorting.
data input;
do group = "A", "B", "C";
do SIZE = 1 to 10;
vars = "xyz";
output;
end;
end;
run;
proc sort data = input;
by group descending SIZE;
run;
data output;
set input;
by group;
if first.group then no = 0;
no + 1;
if no <= 5;
run;
proc print data = output;
by group;
var no group SIZE vars;
run;
All the best
Bart
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.