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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: