Friday, April 8, 2016

Hack 3.1 Controlling the Levels Displayed in One-Way PROC FREQ Tables

SAS Programming Professionals,

Did you know that you can control the number of levels displayed for one-way tables in PROC FREQ?

You can do this by using both the ORDER= option on the PROC FREQ statement and the MAXLEVELS= option on the TABLES statement.  Here is a simple example that you can copy and run:
/* Traditional PROC FREQ with all levels produced */
proc freq order=freq;
      tables make;
/* PROC FREQ with only top 5 levels produced */
proc freq order=freq;
      tables make / maxlevels=5;

In the second PROC FREQ, the ORDER=FREQ option specifies for SAS to list the output in descending order.  The MAXLEVELS=5 option specifies for SAS to only print the first five levels.

The second PROC FREQ produces the following report:
                                        The FREQ Procedure
                                                           Cumulative    Cumulative
                Make             Frequency     Percent     Frequency      Percent
                Toyota                 28        6.54            28         6.54
                Chevrolet              27        6.31            55        12.85
                Mercedes-Benz          26        6.07            81        18.93
                Ford                   23        5.37           104        24.30
                BMW                    20        4.67           124        28.97

                                The first 5 levels are displayed.

Notice the nice note stating that only the first five levels are displayed.  This can come in handy for times where you want an abbreviated report of only the top N frequencies.

Best of luck in all your SAS endeavors!

(aka Michael A. Raithel)

Excerpt from the book:  Did You Know That?  Essential Hacks for Clever SAS Programmers

I plan to post each and every one of the hacks in the book to social media on a weekly basis.  Please pass them along to colleagues who you know would benefit.