I support Friedrich Huebler's
good advice to consider -reshape-.
The idea of looping across variables
is not crazy; nevertheless -for- is a
very old-fashioned way to do it and
now officially deprecated.
-egen, rcount()- is a little old in the tooth
and predates -foreach- and -forval-.
gen age_is_15 = 0
forval i = 1/80 {
replace age_is_15 = age_is_15 + (age`i' == 15)
}
is likely to be faster than the -for- or using
-egen, rcount()-, and only the one new variable
is needed.
Nick
[email protected]
Richard Goldstein
> Yes, there is an egen rcount command, in the egenmore package
> (available via
> ssc) that allows you to count using such a condition
David K Evans wrote:
> > I have a household data set with the up-to 80 members of
> the household
> > listed horizontally, with each household being one
> observation. So, for
> > example, the ages of household members 1, 2, and 3 are in
> the following
> > variables: age_1 age_2 age_3 ...
> >
> > I want to create a variable that tells the number of people
> aged 15 in the
> > household.
> >
> > One (very slow) way to do this is to create a loop and go
> through each of
> > the 80 people, then use robs:
> >
> > for num 1/80: gen age15_X=1 if age_X==15
> > egen num_people_age15=robs(age15_*)
> >
> > The problem with this is that it's so inefficient. Has
> anyone written a
> > command like "egen =robs" but that counts observations that
> satisfy a
> > certain condition rather than merely non-missing observations?
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/