Written and maintained by CASRAI Editorial Board
Last updated
To find the median in Stata, run summarize varname, detail and read the 50th percentile, or run tabstat varname, statistics(median) for a one-line answer. Four official routes produce a median, and they differ in what they show and what they store: summarize, detail leaves it in r(p50), tabstat prints it by group, egen median() writes it into a new variable, and centile adds a confidence interval. This guide covers how to calculate or get the median in Stata with each, and when to pick which.
What the median is used for in research
The median is the middle value of a sorted variable. Researchers report it instead of the mean when a variable is skewed or has outliers, and it is the usual summary behind rank-based comparisons such as the Mann-Whitney ranksum test in Stata and the centre line of a box plot in Stata. For a broader view of where summary statistics fit, see what statistics is.
One naming point saves confusion: Stata’s summarize help page never uses the word median. It calls the same quantity the 50th percentile, and stores it as r(p50). In tabstat, median and p50 are documented as the same statistic.
Install or access
Everything below uses official Stata commands (summarize, tabstat, egen, centile) documented on Stata’s own help pages at stata.com, so no user-written package is needed. You need access to a licensed copy of Stata through your institution or your own account; see stata.com for how to obtain it. The examples use the auto dataset that ships with Stata, loaded with sysuse auto.
Step-by-step: find the median in Stata
Method 1: summarize with detail (median as r(p50))
- Load data:
sysuse auto, clear - Run
summarize price, detail. Thedetailoption adds percentiles, skewness and kurtosis, plus the four smallest and largest values. - Read the 50th percentile in the output. That is the median.
- To use the value in later code, type
display r(p50)straight after the command. Stata’s help listsr(p50)as the 50th percentile, available withdetailonly.
To restrict the sample, add a condition, as in the help page’s own example summarize mpg weight if foreign, detail.
Method 2: tabstat (a median per variable or per group)
- For one or more variables:
tabstat price weight mpg, statistics(median). The default statistic is the mean, so you must request the median.statistics(p50)gives the identical result. - For a median by group:
tabstat price weight mpg, statistics(median) by(foreign). Theby()option shows the statistics separately for each unique value of the grouping variable. - To see the median beside other measures, list several statistics:
statistics(median iqr), whereiqris the interquartile range (p75 minus p25).
The descriptive statistics in Stata guide shows how this fits with summarize and codebook when you build a full summary table.
Method 3: egen median() (store the median as a new variable)
- Run
egen medprice = median(price). Stata’s help describesmedian(exp)as creating a constant (within the by-group, if any) holding the median of the expression. - For a median per group:
egen medprice_grp = median(price), by(foreign). Every row then carries its group’s median. - Nothing is printed. Check the result with
tabulate medprice_grporlist.
Use this route when the median feeds another calculation, for example centring a variable on its group median. To collapse a dataset to one row per group instead, collapse accepts a median statistic; see the collapse command in Stata guide.
Method 4: centile (the median with a confidence interval)
- Run
centile price. With no centiles specified,centilereports the median (the 50th centile). With no variable list it reports every variable. - Request specific centiles with a number list, for example
centile price, centile(25 50 75). - Read the estimate together with its confidence interval. The level defaults to 95%, and by default the interval uses a binomial method that assumes no particular distribution. The options
cci,normalandmeansdchange the method.
This is the route to use when you need to report uncertainty around a median, which summarize and tabstat do not provide. Stored results include r(c_#), r(lb_#) and r(ub_#) for the estimate and its bounds, and r(N) for the number of observations.
Which command should you use?
| You want | Command | Where the median ends up |
|---|---|---|
| A quick look next to other summary statistics | summarize varname, detail |
Printed as the 50th percentile; stored in r(p50) |
| A table of medians, optionally by group | tabstat varlist, statistics(median) by(groupvar) |
Printed table |
| The median as a variable for further calculations | egen newvar = median(varname), by(groupvar) |
New variable in the dataset |
| A median with a confidence interval | centile varname |
Printed estimate and interval; stored in r(c_#), r(lb_#), r(ub_#) |
Output and how to read it
- summarize, detail: the percentiles block includes the 50th percentile, which is the median. Compare it with the mean in the same output: a large gap signals skew.
- tabstat: a table of the statistics you requested, shown separately for each group when you use
by(). - egen: no output; the median is a constant within each group in the new variable.
- centile: the estimate for each requested centile with its confidence limits (95% by default).
Troubleshooting
r(p50)is missing or stale. It is only stored when you used thedetailoption. Withoutdetail,summarizedoes not return it.- Weights and detail.
summarizeallows aweights, fweights and iweights, but iweights cannot be combined withdetail. Use fweights or aweights withdetail, or drop the weights. - Medians by group with summarize. Use the by prefix (
bysort foreign: summarize price, detail) or switch totabstatwithby(). Note thatby()insidetabstatis an option, not the by prefix. - tabstat shows means. The default statistic is the mean; add
statistics(median). - egen printed nothing. That is expected: it creates a variable. Look in the Variables window or run
list.
Free alternatives
| Tool | How to get the median | Source |
|---|---|---|
| R | median(x, na.rm = FALSE) from the stats package; set na.rm = TRUE to strip missing values first. R describes itself as a free software environment for statistical computing. |
R documentation: median; r-project.org |
FAQ
How do I calculate the median in Stata?
Run summarize varname, detail and read the 50th percentile, or run tabstat varname, statistics(median). Both use official Stata commands.
How do I get the median in Stata for each group?
Use tabstat varname, statistics(median) by(groupvar) for a printed table, or egen newvar = median(varname), by(groupvar) to store each group’s median in a new variable.
Is the median the same as p50 in Stata?
Yes. The tabstat help lists median as the same as p50, and the summarize help reports the 50th percentile in r(p50).
Can Stata give a confidence interval for the median?
Yes. centile varname reports the median with a 95% confidence interval by default, using a binomial method that makes no distributional assumption.
Sources
- Stata help: summarize (checked 2026-10-29)
- Stata help: tabstat (checked 2026-10-29)
- Stata help: egen (checked 2026-10-29)
- Stata help: centile (checked 2026-10-29)
- R documentation: median (checked 2026-10-29)








