BookmarkSubscribeRSS Feed
pappusrini
Calcite | Level 5
How to select Top 10 records based on the variable in Proc Freq? Thanks in advance

I did the order = freq however I’m trying to compare the metrics by Week...when I do the order = freq it gives me in descending but not the weeks in order (week1, week2, week3) etc. also how do I choose the top 10 counts?

Please advise. Thank you
4 REPLIES 4
ballardw
Super User

What do you mean by "choose"? Are you attempting to create a subset of the data?

 

How do we know what a "week" might be? Do you have a variable named week? Dates that have to be assigned to a week?

 

You should provide a small example of data as you have now and then show what the desired result would be.

 

You might try 1) sort your data by week and 2) run your proc freq with a BY week. That should get the order within each week value, if that is what you have.

 

Your request is not quite clear because you could be wanting just the counts or sub-setting the data to get the records that provided a count.

 

You could demonstrate the logic with fewer records if using "top 3" instead of "top 10".

Astounding
PROC Star

Here's a possibility based on a few reasonable guesses as to what the results should be:

 

proc freq data=have;

tables week * metric / noprint out=counts;

run;

 

proc sort data=counts;

by week descending count;

run;

 

data want;

set counts;

by week;

if first.week then rank = 1;

else rank + 1;

if rank <= 10;

keep week metric count;

run;

Ksharp
Super User

@Rick_SAS just wrote a blog about it . But you need the newest SAS version.

proc freq data=have order=freq;

table x/ maxlevels=10;

run;

Rick_SAS
SAS Super FREQ

The article "An easy way to make a "Top 10" table and bar chart in SAS" shows how. 

 

@Ksharp is mistaken about needing the latest version of SAS. This option is available in any version of SAS 9.4, and SAS 9.4M0 shipped way back in in 2013.

 

If you aren't using SAS 9.4, there is a link in the blog post to an older technique.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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