Command reference

tabulate — count categories and compare groups

Count the observations in each category, or cross-tabulate two categorical variables. Add percentages to compare differently sized groups or summarize a numeric variable within categories.

Syntax

tabulate rowvar [columnvar] [if condition] [in range] [, options]

With one variable, the table reports category frequencies, percentages, and cumulative percentages. With two variables, the first defines rows and the second defines columns. Numeric codes and string categories can both be tabulated. if and in restrict the sample. tab is an alias for tabulate.

Options

missing
Include missing category values in a frequency table instead of dropping those observations. The included missing categories contribute to the total used for percentages.
sort
For a one-way frequency table, order categories from most frequent to least frequent. Without it, categories follow their underlying numeric or text values; value labels do not change that order.
nopercent
Hide the Percent column in a one-way frequency table.
nocumul
Hide the Cumulative column in a one-way frequency table. Cumulative percentages are most useful when the category order has a meaningful interpretation.
row
For a two-way frequency table, add percentages within each row. Each cell is divided by its row total, so percentages across a nonempty row add to 100%.
col
For a two-way frequency table, add percentages within each column. Each cell is divided by its column total.
cell
For a two-way frequency table, add each cell's percentage of the entire table total.
nofreq
Hide the displayed frequency counts. Combine it with row, col, or cell when you want a two-way percentage table.
chi2
For a two-way frequency table, report Pearson's chi-squared test of independence, its degrees of freedom, and its p-value. The null hypothesis is that the row and column categories are independent.
summarize(numericvar)
Replace category frequencies with the mean, sample standard deviation, and nonmissing count of a numeric variable, within each category or combination of two categories. Observations missing that numeric variable are omitted.
generate(stub)
For a one-way frequency table, create one indicator per observed nonmissing category, named stub1, stub2, and so on. An indicator is 1 for its category and 0 for other nonmissing categories. Missing categories and observations outside the selected sample receive missing indicators. This option cannot be combined with summarize().

Example

The example counts the four race codes in 24 synthetic student records, then cross-tabulates race with the zero/one female variable.

clear
import delimited "data/student_scores.csv"
tabulate race
tabulate race female
Open example in browser

Sample data included. Opens in a new tab; click Run.

What to look for

Every race code appears six times, so each is 25% of the sample. Within every race code, three records have female=0 and three have female=1. These balanced counts were built into the teaching data; they describe no real population.

Example result
Race codefemale = 0female = 1Total
1336
2336
3336
4336
Total121224