I am struggling with something that I know should be easy. My data is in long form, where I have multiple year observations for each respondent. I previously reshaped and merged data so that I have information about the respondent's children. I've pasted some of the data for one respondent below as an example. hhidpn is the id for the respondent. Each "kwork" variable corresponds to a child. That is, kwork1 is the first child's work status, kwork2 is the second child, etc. I'd like to create a variable that indicates "any child is not working" for each respondent for each year. Therefore, I need a way to indicate if any kwork* variables==0. I only went up to 8 in the table below, but I actually go up to kwork33, so it would be very helpful to write this as a loop rather than manually. Here, kwork4-kwork8 are missing because this respondent only has 3 children.
hhidpn | year | kwork1 | kwork2 | kwork3 | kwork4 | kwork5 | kwork6 | kwork7 | kwork8 |
10059020 | 1998 | 1.work p | 2.work f | 0.not wo | . | . | . | . | . |
10059020 | 2000 | 2.work f | 2.work f | 2.work f | . | . | . | . | . |
10059020 | 2002 | 2.work f | 2.work f | 2.work f | . | . | . | . | . |
10059020 | 2004 | 2.work f | 2.work f | 0.not wo | . | . | . | . | . |
10059020 | 2006 | 2.work f | 2.work f | 0.not wo | . | . | . | . | . |
10059020 | 2008 | 2.work f | 2.work f | 2.work f | . | . | . | . | . |
10059020 | 2010 | 2.work f | 2.work f | 0.not wo | . | . | . | . | . |
10059020 | 2012 | 2.work f | 2.work f | 0.not wo | . | . | . | . | . |
10059020 | 2014 | 2.work f | 2.work f | 0.not wo | . | . | . | . | . |
Code:
bysort hhidpn year: gen anyunemployedkid=. replace anyunemployedkid=1 if kwork*==0 replace anyunemployedkid=0 if kwork*==1|kwork*==2
I appreciate any help you can provide! Thank you!
0 Response to Looping over many variables to create new variable "if any" ==0
Post a Comment