Skip to content
Tutorials Center
My learning

Topics

Lesson 21 of 37 ยท Procedures: PROC

PROC FREQ and Frequency Tables

Count categories with PROC FREQ: one-way and two-way tables, percentages, the TABLES options, missing values, WEIGHT and the chi-square test, with a Python stand-in.

PROC FREQ counts. Give it a categorical variable and it prints how many observations fall in each category, with percentages and cumulative totals. Give it two variables and it builds a cross-tabulation, and with one option it adds a chi-square test of independence. It is usually the first procedure you run on a new data set to look for odd categories, typos and missing values.

SAS is not available on this site, so the sas blocks are read-only and I do not print real SAS listings. The TryIts are Python stand-ins that compute the same counts so you can check your reasoning.

One-way tables

SAS
proc freq data=work.customers;
  tables country;
run;

The output table has four numeric columns for each category: Frequency, Percent, Cumulative Frequency and Cumulative Percent. You can list several variables, and each gets its own table:

SAS
proc freq data=work.customers;
  tables country segment status;
run;

Useful options after a slash:

Option Effect
nocum Drop the cumulative columns
nopercent Drop the percent column
missing Count missing values as a category and include them in percents
order=freq List categories by descending count instead of alphabetically
out=work.counts Save the counts as a data set
plots=freqplot Draw a bar chart (needs ODS Graphics)
SAS
proc freq data=work.customers order=freq;
  tables country / nocum missing out=work.country_counts;
run;

By default missing values are excluded from the table and from the percentages; SAS only mentions them in a note below the table. Use missing when you need them visible, which is the right choice when you are checking data quality.

Stand-in: a one-way table

Python Stand-in: one-way frequency table

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Compare the two tables: with missing the total grows from 9 to 10 and every percentage changes. That is the quiet trap of the default.

Two-way tables

Connect two variables with an asterisk to get a cross-tabulation. The first variable defines the rows and the second the columns.

SAS
proc freq data=work.customers;
  tables country * segment;
run;

Each cell shows the count, the percent of the grand total, the row percent and the column percent. Trim what you do not need:

SAS
tables country * segment / norow nocol nopercent;   /* counts only */
tables country * segment / nofreq nocol;            /* row percents only */
tables country * segment / list;                    /* one line per combination, no grid */

Three or more variables (a * b * c) produce a separate two-way table for each level of the earlier variables. Use crosslist or list when the layout gets large.

Stand-in: a cross-tabulation

Python Stand-in: two-way table with row percents

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Row percents answer "within each country, what share is business?". Column percents would answer "within each segment, what share is German?". Choose the one that matches your question.

Chi-square and other tests

Add chisq to ask whether the two variables look independent:

SAS
proc freq data=work.customers;
  tables country * segment / chisq expected;
run;

expected prints the counts you would see if the variables were unrelated. The listing includes the Pearson chi-square, a likelihood-ratio chi-square and, for 2-by-2 tables, Fisher's exact test. The chi-square approximation is unreliable when expected counts are small (a common rule is any expected count below 5), and that is the time to read Fisher's result instead. Other tables options include relrisk and riskdiff (for 2-by-2 medical tables), measures (association measures) and cmh (stratified tests).

Frequencies from summarized data

When your data set already holds a count per row, use weight:

SAS
proc freq data=work.summary;
  tables product;
  weight units;
run;

Each row now counts units times instead of once. Rows with a zero or missing weight are dropped from the table; add the zeros option on weight to keep zero-weight categories.

Using the output data set

out= saves counts so you can feed them into another step, for example to plot or to keep only common categories:

SAS
proc freq data=work.customers noprint;
  tables country / out=work.country_counts;
run;

data work.common;
  set work.country_counts;
  where count >= 100;
run;

The data set has the variable itself plus COUNT and PERCENT. noprint suppresses the report, since you only wanted the data.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What are the default columns in a one-way PROC FREQ table?

    A one-way table lists counts, percents, and their cumulative versions.
  2. 2. In tables a * b, which variable becomes the table rows?

    The first variable defines the rows and the second the columns.
  3. 3. How does PROC FREQ treat missing values by default?

    Add the MISSING option to include them as a category and in the percentages.
  4. 4. Which TABLES option requests a chi-square test of independence?

    CHISQ produces the chi-square tests; EXPECTED only prints the expected counts.
  5. 5. When is the WEIGHT statement useful?

    WEIGHT makes each observation count as many times as the weight variable says.

Summary

  • PROC FREQ with tables x; gives one-way counts; tables x * y; gives a cross-tabulation.
  • Options after / shape the table: nocum, norow, nocol, nopercent, missing, list, order=freq.
  • Missing values are excluded unless you write missing.
  • chisq adds independence tests; read Fisher's exact test when expected counts are small.
  • weight counts pre-summarized rows; out= saves counts as a data set for later steps.

Next: PROC MEANS and PROC SUMMARY