Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 36 ยท Descriptive and Graphs

Summary Statistics with summarize

Get descriptive statistics in Stata with summarize, summarize detail, tabstat and mean: percentiles, skewness, weights, group summaries, stored r() results and confidence intervals.

After cleaning, the first question about every numeric variable is simple: what does it look like? Stata answers it with summarize for a quick overview, summarize, detail for the distribution, tabstat for tidy tables by group and mean for estimates with confidence intervals. This lesson explains what each number means and how to reuse the results in your own code.

summarize

STATA
sysuse auto, clear
summarize                       // all numeric variables
summarize price mpg weight      // some variables
summarize price if foreign == 1 // a subset

For each variable Stata prints:

Column Meaning
Obs Number of non-missing observations used
Mean Arithmetic mean
Std. dev. Sample standard deviation (divides by n - 1)
Min, Max Smallest and largest value

Always read Obs first. If it is smaller than the number of rows, the variable has missing values; a variable with Obs = 0 is almost certainly text. A standard deviation of 0 means a constant variable, and a min or max like -9 or 999999 often signals an unrecoded missing code.

summarize, detail

STATA
summarize price, detail

The detail option adds:

  • Percentiles (1%, 5%, 10%, 25%, 50%, 75%, 90%, 95%, 99%): the 50% percentile is the median.
  • The four smallest and largest values, useful for spotting outliers.
  • Variance, skewness and kurtosis.

Skewness is 0 for a symmetric distribution, positive when the right tail is longer (typical for prices and incomes), negative when the left tail is longer. Kurtosis is about 3 for a normal distribution; larger values mean heavier tails. A mean far above the median hints at right skew and outliers.

tabstat: choose your statistics

tabstat produces a compact table and can split by a variable:

STATA
tabstat price mpg weight, statistics(mean sd median min max n)
tabstat price, by(foreign) statistics(mean median sd n)
tabstat price mpg, by(foreign) statistics(mean) columns(statistics) format(%9.2f)
tabstat price, by(rep78) stat(p25 p50 p75 iqr)

The statistics list accepts mean, sd, variance, median, p1 to p99, iqr, min, max, range, sum, count (or n), skewness, kurtosis, semean, cv. Use , columns(variables) or columns(statistics) to choose which goes across. It is the best command for a descriptive table by group before you export it.

By groups

Several ways to get group summaries:

STATA
bysort foreign: summarize price           // full output per group
tabstat price, by(foreign) stat(mean sd n) // compact
summarize price if foreign == 0
table foreign, statistic(mean price) statistic(sd price) statistic(count price)   // Stata 17+ table command

Weights

Survey data use weights so that each respondent counts for the right number of people:

STATA
summarize income [aweight = popweight]   // analytic weights
summarize income [fweight = freq]        // frequency weights: each row stands for freq people

summarize allows aweight, fweight and iweight (probability weights, pweight, work with mean and regress but not with summarize). Choose by the meaning of the weight, not by the one that runs. For complex survey data, use the svy prefix with svyset first (svy: mean income), because standard errors depend on the design.

mean: estimates with confidence intervals

summarize gives descriptive statistics; mean gives an estimate of the population mean with standard error and confidence interval:

STATA
mean price
mean price, over(foreign)
mean price mpg, level(90)            // 90% confidence interval
proportion foreign                   // for a 0/1 or categorical variable

For a 95% interval Stata uses the t distribution with n - 1 degrees of freedom. Use mean when you report estimates; use summarize when you describe the sample in hand.

Using the stored results

After summarize, Stata stores the numbers in r(), so you can use them in code without copying by hand:

STATA
summarize price
display r(mean)                       // the mean
display r(sd)
generate price_z = (price - r(mean)) / r(sd)     // standardize
summarize price, detail
display r(p50)                        // the median
return list                           // see everything stored

Results are stored by the most recent command only. If you need them later, copy them into a local macro or scalar right away:

STATA
quietly summarize price
local pmean = r(mean)
scalar pmax = r(max)
display "Mean = " `pmean'

summarize, meanonly is a fast version that computes only mean, min, max, sum and N; it is great inside loops over large data.

Beyond summarize: counts and extremes

STATA
count                          // number of observations
count if price > 10000         // how many satisfy a condition
codebook price                 // a profile of the variable
inspect price                  // mini histogram, number of missing, zeros, negatives
list make price if price > 12000   // find the outliers by listing them
egen price_median = median(price)  // a statistic as a variable

inspect is a one-screen check for a variable: it shows negative, zero and positive counts, unique values, missing values and a tiny text histogram. It is very useful during cleaning.

Stand-in: summarize and detail

The Python model computes the same statistics as summarize, detail, using Stata's definition of percentiles (average of two values when n * p / 100 is a whole number) and population-moment skewness and kurtosis. It uses only the standard library.

Python Stand-in: summarize, detail

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Stand-in: tabstat by group

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The numbers in this model come from a made-up mini table, not from Stata's auto dataset. Run the real commands on auto in your own copy to see Stata's output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In summarize output, which column should you read first to detect missing values?

    Obs counts non-missing values; fewer than the dataset size means missing data.
  2. 2. A variable has mean well above its median. What does it suggest?

    High values pull the mean upward, so mean above median signals right skew.
  3. 3. How do you reuse the mean from summarize in later code?

    summarize stores results in r(); copy them to a macro or scalar if you need them later.
  4. 4. Which command is best for a compact table of statistics by group?

    tabstat with by() and statistics() gives a tidy table.

Summary

  • summarize reports N, mean, standard deviation, min and max; detail adds percentiles, skewness and kurtosis.
  • tabstat builds compact tables for chosen statistics and groups; mean gives estimates with confidence intervals.
  • Use the right weight type and svy: for survey designs.
  • Results of the last command live in r(); copy them to macros or scalars before the next command.
  • inspect, codebook and count if help find outliers and odd codes.

Next: Tables and Cross-tabulations