SAS: PROC FREQ with Examples

Deepanshu Bhalla 33 Comments

This tutorial explains how to use PROC FREQ with examples in SAS.

What does PROC FREQ do?

The PROC FREQ procedure is used to summarize categorical variables in SAS. It calculates count/frequency and cumulative frequency of categories of a categorical variable. It can also produce bar charts and tests for association between two categorical variables.

Create a Sample Dataset

The program below creates a sample SAS dataset which will be used to explain examples in this tutorial.

data example1;
input x y $ z;
cards;
6 A 60
6 A 70
2 A 100
2 B 10
3 B 67
2 C 81
3 C 63
5 C 55
;
run;

The created dataset looks like below -

X Y Z
6 A 60
6 A 70
2 A 100
2 B 10
3 B 67
2 C 81
3 C 63
5 C 55
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 statement tells SAS to return n-way frequency and cross-tabulation 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.

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;
PROC FREQ: NOCUM Option

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

proc freq data = example1;
tables y /nopercent nocum;
run;
PROC FREQ: 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 Example

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.

Example 5 : Hide Unwanted Statistics in Cross Tabulation
proc freq data = example1;
tables y * x / norow nocol nopercent;
run;

The NOROW option hides row percentage in cross-tabulation. Similarly, NOCOL option suppresses column percentage.

PROC FREQ: NOROW and NOCOL Options
Example 6 : Request Multiple Tables

Suppose you want to generate multiple crosstabs. To accomplish it, you can run the command below-

proc freq data = example1;
tables y * (x z) / norow nocol nopercent;
run;

The tables y*(x z) statement is equivalent to tables y*x y*z statement. In this case, it returns two tables - y by x and y by z.

Example - tables (a b)*(c d); is equivalent to tables a*c  b*c  a*d  b*d;

Example 7 : Number of Distinct Values

The NLEVELS option is used to count number of unique values in a variable.

proc freq data = example1 nlevels;
tables y;
run;

In this case, it returns 3 for variable Y.

Example 8 : Use WEIGHT Statement

The WEIGHT statement is used when we already have the counts. It makes PROC FREQ use count data to produce frequency and cross-tabulation tables.

Data example2;
input pre $ post $ count;
cards;
Yes Yes 30
Yes No 10
No Yes 40
No No 20
;
run;
proc freq data=example2;
tables pre*post;
weight count;
run;
PROC FREQ WEIGHT Statement
Example 9 : Store result in a SAS dataset

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.

proc freq data = example1 noprint;
tables y * x/chisq;
output All out=temp_chi chisq;
run; 
Example 11 : Generate Bar Chart and Dot Plot

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.

Ods graphics on;
Proc freq data=example1 order=freq;
Tables y/ plots=freqplot (type=bar scale=percent);
Run;
Ods graphics off;
PROC FREQ: Generate Bar Chart

Similarly, we can produce dot plot by adding type=dot. See the implementation below-

Ods graphics on;
Proc freq data=example1 order=freq;
Tables y/ plots=freqplot (type=dot);
Run;
Ods graphics off;
Example 12 : Include Missing Values in Calculation

By default, PROC FREQ does not consider missing values while calculating percent and cumulative percent. The number of missing values are shown separately (below the table). Refer the image below.

Proc freq data=sashelp.heart;
Tables deathcause;
Run; 
PROC FREQ: Exclude Missing

By adding MISSING option, it includes missing value as a separate category and all the respective statistics are generated based on it.

Proc freq data=sashelp.heart;
Tables deathcause / missing;
Run;
PROC FREQ: Include Missing
Example 13 : Ordering / Sorting

In PROC FREQ, the categories of a character variable are ordered alphabetically by default. For numeric variables, the categories are ordered from smallest to largest value.

To sort categories on descending order by frequency (from largest to smallest count), add ORDER=FREQ option.

Proc freq data=sashelp.heart order = FREQ;
Tables deathcause / missing;
Run;

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.

Related Posts
Spread the Word!
Share
About Author:
Deepanshu Bhalla

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

Post Comment 33 Responses to "SAS: PROC FREQ with Examples"
  1. Good explaination!

    ReplyDelete
  2. Great tutorial...
    could you also explain more on 'WEIGHT statement'

    ReplyDelete
    Replies
    1. yes youre right? even i do not understand this syntax called 'weight';

      Delete
  3. Replies
    1. Good question bank and explanations.Keep posting!!

      Delete
  4. Good Work, luck to stumble upon your site.

    ReplyDelete
  5. Amazing tutorial. Keep up the good work!

    ReplyDelete
  6. Amazing description. You are really making it easy for SAS beginners :)
    Thank you for your effort and resources

    ReplyDelete
  7. how to use by statement in proc freq

    ReplyDelete
  8. Good presentation sir

    ReplyDelete
  9. 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.

    ReplyDelete
  10. Excellent explanations. Really easy to understand SAS programming through your portal.

    ReplyDelete
  11. Excellent explanations. Really easy to understand SAS programming through your portal.

    ReplyDelete
  12. Excellent explanations. You make me the life easier.

    ReplyDelete
  13. That is a very nice and well explained summary, thank you!

    ReplyDelete
  14. thanks for excellent turorials. copy and pasted your code but not working..

    proc freq data = example1 noprint;
    tables y * x/chisq;
    output All out=temp_chi chisq;
    run;

    ReplyDelete
  15. Fantastic! Every one of the tutorials worked for me.
    Thank you very much.

    Charles

    ReplyDelete
  16. Thank you for you explanation. Very nice of you!

    ReplyDelete
  17. Exellent explanation ! Thank You so much!

    ReplyDelete
  18. This website is pure gold for newcomers like me who want to get into coding.
    Thank you so very much!!

    ReplyDelete
  19. write a program to get output using proc freq
    X1 X2
    N 19
    M 10
    F 9

    ReplyDelete
  20. write a program to get output using proc freq
    X1 X2
    N 19
    M 10
    F 9

    Ans: I think we can create a new data set and run the proc freq. I pasted the code below I hope this is what you expecting please let me know.

    data temp;
    input x1 :$10. x2;
    datalines;
    N 19
    M 10
    F 9
    ;
    run;
    proc freq data= temp;
    run;

    ReplyDelete
Next → ← Prev