SAS Examples: Proc Freq

Last updated:
Table of Contents

WIP Alert This is a work in progress. Current information is correct but more content may be added in the future.

Simplest possible proc freq

Outputs the frequency for each value in column <column> on table <library>.<source_table>.

proc freq data=<library>.<source_table>; 
    tables <column>;
run;

Save output to another table

while also saving the results to <library>.<output_table>

proc freq data=<library>.<source_table>; 
    tables <column> / out=<library>.<output_table>;
run;

Show missing data separately

Output a frequency table and a histogram for column <column> on table <library>.<source_table>, including missing data as a separate category.

proc freq data=<library>.<source_table>; 
    tables <column> / missing;
run;

Proc freq with histogram

Output a frequency table and a histogram for column <column> on table <library>.<source_table>.

ods graphics on;
proc freq data=<library>.<source_table>; 
    tables <column> / plots=freqplot();
run;

Proc freq with formats to handle continuous data

It's very common to create a FORMAT to bin your data into separate groups, so that you can view continuous data as buckets.

Let <column_name> be a numeric column, such as salaries:

proc format;
    value <my_format>
        0 -< 10000 = "Up to US$ 10,000"
        10000 -< 20000 = "From US$ 10,000 to US$ 20,000"
        20000 -< 30000 = "From US$ 20,000 to US$ 30,000"
        30000 -< 40000 = "From US$ 30,000 to US$ 40,000"
        40000 -< 50000 = "From US$ 40,000 to US$ 50,000"
        50000 - high = "Above US$ 50,000"
        . = "No data"
        other = "???";
run;

ods graphics on;
proc freq data=<output_table>;
    tables <column_name>/ missing plots=freqplot;
    format <column_name> <my_format>.;
run;

Two-way proc freq

Let <namespace>.<tablename> be a table with two categorical columns and you want to see the distribution of one versus the other.

This will group the results by the FIRST element, namely <column1>. You can also use groupby=column to group the results by <column2>

ods graphics on;
proc freq data=<namespace>.<tablename>;
tables <column1>*<column2> / plots=freqplot(groupby=row twoway=cluster);
run;

References

Dialogue & Discussion