A simplified database of time use data is below. T1-T5 are time periods (e.g. T1 is 1pm - 2pm; T2 is 2pm-3pm, etc.). T1-T5 is 1 if the ACTIVITY is done during that time period, otherwise it is zero. Upon reviewing the data, I see that HHID 1 and HHID 2 OTHER activities (napping, snoozing) should be recoded to SLEEP - i.e. the data has been entered incorrectly.
What I want to do is conceptually easy (its essentially a vlookup), but I cannot figure out how to do it in Stata, within this db structure. It goes something like this:
for HHID==1 & HHID==2
foreach var T1-T5 { // NB: There are actually 60 time periods in the real db, so this needs to be a systematic iteration
replace var[OF SLEEP]=1 if var[OF OTHER]=1 // this is the vlookup-like part
and then replace var[OF OTHER]=0 // "clear" the OTHER data because its now accounted for in SLEEP
}
I could see this being a nested loop, except that the HHIDs that need to be revised will always be one-offs...
Help?
And thank you in advance.
HHID | ACTIVITY | UID | SPECIFY | T1 | T2 | T3 | T4 | T5 |
1 | SLEEP | SLEEP1 | 0 | 0 | 0 | 0 | 0 | |
1 | EAT | EAT1 | 0 | 1 | 0 | 0 | 1 | |
1 | WORK | WORK1 | 0 | 0 | 0 | 0 | 0 | |
1 | EXERCISE | EXERCISE1 | 0 | 1 | 0 | 0 | 0 | |
1 | PLAY | PLAY1 | 1 | 0 | 0 | 0 | 0 | |
1 | OTHER | OTHER1 | napping | 1 | 0 | 1 | 1 | 0 |
2 | SLEEP | SLEEP2 | 0 | 0 | 0 | 0 | 0 | |
2 | EAT | EAT2 | 1 | 0 | 0 | 0 | 0 | |
2 | WORK | WORK2 | 1 | 0 | 0 | 0 | 0 | |
2 | EXERCISE | EXERCISE2 | 1 | 1 | 1 | 0 | 1 | |
2 | PLAY | PLAY2 | 1 | 0 | 1 | 0 | 1 | |
2 | OTHER | OTHER2 | snoozing | 0 | 0 | 0 | 1 | 0 |
3 | SLEEP | SLEEP3 | 0 | 0 | 0 | 0 | 0 | |
3 | EAT | EAT3 | 0 | 0 | 1 | 1 | 0 | |
3 | WORK | WORK3 | 0 | 0 | 1 | 0 | 1 | |
3 | EXERCISE | EXERCISE3 | 1 | 1 | 0 | 0 | 0 | |
3 | PLAY | PLAY3 | 1 | 0 | 0 | 0 | 0 | |
3 | OTHER | OTHER3 | cooking | 1 | 0 | 0 | 0 | 0 |
for HHID==1 & HHID==2
foreach var T1-T5 { // NB: There are actually 60 time periods in the real db, so this needs to be a systematic iteration
replace var[OF SLEEP]=1 if var[OF OTHER]=1 // this is the vlookup-like part
and then replace var[OF OTHER]=0 // "clear" the OTHER data because its now accounted for in SLEEP
}
I could see this being a nested loop, except that the HHIDs that need to be revised will always be one-offs...
Help?
And thank you in advance.
Comment