Introduction
If you are new to statistical software and need to find the middle value of a variable in Stata, knowing how to calculate median in stata is an essential skill. The median is a powerful measure of central tendency that tells you the value separating the higher half from the lower half of your data. Unlike the mean, the median is not pulled away by extreme outliers, making it especially useful for skewed distributions. In this article we will walk you through the entire process, from basic commands to more advanced tricks, and we will illustrate each step with real‑world examples. By the end of the guide you will be confident that you can compute medians quickly, interpret the results correctly, and avoid common pitfalls that trip up beginners.
Detailed Explanation
The median is the 50th percentile of a dataset. It is defined as the value that, when the observations are ordered from smallest to largest, leaves exactly half of the observations above it and half below it. When the number of observations is odd, the median is the middle observation; when the number is even, it is the average of the two middle observations. In Stata, the median can be calculated for a single variable, for multiple variables simultaneously, or even after applying filters such as by‑group or time‑series conditions.
Stata provides several built‑in commands that compute medians. There is also the mvmeta package (or meta command) for meta‑analysis where medians are needed across studies. The most straightforward is the summarize command with the detail option, which includes the median in its output. Another common approach is to use the egen function median(), which stores the median in a new variable for further analysis. Understanding the underlying logic of each method helps you choose the right tool for the job and ensures that you do not inadvertently overwrite existing data.
When you calculate a median in Stata, you must also consider how missing values are handled. By default, Stata excludes observations with missing values from the calculation, but you can change this behavior with the missing option. In practice, additionally, if you are working with grouped data (for example, by region or time period), you will need to combine the median command with a by prefix or the collapse command. Mastery of these nuances will save you time and prevent erroneous results later in your analysis.
Step‑by‑Step or Concept Breakdown
Below is a logical flow that takes you from a simple dataset to a more complex, grouped median calculation. Each step includes the exact Stata syntax you should type, followed by a brief explanation of what the command does.
1. Load or create a dataset
use auto.dta, clear // loads Stata’s built‑in automobile dataset
This command loads a sample dataset that many Stata tutorials use. If you have your own data file, replace auto.dta with the appropriate path Practical, not theoretical..
2. View the data structure
browse
Take a quick look at the variables you will be working with. For the purpose of this guide we will focus on the variable price, which contains the vehicle price in dollars Still holds up..
3. Compute the median with summarize
summarize price, detail
The detail option adds the minimum, maximum, quartiles, and the median to the output. The median will appear in the line labeled p50. This method is perfect for a quick glance at the central tendency without creating a new variable.
4. Store the median in a new variable using egen
egen median_price = median(price)
The egen command creates a new variable called median_price. Because median() is an aggregate function, every observation in the dataset will receive the same value – the median of price. This is useful when you need to compare each observation against the median later (for example, to compute dummies).
5. Create a binary indicator for “above median”
gen above_median = price > median_price
Now above_median will be 1 for cars priced above the median and 0 otherwise. You can label the values for clarity:
label define above_median 0 "At or below median" 1 "Above median"
label values above_median above_median
6. Compute medians by group with by
Suppose you want the median price for each origin (foreign vs. domestic).
bysort origin: summarize price, detail
Alternatively, you can store the group medians in a new variable:
bysort origin: egen group_median = median(price)
The bysort prefix tells Stata to apply the command separately for each level of origin.
7. Collapse to a single row per group (useful for reporting)
collapse (median) price = median_price, by(origin)
This creates a new dataset where each row corresponds to a country of origin and contains its median price. The resulting variable median_price holds the collapsed median values.
8. Handling missing values
If your dataset contains missing values in price, you can decide whether to include them:
summarize price, detail missing // includes missing as a valid observation
or
summarize price, detail // excludes missing by default
When using egen median(), missing values are ignored unless you add the missing option:
egen median_price = median(price), missing
Following these steps gives you a complete toolkit for calculating medians in Stata, whether you need a quick summary, a stored variable, or group‑specific medians.
Real Examples
Example 1: Quick summary of a health survey
A researcher collects a health survey dataset (health.dta) that includes the variable bmi. To see the median BMI of the entire sample, they run:
summarize bmi, detail
The output shows p50 = 27.3. This tells the researcher that half of the participants have a BMI above 27.3 and half below, giving a reliable sense of the central health risk in the population.
Example 2: Comparing income across regions
An economist wants to know the median household income for each of three regions (North, South, East). After loading the data (income.dta), they compute group medians:
bysort region: egen region_median_income = median(income)
They then create a flag indicating whether each household’s income is above its region’s median:
gen high_income = income > region_median_income
These flags can be used in further regression models to examine the impact of being “above‑median” on savings behavior.
Example 3: Median age in a longitudinal study
In a longitudinal study, the variable age
Example 3: Median age in a longitudinal study
In a longitudinal study, the variable age is measured annually. To find the median age at each wave, the researcher uses:
bysort wave: egen median_age = median(age)
This creates a variable median_age that reflects the median age for each wave. Alternatively, collapsing the data to report median ages per wave:
collapse (median) age, by(wave)
This produces a concise table showing how median age changes over time, useful for tracking demographic shifts in the study population.
Conclusion
Calculating medians in Stata is straightforward yet powerful, offering insights into central tendencies without the sensitivity to outliers that means can exhibit. Whether you need a quick summary with summarize, detail, a stored variable via egen, or group-specific medians using bysort, Stata provides flexible tools designed for your analytical needs. By mastering these commands, researchers can efficiently summarize data, compare groups, and uncover meaningful patterns in their datasets. Remember to handle missing values thoughtfully, as this ensures your results reflect the true distribution of your observations. With these techniques, medians become an accessible and integral part of exploratory data analysis and reporting.