BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
BrahmanandaRao
Lapis Lazuli | Level 10
proc sql outobs=5;
select  age from sashelp.class;
group by age;
order by age desc;
quit;

Top N Values  how to get

1 ACCEPTED SOLUTION

Accepted Solutions
gamotte
Rhodochrosite | Level 12

Hello,

 

Your SQL code is only one instruction so you only need a semi-colon at the end.

You also need to add the DISTINCT keyword.

proc sql outobs=5;
select distinct age from sashelp.class
order by age desc;
quit;

EDIT: removed group by.

View solution in original post

6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20

Sort by descending and take the first N values of that.

BrahmanandaRao
Lapis Lazuli | Level 10
i am not getting ouput
PeterClemmensen
Tourmaline | Level 20
proc sort data = sashelp.class out = temp;
   by descending age;
run;

data want;
   set temp (keep = age obs = 5);
run;
andreas_lds
Jade | Level 19

@BrahmanandaRao wrote:
i am not getting ouput

Please post the code you use and the complete log. Both as text!

gamotte
Rhodochrosite | Level 12

Hello,

 

Your SQL code is only one instruction so you only need a semi-colon at the end.

You also need to add the DISTINCT keyword.

proc sql outobs=5;
select distinct age from sashelp.class
order by age desc;
quit;

EDIT: removed group by.

Rick_SAS
SAS Super FREQ

In addition the solutions given, you might also want to learn how to create a table and bar chart of the Top N values by using PROC FREQ:  How to create a "Top 10" table and bar chart

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 6 replies
  • 2789 views
  • 0 likes
  • 5 in conversation