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