Opens in a new tab

Paired Samples t-Test in Stata
Wide Data, the Difference and Its Interval

A paired samples t-test in Stata reads two columns, one per measurement, so long-format data must be reshaped to wide first. The output gives the mean difference with its interval, and the standardized change is computed from the differences.

What You'll Need
Stata with the dataset open
Two measurements per participant
A participant ID variable

IN SHORT

What ttest Estimates for Two Linked Measurements

A paired samples t-test estimates the mean of the within-pair differences, for participants measured twice or for matched pairs, and how precisely the data fix it. In Stata the command is ttest followed by the two variable names joined by a double equals sign, and it needs wide data with one row per pair. The output prints both means, the mean difference with its confidence interval and three p-values, while the standardized change is computed separately from the differences. OnlineSPSS, a specialist statistical consultancy for academic, clinical and professional research, carries out and reviews this analysis through an in-house PhD-level statistician.

In This Guide

1
Reshape Long Data to Wide
2
Generate the Difference Variable
3
Plot the Differences Directly
4
Run ttest on the Two Variables
5
Read the Interval and df
6
Compute a Standardized Change

ANALYSIS SUPPORT

Analysis Support for This Method

A mean of differences is one summary of change. Ordinal ratings, or a handful of pairs with heavily skewed differences, are better described by a test built on the signs and ranks of those differences.

Paired Samples t-Test

Mean change within pairs estimated with an interval, from the check on the differences to the effect size and the sentence that reports them.

Explore Paired Samples t-Test
Wilcoxon Signed-Rank Test

Linked measurements compared through the ranks of their differences, for ordered outcomes or when the mean change describes few of the pairs well.

Explore Wilcoxon Signed-Rank Test

BEFORE YOU RUN IT

What ttest Needs From a File of Linked Pairs

Stata compares two columns that sit on the same row, so the layout and the direction of the subtraction are settled before the command runs.

One Row per Pair Wide data for paired tests in Stata put each participant or matched pair on one row, with the first measurement in one variable and the second in another. A long file with a time column needs a reshape before ttest can read it.

A Direction of Change Fixed Stata subtracts the second name from the first, so naming the later measurement first keeps a positive difference meaning an increase. The order is chosen before the command is typed.

Only Complete Pairs Count ttest keeps the pairs in which both variables are observed, so the n in the output can be smaller than the number of participants. The complete pairs are counted before the test is read.

A Plot of the Differences The assumption concerns the differences, not each measurement. A histogram or box plot of the generated difference shows skew and extreme pairs before any p-value is read.

IN STATA

How to Run a Paired Samples t-Test in Stata

The paired ttest syntax in Stata and reshape both ship with Stata itself, so nothing needs installing, and the sign of the generated difference follows the order of the two names in the command.

1

Reshape Long Data to Wide

If each participant has one row per occasion, use reshape wide so that a single row holds both measurements in separate variables.

Check: reshape stops with an error when another variable changes within an ID. Read that message instead of working around it, because it points to rows that do not describe the same participant.

2

Generate the Difference Variable

Create the difference as the later measurement minus the earlier one, so that its sign has a plain meaning in the report.

Tip: Label the new variable with its direction, for example later minus earlier, so the table, the plot and the sentence all use the same sign.

3

Plot the Differences Directly

Draw a histogram or box plot of the differences to see skew, extreme pairs and whether the changes cluster around zero.

Watch: Differences piled at zero or at the limit of the scale point to a coarse or bounded outcome. With few pairs, one extreme difference can also move the mean change, so say how such a pair was examined.

4

Run ttest on the Two Variables

Run ttest with the two names joined by ==, listing the later measurement first so that the output matches the generated difference.

Check: The diff row should agree with the mean of the generated variable, and Obs should equal the number of complete pairs counted earlier. A mismatch points to a coding or missing-value problem.

5

Read the Interval and df

Read the mean difference with its confidence interval, the degrees of freedom (the number of pairs minus one) and the middle p-value.

Tip: Quote the interval in the original unit of the outcome and say what change would matter in practice, since the interval alone does not make that judgment.

6

Compute a Standardized Change

Get the standardized change from the difference variable with summarize, and name the standardizer that was used.

Tip: Dividing by the standard deviation of the differences answers a different question from dividing by the spread of the raw scores. Name which one the effect size uses.

Stata Commands
reshape wide score, i(id) j(time)
generate diff = score2 - score1
histogram diff, normal
ttest score2 == score1
summarize diff
display r(mean)/r(sd)

READING THE OUTPUT

Reading the Paired Block of ttest Output

The two variables and the diff row share one table, and the direction of the subtraction is printed once, below it, in a single line that is often skipped.

Output element What it tells you What to check
Header: Paired t test That the paired form ran, with both variables listed That the names are the intended measurements
Variable rows: Obs, Mean, Std. err., Std. dev. Size, mean and spread of each measurement, with an interval for each mean (Std. Err. in older releases) That Obs equals the number of complete pairs
diff row The mean of the within-pair differences with its confidence interval That the interval is read with the direction of change in mind
mean(diff) = mean(first – second) The direction, set by the order of the names after ttest That the sentence states the direction in words
t and Degrees of freedom The statistic and its degrees of freedom, which equal the pairs minus one That df is quoted as printed and agrees with the pair count
Ha: mean(diff) != 0, the middle Pr line The two-sided alternative and its p-value That the middle line is used unless a direction was fixed in advance
Ha: mean(diff) < 0 and mean(diff) > 0 One-sided alternatives with their own p-values That neither is chosen once the sign of the change is known
summarize diff Mean and standard deviation of the differences That the standardizer behind the effect size is named

REPORTING

Reporting the Change and Its Standardized Size

The direction, the count of complete pairs and the standardizer are details that often go missing between the console and the manuscript.

Give t(df) = ___ and p = ___ with df as printed, then the mean difference with its 95% CI [___, ___] in the units of the outcome, and say in words which measurement was higher, because the sign follows the order of the names in the command. Add the two means and standard deviations and the number of complete pairs, since ttest leaves out any pair with a missing value. State the standardized change as Hedges’s g or Cohen’s d, name its standardizer (the standard deviation of the differences or of the scores) and give an interval from a stated method. The esize command is documented for two independent groups, so for linked data the value is computed from the difference variable.

Direction in WordsMean Difference CIComplete PairsStandardizer NamedEffect Size Method

COMMON MISTAKES

Misreported Pairs, Signs and Standardizers

The diff row, the Obs column and the do-file between them reveal all six, which is why they are read side by side with the draft.

Pairing Dropped Adding the unpaired option treats the two columns as independent samples, which tends to give a wider interval when the scores of a pair are positively correlated.

Direction Reversed The first name after ttest is the one subtracted from. A sentence saying scores rose beside a negative diff, or the reverse, flips the finding, so the sign is checked against the two means.

Participants Counted Instead of Pairs The degrees of freedom and Obs reflect complete pairs only. Quoting the number recruited beside a statistic computed on fewer pairs misstates the analysis.

Normality Checked on Raw Scores The assumption concerns the differences. Two skewed measurements can still give roughly symmetric differences, so the plot that matters is the one of the diff variable.

Effect Size From a Two-Group Command Standardizing by the pooled spread of two columns ignores the pairing and answers a different question from change within pairs. The standardizer used is named in the text.

Order and Carry-Over Left Unmentioned With repeated measures on the same people, practice, order and carry-over can look like change. The design section says how order was handled, because ttest cannot.

Reshape done but the direction of change unconfirmed? Independent Statistical Review reads an analysis of linked measurements from the file layout to the reported sentence.

Request an Assessment

What's Next

Three or more occasions on the same people lead to Repeated Measures ANOVA.
A yes or no outcome recorded twice per person is covered by McNemar's Test.
Two unrelated groups in place of linked pairs fall under Independent Samples t-Test.

FREQUENTLY ASKED QUESTIONS

Questions About ttest on Linked Measurements

Stata wants the pairs on one row before the test and offers no paired effect size after it.

No: ttest var1 == var2 compares two variables on the same row, so long data are converted with reshape wide first. The time column becomes a suffix on the outcome name, and each participant ends up on one row. A mixed model, which reads the long layout directly, is the alternative when the data are incomplete.

The esize command is documented for two independent groups, so the value for linked data is computed from the difference variable. Divide the mean difference by the standard deviation of the differences, or by the spread of the scores if that is the question, and name the choice. A small-sample correction turns d into g, and an interval needs its own stated method.

Yes: a paired test is a one-sample test on the differences, so ttesti with the number of pairs, the mean and standard deviation of the differences and a hypothesized mean of 0 reproduces it. The two means and their separate standard deviations are not enough, because the correlation between the measurements is missing from them. It is a way to check a reported result without the raw data.

As far as the reshape: an in-house PhD-level statistician at OnlineSPSS goes through it, the difference variable, the printed ttest block and the sentence you plan to submit, looking at direction, the count of pairs and the standardizer named. Tables and wording for the results section are a separate stage, handled under Results Reporting & Interpretation, and what the change means for your field is for you to decide.

Your data and project details are handled confidentially, and project information is not published. Researcher names and institutions are not used publicly without permission. An NDA is available where required and can be arranged before sensitive materials are shared. Read more about confidentiality and data security.

You share your research question, design and relevant materials, such as the dataset or the output you have, and a statistically trained Research Coordinator reviews the request and responds the same day. The analysis, deliverables, timeline and one fixed project fee are then confirmed before any work begins, so the timeline follows from the agreed scope.

Still have a question? Find more answers in our full FAQ.

View All FAQs →

NEXT STEP

Is Later Minus Earlier
Spelled Out in Your Report?

The diff row prints a number and a sign, and only the text can say which measurement it subtracts from which. The do-file, or the long-format file before reshaping, settles that for the assessment.

No Commitment Required

Strictly Confidential

Fixed Project Fee

In-House PhD-Level Statisticians

Same-Day Response

Serving Researchers Since 2014