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
Onyx | Level 15

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



sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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