Friday, May 13, 2016

Hack 3.7 Creating More Readable PROC FREQ’s

SAS Programming Professionals,

Did you know that you can create less busy looking frequencies by specifying a few options in PROC FREQ?

The NOROW, NOCOL, and NOPERCENT options on the TABLES statement remove row percents, column percents, and row percentages, respectively.  They can be useful when such information is not really necessary for a given report.  Let’s look at an example of how each of these options work.

The following program:

proc freq data=sashelp.class;
    tables sex * age;
run;

…produces this output:

Sex       Age

Frequency|
Percent  |
Row Pct  |
Col Pct  |      11|      12|      13|      14|      15|      16|  Total
---------+--------+--------+--------+--------+--------+--------+
F        |      1 |      2 |      2 |      2 |      2 |      0 |      9
|   5.26 |  10.53 |  10.53 |  10.53 |  10.53 |   0.00 |  47.37 |    
11.11 |  22.22 |  22.22 |  22.22 |  22.22 |   0.00 |        |    
50.00 |  40.00 |  66.67 |  50.00 |  50.00 |   0.00 |        |    
---------+--------+--------+--------+--------+--------+--------+
M        |      1 |      3 |      1 |      2 |      2 |      1 |     10
|   5.26 |  15.79 |   5.26 |  10.53 |  10.53 |   5.26 |  52.63 |    
|  10.00 |  30.00 |  10.00 |  20.00 |  20.00 |  10.00 |        |    
50.00 |  60.00 |  33.33 |  50.00 |  50.00 | 100.00 |        |    
---------+--------+--------+--------+--------+--------+--------+
Total           2        5        3        4        4        1       19
     10.53    26.32    15.79    21.05    21.05     5.26   100.00

If we add the NOROW option:

proc freq data=sashelp.class;
    tables sex * age / norow;
run;

…we get the following:
Sex       Age

Frequency|
Percent  |
Col Pct  |      11|      12|      13|      14|      15|      16|  Total
---------+--------+--------+--------+--------+--------+--------+
F        |      1 |      2 |      2 |      2 |      2 |      0 |      9
|   5.26 |  10.53 |  10.53 |  10.53 |  10.53 |   0.00 |  47.37 |    
|  50.00 |  40.00 |  66.67 |  50.00 |  50.00 |   0.00 |        |    
---------+--------+--------+--------+--------+--------+--------+
M        |      1 |      3 |      1 |      2 |      2 |      1 |     10
|   5.26 |  15.79 |   5.26 |  10.53 |  10.53 |   5.26 |  52.63 |    
|  50.00 |  60.00 |  33.33 |  50.00 |  50.00 | 100.00 |        |    
---------+--------+--------+--------+--------+--------+--------+
Total           2        5        3        4        4        1       19
     10.53    26.32    15.79    21.05    21.05     5.26   100.00

If we add the NOCOL option:

proc freq data=sashelp.class;
    tables sex * age / norow nocol;
run;

…we get this:

Sex       Age

Frequency|
Percent  |      11|      12|      13|      14|      15|      16|  Total
---------+--------+--------+--------+--------+--------+--------+
F        |      1 |      2 |      2 |      2 |      2 |      0 |      9
|   5.26 |  10.53 |  10.53 |  10.53 |  10.53 |   0.00 |  47.37 |    
---------+--------+--------+--------+--------+--------+--------+
M        |      1 |      3 |      1 |      2 |      2 |      1 |     10
|   5.26 |  15.79 |   5.26 |  10.53 |  10.53 |   5.26 |  52.63 |    
---------+--------+--------+--------+--------+--------+--------+
Total           2        5        3        4        4        1       19
     10.53    26.32    15.79    21.05    21.05     5.26   100.00

If we add the NOPERCENT option:

proc freq data=sashelp.class;
    tables sex * age / norow nocol nopercent;
run;

…we produce the following:

Sex       Age

Frequency|      11|      12|      13|      14|      15|      16|  Total
---------+--------+--------+--------+--------+--------+--------+
F        |      1 |      2 |      2 |      2 |      2 |      0 |      9
---------+--------+--------+--------+--------+--------+--------+
M        |      1 |      3 |      1 |      2 |      2 |      1 |     10
---------+--------+--------+--------+--------+--------+--------+
Total           2        5        3        4        4        1       19

The NOROW, NOCOL, and NOPERCENT options on the TABLES statement can be used to create leaner, cleaner reports from the FREQUENCY procedure when the various percentages do not add to the value of the information you are presenting to your client.  And, we do want to keep our clients happy, don’t we!

Best of luck in all your SAS endeavors!

----MMMMIIIIKKKKEEEE
(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.