Written and maintained by CASRAI Editorial Board
Last updated
Stata’s tabulate command builds a two-way frequency table, and adding the chi2 option makes it print the Pearson chi-square test of independence for that same table in one step — no separate test command is needed. This page is the Stata-specific procedure: exact syntax, how to read the Pearson chi2 line Stata prints, what to do when expected cell counts run small, and tab2 for testing several variable pairs without retyping the command each time.
For the statistical logic behind the test itself — what independence means, the assumptions, and the difference between a test of independence and a goodness-of-fit test — see CASRAI’s guide to the chi-square test first and come back here to run it in Stata specifically.
tabulate with the chi2 option
The base command:
tabulate varname1 varname2, chi2
For example, testing whether an IRB protocol’s outcome (approved or deferred) is independent of its submission track (expedited or full board):
tabulate outcome track, chi2
Stata prints the crosstabulation — observed counts with row and column totals — followed by the test statistic. You can combine chi2 with the usual display options in the same command: row and column for row/column percentages, cell for cell percentages, and expected to print the expected count under independence next to each observed count, which is exactly what you need to check for the small-cell problem covered below:
tabulate outcome track, chi2 expected column
Add V to the same command to get Cramér’s V, a standardized effect size for the association — the chi-square statistic and its p-value tell you whether an association is detectable, not how strong it is; see CASRAI’s guide to effect size for why that distinction matters when you write up the result.
Reading the Pearson chi2 line
Illustrative output, laid out to show the format and computed to be internally consistent — not a reported research finding:
| track
outcome | Expedited Full board | Total
-----------+-----------------------+----------
Approved | 62 41 | 103
Deferred | 8 19 | 27
-----------+-----------------------+----------
Total | 70 60 | 130
Pearson chi2(1) = 8.0414 Pr = 0.005
- The table itself — observed counts, with row and column totals. Nothing here is a test result yet; it is the raw crosstabulation the test is computed from.
chi2(1)— the degrees of freedom in parentheses, computed as (rows − 1) × (columns − 1). For a 2×2 table that is always 1, which is why the small-cell caveat below matters more for tables this size than for larger ones.Pr— the p-value for the null hypothesis that the two variables are independent (no association) in the population. This is what most write-ups report next to the chi-square statistic. For what a p-value does and does not mean once you have it, see CASRAI’s guide to what a p-value is.
Unlike some other statistical packages, Stata does not automatically flag or footnote low expected cell counts next to the chi2 result — you have to check for them yourself, which is what the expected option is for.
Small expected cell counts: when to use Fisher’s exact instead
The Pearson chi-square statistic is an approximation that assumes each cell’s expected count under independence is large enough for the chi-square reference distribution to be a good fit. The widely used rule of thumb is an expected count of at least 5 in every cell; when a cell falls below that, the reported Pr value becomes unreliable — usually overstating significance.
Add exact to run Fisher’s exact test instead, which computes the p-value directly from the hypergeometric distribution rather than relying on the chi-square approximation, so it is valid at any sample size:
tabulate varname1 varname2, exact
Stata reports it on its own line, with no test statistic — Fisher’s exact does not have one, only a p-value:
Fisher's exact = 0.057
Small illustrative example: a 10-person treatment group and a 10-person control group, 7 events total (again, constructed to show the format, not a reported finding):
| group
outcome | Treatment Control | Total
-----------+-----------------------+----------
Event | 1 6 | 7
No event | 9 4 | 13
-----------+-----------------------+----------
Total | 10 10 | 20
Pearson chi2(1) = 5.4944 Pr = 0.019
Fisher's exact = 0.057
Two of the four expected counts in this table are 3.5, below the rule-of-thumb floor of 5. The chi-square approximation puts the p-value at 0.019; the exact test, which does not depend on that approximation, puts it at 0.057 — a materially different conclusion at a conventional 0.05 threshold. This is the concrete reason to check expected before trusting a chi-square p-value on a small or unevenly distributed table, not just a formality.
For a 2×2 table Fisher’s exact is fast regardless of sample size; for larger tables (more than two rows or columns) it is computationally heavier, and Stata may take noticeably longer or need more memory as the table grows — run chi2 alone first if you just need a quick check on a large table, and add exact once you already suspect small-cell trouble.
tab2: the same test across every pair in a list of variables
tabulate only ever compares two variables at a time. When you have several categorical variables and want the pairwise association between every combination of them, tab2 runs tabulate once for every possible pair in a variable list, with the same options applied to each table:
tab2 varname1 varname2 varname3, chi2
For three variables that produces three separate two-way tables and three separate chi-square tests — every pair, not a single combined three-way table. tab2 accepts the same options as tabulate‘s two-way form, including exact, expected, and V, and adds one option of its own: firstonly, which restricts the output to only the pairs that include the first variable in your list — useful when that first variable is a single outcome you want tested against several candidate predictors, rather than every predictor tested against every other predictor as well:
tab2 outcome track reviewer, chi2 firstonly
One caution worth stating plainly: tab2 runs multiple significance tests in one command, and it does not apply any multiple-comparisons correction on its own. Running chi-square tests across many pairs and reporting whichever ones came back significant inflates the false-positive rate exactly the way running many t-tests would. If you are screening many variable pairs rather than testing one pre-specified pair, treat the p-values as exploratory and adjust or pre-register accordingly, rather than reporting raw Pr values from a large tab2 run as confirmatory results.
Common mistakes
- Treating
chi2‘s p-value as a measure of how strong an association is. A significant chi-square result only says an association is unlikely to be due to chance at your sample size — it says nothing about size. AddVfor Cramér’s V if you need an effect size to report alongside it. - Not checking expected counts before trusting
chi2. Addexpectedto see them, or runexactdirectly if your table is small or has an uneven margin — don’t assume the rule-of-thumb floor of 5 is met just because the total sample size looks reasonable; a skewed margin can produce a small expected count even in a fairly large dataset. - Running
tabulateon a continuous variable that was never categorized.tabulatetreats every distinct value as its own category, so a continuous variable with dozens of unique values produces a huge, useless table and a chi-square result that means little. Categorize it first (withegen ... cut()or a similarly deliberate cutpoint choice) if a categorical comparison is genuinely what you want. - Assuming
tab2‘s pairwise results are equivalent to a single test that controls for every variable at once. Each table in atab2run tests only that one pair, ignoring the others. If you need to test an association while holding a third categorical variable constant, that is a stratified analysis (or a model), not a largertab2run.
Frequently asked questions
What does chi2 mean in Stata’s tabulate output?
chi2 is the option that adds Pearson’s chi-square test of independence to a tabulate two-way table. Stata prints the test statistic with its degrees of freedom as Pearson chi2(df), followed by the p-value (Pr) for the null hypothesis that the two tabulated variables are independent.
How do I run Fisher’s exact test in Stata?
Add the exact option to tabulate: tabulate var1 var2, exact. It can be combined with chi2 in the same command so both results print together. Use it in place of, or alongside, chi2 whenever an expected cell count in the table falls below about 5.
Does Stata warn me automatically about small expected cell counts?
No. Unlike some other statistics packages, Stata’s tabulate does not print a warning next to the chi2 result when expected counts are low. You have to check for it yourself by adding the expected option, which prints the expected count under independence alongside each observed count.
What’s the difference between tabulate and tab2 in Stata?
tabulate takes exactly two variables and produces one two-way table. tab2 takes a list of three or more variables and runs tabulate once for every possible pair among them, using the same options for each table — it is a convenience wrapper, not a different statistical test.
How do I get an effect size for a chi-square test in Stata?
Add the V option to tabulate alongside chi2 to print Cramér’s V, a standardized measure of association strength that, unlike the chi-square p-value, does not simply grow with sample size. See CASRAI’s guide to effect size for how to interpret it.
For choosing between Stata and other statistical packages more generally, see CASRAI’s comparisons of SPSS vs. Stata and R vs. Stata. For running other common tests in Stata, see CASRAI’s guides to the t-test and regression in Stata.








