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

How to Find the Median in Stata: summarize, tabstat, egen and centile

Four official ways to find the median in Stata: summarize detail, tabstat, egen median() and centile, with commands, stored results and fixes.

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))

  1. Load data: sysuse auto, clear
  2. Run summarize price, detail. The detail option adds percentiles, skewness and kurtosis, plus the four smallest and largest values.
  3. Read the 50th percentile in the output. That is the median.
  4. To use the value in later code, type display r(p50) straight after the command. Stata’s help lists r(p50) as the 50th percentile, available with detail only.

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)

  1. 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.
  2. For a median by group: tabstat price weight mpg, statistics(median) by(foreign). The by() option shows the statistics separately for each unique value of the grouping variable.
  3. To see the median beside other measures, list several statistics: statistics(median iqr), where iqr is 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)

  1. Run egen medprice = median(price). Stata’s help describes median(exp) as creating a constant (within the by-group, if any) holding the median of the expression.
  2. For a median per group: egen medprice_grp = median(price), by(foreign). Every row then carries its group’s median.
  3. Nothing is printed. Check the result with tabulate medprice_grp or list.

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)

  1. Run centile price. With no centiles specified, centile reports the median (the 50th centile). With no variable list it reports every variable.
  2. Request specific centiles with a number list, for example centile price, centile(25 50 75).
  3. 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, normal and meansd change 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 the detail option. Without detail, summarize does not return it.
  • Weights and detail. summarize allows aweights, fweights and iweights, but iweights cannot be combined with detail. Use fweights or aweights with detail, or drop the weights.
  • Medians by group with summarize. Use the by prefix (bysort foreign: summarize price, detail) or switch to tabstat with by(). Note that by() inside tabstat is 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

Follow CASRAI

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

Ask CASRAI · free to try

Ask about How to Find the Median in Stata: summarize, tabstat, egen and centile

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

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 →

Ask CASRAI · Regulatory Radar

Research-admin question? Get an answer that links its sources.

An AI assistant specialized in research administration. Every answer links its sources to check before you act. 2 questions free, no account. $29/month after.

  • Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.
  • Every answer numbers its sources and links each one, so you can check the source yourself.