egen examples: rowmean, rowtotal, and group
Calculate across variables within each row, handle missing components deliberately, and create identifiers for observed groups.
Choose the egen function for the task
- Average across columns in each row
egen result = rowmean(a b c); missing components are omitted when another value is available.- Add across columns in each row
egen result = rowtotal(a b c); missing components contribute zero in this tested workflow.- Repeat an overall mean or count
egen result = mean(x)oregen result = count(x).- Identify observed combinations
egen result = group(a b).
Calculate a row mean with egen rowmean()
egen score_mean = rowmean(read write math)
rowmean() calculates across the listed variables within each observation. It averages the available components, so a row can still receive a result when one component is missing.
Calculate a row total with egen rowtotal()
egen score_total = rowtotal(read write math)
rowtotal() adds the listed variables within each observation. In the tested workflow, a missing component contributes zero rather than making the whole total missing.
Create group identifiers with egen group()
egen race_female = group(race female)
group() assigns a numeric identifier to each observed combination of race and female. The identifiers are convenient keys, but their numeric order should not be given a substantive meaning.
Create repeated means and counts with egen
egen mean_math = mean(math)
egen count_math = count(math)
Both results are repeated on every observation. In this dataset, mean_math is 57.375 and count_math is 24 because all math values are present. Under a supported by workflow, the same functions calculate within each group.
Complete runnable egen example
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
The single do-file keeps rowmean(), rowtotal(), group(), mean(), count(), and std() together so their results can be compared directly.
Output from stats.camp
+----------------------------------------------------------------------------------------------+ | # id math mean_math count_math z_math score_mean score_total race_female | |----------------------------------------------------------------------------------------------| | 13 13 47 57.38 24 -1.36 47 141 5 | | 14 14 55 57.38 24 -0.31 55 165 6 | | 15 15 50 57.38 24 -0.96 49.50 99 5 | | 16 16 58 57.38 24 0.08 58 174 6 | +----------------------------------------------------------------------------------------------+
How rowmean() and rowtotal() handle missing values
Observation 15 has read=49, a missing write value, and math=50. In the current implementation, rowmean() averages the two available values and returns 49.50. rowtotal() treats the missing component as zero and returns 99.
That difference can materially affect a scale or index. Inspect rows with zero, one, or many available components and decide whether a minimum nonmissing-count rule is needed for your analysis.
When to use generate instead of egen
Use generate for an ordinary expression such as generate average = (a + b + c) / 3. That expression becomes missing when any component is missing. Use egen rowmean() when the intended rule is to average the available nonmissing components, or another egen function when the calculation operates across variables, observations, or groups.
Neither behavior is automatically correct for a scale. Decide the missing-data rule first, then choose the command that implements it.
Current limits and common mistakes
rowtotal(a b c)adds across columns within a row; it does not total one variable down the dataset.mean(math)creates an aggregate repeated for all observations; it is not a running mean.- The numeric identifiers from
group()are based on observed value combinations and should be inspected before use. egenis a large family; do not assume an unlisted official or community function is implemented.
See the current egen entry and Stata's egen manual for an external reference.
Related guides
Try rowmean, rowtotal, and group
Clone the example repository in stats.camp and open the exact do-file shown on this page. No stats.camp account is required.
Run the rowmean and rowtotal example