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! 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 10 replies
  • 16831 views
  • 4 likes
  • 7 in conversation