Command reference

tabstat — choose statistics for a summary table

Build a compact table containing the descriptive statistics you choose. Compare several variables, summarize groups, or switch the table so variables appear in columns.

Syntax

tabstat varlist [if condition] [in range] [weight] [, options]
by groupvars: tabstat varlist [, options]

List the numeric variables after tabstat. Add options after the comma. if and in restrict the observations before statistics are calculated. For grouped summaries, use either the by() option or a by groupvars: prefix.

Options

statistics(statlist)
Choose the statistics and their order. The default is count mean sd min max. Available names are count (or n), mean, median, sum, sd, var, range, min, max, iqr, skewness, kurtosis, and p1 p5 p10 p25 p50 p75 p90 p95 p99. Here range is maximum minus minimum and iqr is the 75th minus the 25th percentile.
by(groupvars)
Calculate the selected statistics separately for each combination of grouping values. This form also includes a Total summary across the selected observations. A by: prefix produces group summaries without that overall Total.
nototal
Omit the overall Total when using by().
columns(statistics | variables)
Choose the table orientation. The default, columns(statistics), puts statistics in columns and variables in rows. columns(variables) puts variables in columns. column() and abbreviated arguments such as col(var) are accepted.
format(%fmt)
Apply one numeric display format to every statistic, including counts. For example, format(%9.2f) displays two decimal places. Fixed, exponential, and general numeric formats are available; formatting changes the display, not the calculated values.
format
Use each variable's own numeric display format for its statistic cells.
save
Store the selected results in r(StatTotal). With by(), group matrices are stored as r(Stat1), r(Stat2), and so on, with group names in r(name1), r(name2), and so on. Matrix rows are statistics and columns are variables, regardless of the displayed orientation. nototal omits r(StatTotal) for grouped output.
[fw=weightvar] or [aw=weightvar]
Place frequency or analytic weights before the comma to calculate weighted statistics, including percentiles and measures of spread. Frequency weights must be nonnegative integers and count repeated observations; analytic weights retain the included-row count. Missing and zero weights are excluded. Importance and probability weights are not accepted.

Example

The example compares math, reading, and writing scores in 24 synthetic student records, then requests counts, means, and standard deviations for math within each race code.

clear
import delimited "data/student_scores.csv"
tabstat math read write, statistics(count mean sd min max)
by race: tabstat math, statistics(count mean sd)
Open example in browser

Sample data included. Opens in a new tab; click Run.

What to look for

The first table gives 24 observations for math and reading but 23 for writing because one writing score is missing. Each race group has six math scores. Their means range from 50.50 to about 64.17; the standard deviations describe variation within those groups.