BookmarkSubscribeRSS Feed
byeh2017
Quartz | Level 8

Hi everyone,

 

I have a massive output of a proc freq of a variable in which I want to only limit to the top 5. How do I set up the code? I used this and it seemed to not work:

 

proc freq data=janfeb.jfgmi order=freq rowlimit=5;
table finalicd10;
run;
10 REPLIES 10
Astounding
PROC Star

SAS has to count every observation, to know what belongs in the highest 5.  So you might as well count them all:

 

proc freq data=jabfeb order=freq;

tables finalicd10 / noprint out=counts;

run;

 

By creating a SAS data set instead of printing the table, you now have choices.  The simplest:

 

data want;

set counts (obs=5);

run;

 

However, if the 5th highest count also appears as the 6th or 7th highest count, you lose the ties.  You could instead  continue with:

 

data want;

set counts;

by descending count;

if first.count and _n_ > 5 then stop;

run;

Rick_SAS
SAS Super FREQ

You can also use the method that Astounding mentions to graph the top categories. See "Create a bar chart with only a few categories."

Ksharp
Super User
Use SQL instead .


proc sql outobs=5;
 select air,count(*) as count
  from sashelp.air
   group by air
    order by count desc;
quit;



byeh2017
Quartz | Level 8

This works! Thanks. How do I title a proc sql statement?

Ksharp
Super User

You mean add a title or lable ?

 

title 'xxxxxxx';

proc sql outobs=5;
 select air,count(*) as count label='xxxxx'
  from sashelp.air
   group by air
    order by count desc;
quit;
byeh2017
Quartz | Level 8
That works, thanks!

##- Please type your reply above this line. Simple formatting, no
attachments. -##
spcoman
Fluorite | Level 6

Thanks so much for this code it is very helpful! How would you go about formatting a table like this. I tried using PROC TABULATE to rename the row headers etc. but I don't know how to point the PROC TAB at the new table.

Ksharp
Super User

Once you assign a label to a variable, proc tabulate would print the label.

like:

 

options label;

proc tabulate .......

 

 

And can start a brand new session to let other know your Q,and maybe others have a better idea.

dhwanipatel109
Calcite | Level 5
You can try:

proc freq data=janfeb.jfgmi ORDER=FREQ;
tables make / maxlevels=5 Plots=FreqPlot;
run;
wlauzon
Calcite | Level 5

I just stumbled upon this answer and it is by far the simplest solution, thank you so much! 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 10 replies
  • 16172 views
  • 4 likes
  • 7 in conversation