Skip to main content
v2026.11,610 entries · CC-BY 4.0

Running a t-Test in Stata: ttest and ttesti

How to run a t-test in Stata: the ttest command for raw data, the ttesti immediate form for summary statistics, the one-sample/two-sample/paired variants, the unequal and welch options, and how to read Stata’s output block.

Ask about Running a t-Test in Stata: ttest and ttesti

Answers are drawn from this guide and the rest of the CASRAI corpus, with a link to every source.

Answers are AI-generated from CASRAI’s own published pages and can be wrong, so check the linked sources before relying on one; your question is logged without personal data — never sold, never used to train a third-party model — to show us what CASRAI is missing, so please do not type personal or confidential details. How we use this

Written and maintained by CASRAI Editorial Board

Last updated

Stata has two commands for a t-test: ttest runs the test on raw data already loaded in memory, and ttesti runs the identical test from typed-in summary statistics (n, mean, standard deviation) when you only have a published table, not the original observations. Both commands cover the same three designs — one-sample, two-sample independent, and paired — and both print the same output block, so once you can read one, you can read the other.

This page is the Stata-specific procedure: exact syntax, the unequal/welch options, and how to read every row Stata prints. For the statistical logic behind the test itself — why the three designs exist, what the assumptions are, and when a t-test is the wrong tool — see CASRAI’s guide to the t-test first and come back here to run it in Stata specifically.

ttest vs. ttesti: which one you need

Use ttest whenever the underlying observations are loaded in your dataset — Stata computes the mean, standard deviation, and standard error itself. Use ttesti (the “immediate” form) when you don’t have the raw data at all, only reported summary numbers — a results table in a paper you’re re-analyzing, a colleague’s SPSS output, or numbers from a grant report. ttesti takes the sample size, mean, and standard deviation as typed arguments and reconstructs the identical test.

The two commands are not interchangeable for every design: ttesti has no paired variant. A paired t-test needs the correlation between the two sets of measurements (or, equivalently, the individual within-pair differences), which summary statistics alone don’t contain — two means and two standard deviations aren’t enough to reconstruct how strongly paired observations move together. If you need a paired test and only have summary numbers, you need the standard deviation of the differences themselves, not of each variable separately; without the raw data or that specific figure, Stata cannot run it.

One-sample t-test

Tests whether a variable’s mean differs from a fixed, hypothesized value.

ttest varname == #

For example, testing whether average IRB review turnaround at one institution (variable days) differs from a 30-day benchmark:

ttest days == 30

Immediate form, from summary statistics only (sample size, mean, standard deviation, then the test value):

ttesti 40 34.2 9.6 30

Two-sample independent (unpaired) t-test

Compares means between two separate, unrelated groups. In long-format data (one row per subject, a grouping variable, and a single outcome column), use by():

ttest outcome, by(groupvar)

If the two groups are instead stored as two separate variables in wide format — and are genuinely independent samples, not paired — add the unpaired option:

ttest var1 == var2, unpaired

This is the syntax point that trips people up most: writing ttest var1 == var2 without unpaired does not error — it silently runs a paired test instead, comparing the two variables as matched pairs. If your two variables are actually independent groups, that gives you the wrong test with no warning. Always specify unpaired explicitly when comparing two separate variables that are not matched.

Immediate form, from summary statistics for both groups (n, mean, sd for group 1, then n, mean, sd for group 2):

ttesti 45 68.24 8.08 45 72.58 8.78

Paired t-test

Compares two related measurements on the same subjects — before/after scores, matched pairs. This is ttest‘s default behavior when you give it two variables and no options:

ttest var1 == var2

For example, comparing publication counts for the same researchers before and after a mentoring intervention (pubs_before, pubs_after):

ttest pubs_before == pubs_after

There is no paired ttesti form — see the ttest-vs-ttesti section above for why.

The unequal and welch options

By default, Stata’s two-sample ttest assumes both groups have equal population variances and pools them into a single estimate (Student’s t-test). Add unequal to drop that assumption and run the non-pooled version instead, using Satterthwaite’s approximation for the degrees of freedom:

ttest outcome, by(groupvar) unequal

Add welch together with unequal to use Welch’s degrees-of-freedom formula instead of Satterthwaite’s — the two approximations usually land close to each other but aren’t identical:

ttest outcome, by(groupvar) unequal welch

unequal is only valid for the two-sample design; it isn’t meaningful for a one-sample or paired test, since neither of those compares two independent groups’ variances. As a practical default, if you don’t have a specific reason to assume the two groups share the same variance, running unequal costs little and avoids a wrong pooled standard error when variances genuinely differ.

Reading Stata’s output

Every ttest/ttesti variant prints the same structure. Here is what a two-sample result looks like (illustrative numbers, laid out to show the format — not a reported research finding):

Two-sample t test with equal variances
------------------------------------------------------------------------------
   Group |     Obs        Mean    Std. err.   Std. dev.   [95% conf. interval]
---------+--------------------------------------------------------------------
       0 |      45       68.24        1.20        8.08       65.82       70.67
       1 |      45       72.58        1.31        8.78       69.94       75.22
---------+--------------------------------------------------------------------
Combined |      90       70.41        0.91        8.62       68.60       72.22
---------+--------------------------------------------------------------------
    diff |                -4.33        1.78                  -7.87       -0.79
------------------------------------------------------------------------------
    diff = mean(0) - mean(1)                                    t =  -2.43
H0: diff = 0                                     Degrees of freedom =      88

    Ha: diff < 0                 Ha: diff != 0                 Ha: diff > 0
 Pr(T < t) = 0.0084         Pr(|T| > |t|) = 0.0169          Pr(T > t) = 0.9916
  • Group rows — obs, mean, standard error, standard deviation, and a 95% CI for each group’s mean separately. (Stata versions before 16 label these columns “Std. Err.” and “Std. Dev.”; the lowercase style shown here is current.)
  • diff row — the difference between the two group means, its own standard error, and the CI for that difference. This is the number you’re actually testing.
  • t and degrees of freedom — the test statistic and the df used to evaluate it. Under unequal, df is a fraction (the Satterthwaite or Welch approximation), not a whole number — that’s expected, not an error.
  • The three Ha columns — Stata always reports all three alternative hypotheses (difference less than zero, not equal to zero, greater than zero) in one row, with their own p-values (Pr(T < t), Pr(|T| > |t|), Pr(T > t)). Almost every applied use wants the middle one, the two-sided Pr(|T| > |t|) — that’s the p-value that answers “is there a difference at all,” and it’s the one to report unless you had a directional hypothesis specified in advance. For what a p-value does and doesn’t mean once you have it, see CASRAI’s guide to what a p-value is.

The one-sample and paired forms print the same layout with one row instead of two group rows, and the diff row showing the difference from the fixed value (one-sample) or the mean of the paired differences (paired).

Common mistakes

  • Forgetting unpaired. Covered above — the single most common source of a silently wrong result with this command.
  • by() requires a variable with exactly two values. If groupvar has three or more distinct values, ttest ..., by(groupvar) returns an error rather than running a two-sample test on the first two it finds. For three or more groups, a t-test is the wrong tool regardless — see ANOVA.
  • Missing values silently reduce your n. Stata drops observations with a missing value in either the outcome or the grouping variable before computing Obs — check that the reported Obs matches what you expect, especially after a merge.
  • Reporting the wrong tail. Because Stata prints all three p-values in one row, it’s easy to accidentally cite Pr(T < t) or Pr(T > t) in a write-up when the two-sided Pr(|T| > |t|) is what a non-directional hypothesis calls for.

Frequently asked questions

What’s the difference between ttest and ttesti in Stata?

ttest runs on raw data already loaded into Stata; ttesti (the immediate form) runs the same test from typed-in summary statistics — sample size, mean, and standard deviation — when you don’t have the original observations. They produce identical output for the designs both support.

How do I run a paired t-test in Stata?

Use ttest var1 == var2 with no options — that’s the default behavior for two variables. There is no immediate (ttesti) form for a paired test, because summary statistics alone don’t capture the correlation between the paired measurements.

Why did my ttest command give a different answer than I expected?

The most common cause is comparing two independent-sample variables without adding the unpaired option — Stata defaults to a paired test whenever you give it two variables and no by(). Check whether your two variables represent matched pairs or genuinely separate groups, and add unpaired if it’s the latter.

When should I use the unequal or welch option?

Add unequal when you have reason to think the two groups have different population variances — it switches from a pooled standard error to a non-pooled one with an approximated degrees of freedom. Add welch alongside unequal to use Welch’s specific df formula. If you’re unsure whether variances are equal, running unequal is a low-cost way to avoid relying on that assumption.

Can I run a t-test in Stata without the raw data?

Yes, for the one-sample and two-sample independent designs, using ttesti with the sample size, mean, and standard deviation you already have — from a published table, another program’s output, or a report. The paired design is the one exception; it requires either the raw paired observations or the standard deviation of the differences specifically.

For choosing between Stata and other statistical packages more generally, see CASRAI’s comparisons of SPSS vs. Stata and R vs. Stata. For the underlying statistical choice between a t-test and a z-test, see z-test vs. t-test.

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →

Regulatory Radar

Stop finding out after the fact

$29/month, cancel anytime. Daily digest updates from our analysis, a dashboard holding the same items, and a cited assistant for everything they raise.

  • Federal Register, Federal Register+, Grants.gov, Regulations.gov, NSF News, UKRI, plus CASRAI’s own published content.
  • 44,322 indexed passages, and every answer cites the ones it drew on.