BookmarkSubscribeRSS Feed
braam
Quartz | Level 8

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;

 

 

 

 

2 REPLIES 2
novinosrin
Tourmaline | Level 20

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

yabwon
Amethyst | Level 16

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1629 views
  • 2 likes
  • 3 in conversation