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;
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;
/* 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;
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?
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.
Or use proc sql to sort, together with its outobs= option
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 25. 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.