Proc Freq Explained with Examples

This tutorial explains how to use PROC FREQ with various examples. The PROC FREQ is one of the most frequently used SAS procedures which helps to summarize categorical variable. It calculates count/frequency and cumulative frequency of categories of a categorical variable. In other words, it returns the number and percentage of cases falling in multiple categories of a categorical variable. It's not just restricted to counts. It also produces bar charts and tests for association between two categorical variables.

Create a sample data set

The program below creates a sample SAS dataset which would be used in further examples to explain PROC FREQ.

Example 1 : To check the distribution of a categorical variable (Character)

Suppose you want to see the frequency distribution of variable 'y'.

proc freq data = example1;
tables y;
run;

The TABLES statements tells SAS to return n-way frequency and crosstabulation tables and computes the statistics for these tables.

Output : PROC FREQ

It answers a question 'which category holds the maximum number of cases'. In this case, the category 'C' contains maximum number of values.Tip :

Categorical variables are of two types - Nominal and Ordinal. A nominal variable is a categorical variable in which categories do not have any order. For example, gender, city etc. An ordinal categorical variable has categories that can be ordered in a meaningful way. For example, rank, status (high/medium/low) etc.

Example 2 : To remove unwanted statistics in the table

Suppose you do not want cumulative frequency and cumulative percent to be displayed in the table. The option NOCUM tells SAS to not to return cumulative scores.

proc freq data = example1;
tables y /nocum;
run;

NOCUM Option

If you want only frequency, not percent distribution and cumulative statistics.

proc freq data = example1;
tables y /nopercent nocum;
run;

NOPERCENT and NOCUM option

Example 3 : Cross Tabulation ( 2*2 Table)

Suppose you want to see the distribution of variable 'y' by variable 'x'.

proc freq data = example1;
tables y * x;
run;

Proc Freq Output

The output of the above SAS program is shown in the image above.

Example 4 : Show Table in List Form

Suppose you do not want output to be shown in tabular form. Instead, you want final analysis to be displayed in list form (See the image below)

PROC FREQ List Form

proc freq data = example1;
tables y * x / list;
run;

The forward slash followed by LIST keyword produces the list styled table.

Suppose you wish to save the result in a SAS dataset instead of printing it in result window.

proc freq data = example1 noprint;
tables y *x / out = temp;
run;

The OUT option is used to store result in a data file. NOPRINT option prevents SAS to print it in results window.

Example 10 : Run Chi-Square Analysis

The CHISQ option provides chi-square tests of homogeneity or independence and measures of association between two categorical variables. Also it helps to identify the statistically significant categorical variables that we should include in our predictive model. All the categorical variables with a chi-square value less than or equal to 0.05 are kept.

The bar chart can be generated with PROC FREQ. ​To produce a bar chart for variable 'y', the plots=freqplot (type=bar) option is added. By default, it shows frequency in graph. In order to show percent, you need to add scale=percent. The ODS graphics ON statement tells SAS to produce graphs. Later we turn it off.

It is generally advisable to show distribution of a nominal variable after sorting categories by frequency. For ordinal variable, it should be shown based on level of categories.

To order categories based on a particular FORMAT, you can use order = FORMATTED option.

Conclusion

PROC FREQ is a simple but powerful SAS procedure. This tutorial was designed for beginners who have no background of any programming language. Hope the above examples help to understand the procedure crystal clear.

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 7 years of experience in data science and predictive modeling. During his tenure, he has worked with global clients in various domains like banking, Telecom, HR and Health Insurance.

While I love having friends who agree, I only learn from those who don't.

Hi Deepanshu,Well explained. I have 1 query regarding ods file. We are using VM space and when we are trying to save the following error occured. Could you please help. ods rtf file = "C:\Users\kiran\table1a.rtf" style = forNESUG; WARNING: Style FORNESUG not found; Rtf style will be used instead. ERROR: Insufficient authorization to access /opt/sasinside/SASConfig/Lev1/SASApp/C:\Users\kiran\table1a.rtf.