BookmarkSubscribeRSS Feed
fengyuwuzu
Pyrite | Level 9

 

I want to select the top 10 obs with largest "amount" variable. Using the code belwo, sorting is done, but I still got the whole set, not the first 10 obs. What could be the problem that "obs=10" did not work?

 


proc sort data=have out=top_10 (obs=10);
by descending amount;
run;
5 REPLIES 5
ballardw
Super User

From the online documentation onthe OBS=Data set option:

Restriction: Use with input data sets only

 

Sort you data and then use

Data top_10;

   set top_10 (obs=10);

run;

SukritMehta
Calcite | Level 5

 

 

/* You can refer to this sample code and modify according to your needs..! */

 

/* To find top 10 actual sales for each country from the SAShelp.prdsal2 dataset. */

 

proc sort data=sashelp.prdsal2 out = class;
by country descending actual;
run;

 

data test;
set class;
by country;
if first.country = 1 then cnt = 0;
cnt + 1;
if cnt <= 10;
if last.country = 1 then cnt = 0;
run;

orb204
Calcite | Level 5

This is very close to what I am looking for but what if I want the top 10 by Jurisdiction and also by Date.

This is my sample code for top 10 by Jurisdiction

proc sort data=ATM_TOP_VALUE out = ATM_TOP_VALUE1;
by Jurisdiction descending amount;
run;


data ATM_TOP_VALUE2;
set ATM_TOP_VALUE1;
by Jurisdiction;
if first.Jurisdiction = 1 then cnt = 0;
cnt + 1;
if cnt <= 10;
if last.Jurisdiction = 1 then cnt = 0;
run;

However there is a field called month of and I want the top 10 for each jurisdiction for each month. How would I have to change the above code to get that?

FreelanceReinh
Jade | Level 19

Hello @orb204 and welcome to the SAS Support Communities!

 

Just insert variable month as the second BY variable and use first.month instead of first.Jurisdiction to indicate where the counter is to be reset to zero:

 

proc sort data=atm_top_value out=atm_top_value1;
by jurisdiction month descending amount;
run;

data atm_top_value2;
set atm_top_value1;
by jurisdiction month;
if first.month then cnt = 0;
cnt + 1;
if cnt <= 10;
run;

(Note that I also simplified the code a bit.)

 

 

General recommendation: For quicker answers please open a new thread when you have a question. Only a few people will notice that an old thread (like this from October 2018 or actually even April 2016) has been continued. If you want to refer to an old thread, you can provide a hyperlink like this: https://communities.sas.com/t5/SAS-Programming/proc-sort-and-select-10-largest-values/m-p/500910.

ChrisNZ
Tourmaline | Level 20

Or use proc sql to sort, together with its outobs= option

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 5 replies
  • 16052 views
  • 1 like
  • 6 in conversation