Stata Cheat Sheet

The core commands for this course

Author

Prof. Jack Reilly

Published

Fall 2026

A short reference for the Stata commands we actually use in PAI 721. Every command has more options than shown here; type help followed by any command name (for example, help summarize) to see them all. Words like var, group, y, and x are placeholders for your own variable names.

Don’t forget - for a more extensive references, you also have:

Setup & Files

Command What it does
cd "folder" Set the working directory (where Stata looks for files)
use "file.dta", clear Open a Stata dataset, closing whatever is already open
import delimited "file.csv", clear Open a .csv file
save "file.dta", replace Save the data in memory, overwriting the old copy
clear Wipe the data in memory
browse Open the spreadsheet view of your data
describe List the variables in the dataset
Note.do files

A .do file is a plain-text list of commands that Stata runs top to bottom. Open the editor with doedit. Lines starting with * or // are comments; use them to explain what you’re doing and to label which question each chunk of code answers. Put /// at the end of a line to continue a long command onto the next line.

Looking at Data

Command What it does
sum var Mean, standard deviation, min, max, and n (summarize)
sum var, detail Adds the median and percentiles (25th = Q1, 75th = Q3)
tab var Frequency table: how many cases take each value
tab var1 var2 Cross-tabulation of two variables
tabstat var, stats(mean sd n) Choose your own summary statistics
tabstat var, by(group) The same, separately for each group
bys group: sum var Run any command separately for each group
corr var1 var2 Correlation coefficient
sort var Sort cases from low to high (gsort -var for high to low)

Creating & Changing Variables

Command What it does
gen new = pts / g Create a new variable from a formula
replace var = 0 if var == 9 Change values that meet a condition
recode var 7=., gen(newvar) Recode values into a new variable (here, 7 becomes missing)
keep if year == 2010 Keep only the cases that meet a condition
drop if year == 1996 Drop the cases that meet a condition
keep var1 var2 Keep only these variables (drop var1 to drop one)
sample 20 Keep a random 20% of cases (sample 50, count for exactly 50)
set seed 12345 Run before sample so your random draw comes out the same each time
collapse (mean) var, by(group) Replace the data with one row per group
preserve … restore Take a snapshot of the data, do something to it, then get the original back

Conditions use == (equals), != (not equal), >, <, >=, <=, & (and), and | (or). Missing values are stored as . and Stata treats them as larger than any number, so keep if x > 10 also keeps the missings. Add & x < . to leave them out.

Graphs

Command What it does
histogram var Histogram of a continuous variable
histogram var, discrete percent Histogram of a variable with only a few whole-number values
graph bar (count), over(var) Bar chart of a categorical variable
scatter y x Scatterplot
scatter y x || lfit y x Scatterplot with a best-fit line
twoway (scatter y x) (lfit y x) Scatterplot with a best-fit line (alternate)
graph box y, over(group) Box plots, one per group
graph export "plot.png", replace Save the most recent graph to a file

Add titles and axis labels to any graph with options at the end of the command: , title("...") xtitle("...") ytitle("..."). Doing it in the command rather than the graph editor means it ends up in your .do file.

Regression & Tests

Command What it does
reg y x Regress y on x (regress)
reg y x1 x2 Multiple regression
predict yhat After reg: the predicted value for every case
predict resid, residuals After reg: the residual for every case
ci means var Confidence interval for a mean (add , level(90) for 90%)
ttest var == 30 One-sample t-test against a specific number
ttest var, by(group) Difference-in-means t-test (add unequal if the spreads differ)
tab var1 var2, chi2 expected Chi-square test on a crosstab, showing expected counts
tabi 16 4 \ 10 9 \ 6 18, chi2 Chi-square test on a table you type in (\ starts a new row)

Stata as a Calculator

display (or just di) prints the result of any expression, so you can do hand calculations without leaving Stata.

Command What it does
di (1820 - 1509) / 321 Arithmetic (here, a z-score)
di normal(0.97) Area to the left of z = 0.97 under the Normal curve
di 1 - normal(0.97) Area to the right of z = 0.97
di invnormal(0.975) The z-score with 97.5% of the area below it (1.96)
di invttail(35, 0.025) Critical t with df = 35 and 2.5% in the upper tail (for a 95% CI)
di sqrt(36) Square root
di _b[x] After reg: the slope on x (_b[_cons] is the intercept)