Write and run SAS programs in your web browser

Simple proc freq of a variable limiting to top 5 on SAS studio

Reply
Frequent Contributor
Posts: 77

Simple proc freq of a variable limiting to top 5 on SAS studio

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;
Respected Advisor
Posts: 5,005

Re: Simple proc freq of a variable limiting to top 5 on SAS studio

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;

SAS Super FREQ
Posts: 3,420

Re: Simple proc freq of a variable limiting to top 5 on SAS studio

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

Grand Advisor
Posts: 9,596

Re: Simple proc freq of a variable limiting to top 5 on SAS studio

Use SQL instead .


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



Frequent Contributor
Posts: 77

Re: Simple proc freq of a variable limiting to top 5 on SAS studio

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

Grand Advisor
Posts: 9,596

Re: Simple proc freq of a variable limiting to top 5 on SAS studio

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;
Frequent Contributor
Posts: 77

Re: Simple proc freq of a variable limiting to top 5 on SAS studio

That works, thanks!

##- Please type your reply above this line. Simple formatting, no
attachments. -##
Ask a Question
Discussion stats
  • 6 replies
  • 131 views
  • 0 likes
  • 4 in conversation