Command reference

egen — calculate row summaries and group statistics

Create a variable with a named summary function. Use egen to average several columns within each observation, repeat a group statistic on its members, or assign identifiers to combinations of values.

Syntax

egen [type] newvar = function(arguments) [if condition] [in first/last] [, options]

newvar must be a new name. Functions such as mean(math) summarize one variable across observations; rowmean(read write math) works across several variables within each observation. The arguments here are variable names. Use generate for ordinary arithmetic expressions.

Functions and their options

rowmean(varlist)
Average the observed numeric values across the listed columns. Missing components are omitted from the numerator and denominator. The result is missing if all components are missing.
rowtotal(varlist)
Add the numeric values across columns, treating missing components as zero. An all-missing row produces zero. The optional missing modifier is not supported for this function here.
rowmin(), rowmax(), rowsd()
Calculate the smallest value, largest value, or sample standard deviation across numeric columns, omitting missing components. rowsd() needs at least two observed components.
rowmiss(), rownonmiss()
Count missing or observed components across the listed numeric variables. These counts can help you require enough completed items before using a row average.
rowfirst(), rowlast()
Return the first or last observed numeric value in the supplied variable order. These functions follow the order of columns in the argument, not the size of their values.
mean(x), total(x), count(x)
Repeat the mean, total, or count of nonmissing values on the selected observations. total() gives a complete total, not a running sum. count() counts observed values rather than all rows.
min(x), max(x), sd(x), median(x), iqr(x)
Repeat a summary of the observed values: minimum, maximum, sample standard deviation, median, or interquartile range. iqr() is the 75th percentile minus the 25th.
pctile(x), p(#)
Repeat a selected percentile. Set p(25) for the 25th percentile; the default is 50. Values from 0 through 100 are accepted.
std(x)
Create a standardized value by subtracting the mean and dividing by the sample standard deviation. Missing inputs and groups without positive variation produce missing standardized values.
mode(x)
Repeat the most frequent observed value. If multiple values tie for the highest frequency, the result is missing.
group(varlist), missing
Assign the same numeric ID to observations with the same combination of values. Combinations with a missing component have missing IDs by default; add missing to include them. IDs identify groups and are not meaningful scores.
tag(varlist)
Mark the first selected observation of each value combination with 1 and the others with 0. Observations outside an if or in selection also receive 0.
rank(x), unique | track | field
Rank observed values. The default ranks smallest first and averages tied ranks. unique breaks ties in row order; track gives ties their lowest ascending rank; field ranks largest first with tied values sharing their best rank. Choose one method.
seq()
Number selected observations consecutively, starting at 1. With groups, the sequence restarts in each group.
by(varlist)
Calculate group statistics separately for each combination of grouping values. Alternatively use a by: or bysort: prefix; do not combine a grouping prefix with by().

Example

The sample combines an overall mean and count, standardized math scores, row means and totals, and IDs for demographic groups. The listing includes a student with a missing writing score so you can compare the row functions.

clear
import delimited "data/student_scores.csv"
egen mean_math = mean(math)
egen count_math = count(math)
egen z_math = std(math)
egen score_mean = rowmean(read write math)
egen score_total = rowtotal(read write math)
egen race_female = group(race female)
list id math mean_math count_math z_math score_mean score_total race_female in 13/16
Open example in browser

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

What to look for

mean_math repeats 57.375 and count_math repeats 24. For student 15, reading is 49, math is 50, and writing is missing: score_mean is 49.5 and score_total is 99. The row mean averages two observed subjects. Students with the same race and female values share a race_female ID.