Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 36 ยท Descriptive and Graphs

Tables and Cross-tabulations

Build frequency tables and cross-tabulations in Stata with tabulate, tab1, tab2, row and column percentages, chi-square tests, collapse and the modern table command.

For categorical variables, numbers like a mean make no sense; you want counts and percentages. Stata's tabulate command makes frequency tables in one line, crosses two variables, and can run a chi-square test at the same time. This lesson covers one-way and two-way tables, percentages, tests, and the newer table command for flexible reports.

One-way tables

STATA
sysuse auto, clear
tabulate foreign                    // counts, percent, cumulative percent
tabulate rep78, missing             // show missing values as a row
tabulate foreign, plot              // adds a text bar chart
tabulate rep78, generate(rep_)      // creates indicator variables rep_1, rep_2, ...
tab1 foreign rep78 headroom         // one-way tables for several variables

Output has three columns: Freq. (count), Percent (share of the total) and Cum. (running percent). By default, observations with missing values are excluded and the percentages are of the non-missing total. Add missing to see them, and compare with count to be sure of the base.

tabulate varname, generate(prefix) is a convenient way to create a set of 0/1 indicator variables, one per category (the i. factor-variable notation in regression does this on the fly instead).

If the variable has value labels, the table shows the labels; add nolabel to see the underlying numbers. Variables with very many unique values cannot be tabulated (there is a limit that depends on your edition): use codebook or inspect instead.

Two-way tables

STATA
tabulate foreign rep78                  // rows: foreign, columns: rep78
tabulate foreign rep78, row             // row percentages
tabulate foreign rep78, column          // column percentages
tabulate foreign rep78, cell            // percentage of the grand total
tabulate foreign rep78, row nofreq      // percentages only
tabulate foreign rep78, missing         // include missing as a category
tab2 foreign rep78 headroom             // all pairs among the variables

Pick the percentages that answer your question:

  • Row percentages answer "among cars of each origin, what share is each rating?" (rows sum to 100%).
  • Column percentages answer "among cars with each rating, what share is foreign?" (columns sum to 100%).
  • Cell percentages answer "what share of all cars is in this cell?".

A frequent mistake is reading column percentages as if they were row percentages. Say aloud what the 100% base is before interpreting.

Tests of association

Add options to test whether the two variables are related:

STATA
tabulate foreign rep78, chi2           // Pearson chi-square test
tabulate foreign rep78, exact          // Fisher's exact test (small samples)
tabulate foreign rep78, expected       // expected counts under independence
tabulate foreign rep78, chi2 lrchi2 V  // also likelihood-ratio chi-square and Cramer's V

The Pearson chi-square compares the observed counts with the counts expected if the two variables were independent. A small p-value is evidence of association. It requires enough data: a common rule is that expected counts should be 5 or more in most cells; use exact when many are smaller. Statistical significance says nothing about the size of the effect, so look at the percentages and at Cramer's V (0 to 1) too.

Tables of summaries

tabulate can also summarize another variable by category:

STATA
tabulate foreign, summarize(price)            // mean, sd, frequency of price per category
tabulate foreign rep78, summarize(price) means // two-way table of means

These tables are quick but limited. For anything richer, use tabstat, table or collapse.

The table command (Stata 17 and later)

Modern Stata has a redesigned table command that builds multi-dimensional tables with any statistic and can export them:

STATA
table foreign                                        // frequencies
table (foreign) (rep78), statistic(frequency)         // crosstab
table (foreign) (rep78), statistic(percent, across(rep78))
table foreign, statistic(mean price mpg) statistic(sd price mpg) nformat(%9.2f)
table foreign rep78, statistic(count price) statistic(mean price)

The format is table (row variables) (column variables), statistic(...). It produces a collection of results that you can label, style and export to Word, Excel, PDF, HTML or Markdown with collect export. That connects to the reporting chapter. In older versions (before 17), table has a different syntax with contents(); check about and help table for your version.

Counting with by and egen

Counts can also be stored as variables:

STATA
bysort foreign: generate n_group = _N
egen n_rep = count(price), by(rep78)
contract foreign rep78, freq(n) percent(pct)    // replaces the data with a table of counts

contract turns the tabulation into a dataset (one row per combination), which you can then merge, graph or export.

Multiple responses and surveys

A question where respondents tick several options is stored as several 0/1 variables (opt1, opt2, opt3). Use tab1, or egen total = rowtotal(opt1-opt3), and graph the means of the indicators. For survey data with weights and clusters, use svy: tabulate varname1 varname2, row after svyset; it reports design-based percentages and an adjusted test.

Stand-in: a crosstab with row percentages

The Python model builds the table by counting pairs and printing counts with row percentages, as tabulate a b, row does. It then computes a Pearson chi-square statistic by hand.

Python Stand-in: tabulate a b, row chi2

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.

Python Stand-in: one-way table with cumulative percent

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.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which option shows percentages that add to 100 within each row?

    row gives row percentages; column gives column percentages.
  2. 2. What does tabulate rep78, missing change?

    By default, missing values are left out of the table; missing includes them.
  3. 3. Which test fits a crosstab with very small counts?

    Use exact when expected counts are small and the chi-square approximation is unreliable.
  4. 4. Which command turns a table of counts into a dataset?

    contract replaces the data with one row per combination and its frequency.

Summary

  • tabulate makes one-way and two-way frequency tables; missing shows missing values; generate() creates indicators.
  • Choose row, column or cell percentages by the question you ask and name the 100% base.
  • chi2, exact and V test and measure association; significance is not effect size.
  • tabulate, summarize() and table (Stata 17+) report statistics by category; contract creates a data table.
  • Weighted or survey data need svy: tabulate.

Next: Graph Basics