help for collapse2}

Augmented version of collapse to make dataset of means, medians, etc.

collapse2 clist [weight] [if exp] [in range] [, by(varlist) cw fast]

where clist is either

[(stat)] varlist [ [(stat)] ... ] [(stat)] target_var=varname [target_var=varname ...] [ [(stat)] ...]

or any combination of the varlist or target_var forms, and stat is one of

first first last last firstnm first non-missing lastnm last non-missing

sd standard deviations sum sums rawsum sums ignoring optionally specified weight

count number of nonmissing observations max maximums min minimums

median medians p1 1st percentile p2 2nd percentile ... 3rd -- 49th percentiles p50 50th percentile (same as median) ... 51st -- 97th percentiles p98 98th percentile p99 99th percentile iqr interquartile range

If stat is not specified, mean is assumed.

aweights, fweights, pweights, and iweights are allowed; see help weights and see note concerning weights below. Statistic sd (standard deviation) is not allowed with pweights.

varlist and varname in clist may contain time-series operators; see help varlist.


collapse2 is an extension of Stata's built-in collapse command, which converts the data in memory into a dataset of means, sums, medians, etc. It differs only in offering four additional "aggregation" operators: first, last, firstnm, and lastnm, described above. These are useful, for example, if you want to create a cross-section data set from annual data, with initial population as a variable. You must first tsset your data to use these additional operators.

For most details on the command, see help collapse.


. collapse2 (first) startpop=pop (last) endpop=pop, by(country)

. collapse2 (firstnm) startknownpop=pop (lastnm) endknownpop=pop, by(country)

* Extract first available observation for pop and year it is for. . gen popdatastart = year if pop < . . collapse2 (firstnm) popdatastart startpop=pop, by(country)


David Roodman Center for Global Development Washington, DC droodman@cgdev.org

Also see

Manual: [R] collapse

Online: help for collapse, contract, egen, statsby, summarize, tabdisp, table