BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SIgnificatif
Quartz | Level 8

Hi all, 
This question that is related to the SAS learning ( Programming 1 > Lesson 5 > Creating Frequency Reports > Level 2 Practice: Creating Two-Way Frequency Reports :

title1 'Selected Park Types by Region';
ods graphics on;
proc freq data=pg1.np_codelookup order=freq;
    tables Type*Region /  nocol crosslist 
           plots=freqplot(groupby=row scale=grouppercent orient=horizontal);
    where Type in ('National Historic Site', 'National Monument', 'National Park');
run;
title;

How to select the top 3 from Type instead of writing this ?

where Type in ('National Historic Site', 'National Monument', 'National Park');

 

 

Thank you for the response./ Update : Merry Xmas, Problem solved by the great community, thanks to everyone!

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

I think you want a two-way frequency containing all the cells for the 3 most populous rows (variable TYPE). 

 

proc freq data=np_codelookup order=freq  noprint;
  tables type*region /out=TR_freqs;
run;

data freqsubset;
  set tr_freqs;
if type^=lag(type) then ntypes+1; if ntypes>3 then stop; run; ods graphics on; proc freq data=freqsubset order=freq; tables Type*Region / nocol crosslist plots=freqplot(groupby=row scale=grouppercent orient=horizontal); weight count; run; ods graphics off;

 

Apparently the "order=freq" options orders results by descending row frequency (luckily not by descending cell freq).  So data set TR_FREQS is ordered as wanted, with one observation per cell and a new variable COUNT.  Then data set freqsubset is created just to include the first 3 types.  Using this data set in the 2nd proc freq with "weight count" produces what I think you want.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

2 REPLIES 2
Rick_SAS
SAS Super FREQ

In a two-way table, it is not entirely clear what you mean by "top 3." Do you mean that among all the cells, you want to print the levels of the cells that have the largest counts? If so, sort the CROSSLIST table in descending order and print or subset the first 3 observations:

 

proc freq data=sashelp.cars order=freq noprint;
    tables Type*Origin /  nocol crosslist out=FreqOut;
run;

proc sort data=FreqOut;
by descending Count;
run;

proc print data=FreqOut(obs=3);
run;
mkeintz
PROC Star

I think you want a two-way frequency containing all the cells for the 3 most populous rows (variable TYPE). 

 

proc freq data=np_codelookup order=freq  noprint;
  tables type*region /out=TR_freqs;
run;

data freqsubset;
  set tr_freqs;
if type^=lag(type) then ntypes+1; if ntypes>3 then stop; run; ods graphics on; proc freq data=freqsubset order=freq; tables Type*Region / nocol crosslist plots=freqplot(groupby=row scale=grouppercent orient=horizontal); weight count; run; ods graphics off;

 

Apparently the "order=freq" options orders results by descending row frequency (luckily not by descending cell freq).  So data set TR_FREQS is ordered as wanted, with one observation per cell and a new variable COUNT.  Then data set freqsubset is created just to include the first 3 types.  Using this data set in the 2nd proc freq with "weight count" produces what I think you want.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 2 replies
  • 2476 views
  • 2 likes
  • 3 in conversation