Two distinct kinds of problems are in danger of being conflated here.
Neither requires any use of -foreach- or -forval- or explicit looping
over the data. The FAQ Martin cites and the help for -egen- are more
directly useful than any documentation on loops.
The two problems are
1. Within each group, is any observation of a particular type?
2. Within each group, for each observation, is any other observation of
a particular type?
Robin's wording starts off focusing on #2 but ends up asking about #1.
Much hinges on what household means exactly but if as I suppose it does
then exceptionally the two problems are the same, i.e. if anybody is
living in a three-generational household then they all are. And Kieran's
solution is fine. If you knew that there were no missing values then
bysort hh_id (hh3) : gen new_hh3 = hh3[_N]
is from Stata's point of view (but not necessarily the user's) even
simpler.
A more general approach -- assuming indicator variables -- is to count
first. The easiest way to count is to add indicators.
egen count = total(indicator), by(group)
gen count_everybodyselse = count - indicator
gen anybodyelse = everybodyelse > 0
gen anybody = count > 0
covers a fair fraction of the needed technique.
Nick
[email protected]
Martin Weiss
============
Robin may want to dig into Nick`s canonical reference for these
problems,
http://www.stata-journal.com/sjpdf.html?articlenum=pr0004
Also note that not every problem in connection with the "other members"
of a family is as easily solved as this one:
http://www.stata.com/support/faqs/data/members.html
Kieran McCaul
=============
Assuming that hh_3 is coded either 0 or 1:
clear
input hh_id person_id hh_3
4003 01 0
4003 02 1
4003 03 0
4004 01 0
4004 02 1
4004 03 1
4005 01 0
4005 02 0
4005 03 0
end
sort hh_id
by hh_id:egen new_hh_3 = max(hh_3)
list
+------------------------------------+
| hh_id person~d hh_3 new_hh_3 |
|------------------------------------|
1. | 4003 1 0 1 |
2. | 4003 2 1 1 |
3. | 4003 3 0 1 |
4. | 4004 1 0 1 |
5. | 4004 2 1 1 |
|------------------------------------|
6. | 4004 3 1 1 |
7. | 4005 1 0 0 |
8. | 4005 2 0 0 |
9. | 4005 3 0 0 |
+------------------------------------+
Robin Pleau
===========
I am using Stata 9. I have what seems like a simple coding
problem but can't figure it out (I'm a relatively new Stata user). I
believe the solution lies in the foreach/forvalues commands, but can't
seem to come up with a solution.
I need to recode a person-level variable based on whether other people
in the household have the same characteristic. Specifically, I want to
recode the variable hh_3 with the value of 1 (1=the person lives in a
three-generational household) if anyone in the household already has
hh_3=1. I want to loop through the whole dataset.
hh_id person_id hh_3
4003 01 0
4003 02 1
4003 03 0
4004 01 0
4004 02 1
4004 03 1
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/