Hi everyone,
I hope you are all doing well and safe. I have a question about entering my command in a loop for 2 different variable.
I have a data set that investigates parents and children. Parents are the respondents and they are giving info about their children. So parents are located in rows and children info located in column section. I will demonstrate the data structure below:
Parents w6_ch_occupation_1 w6_ch_occupation_2 w6_ch_occupation_3 ...... this variable indicates the occupation of child number one to child number 20
1 5 2 1
2 1 3 .
3 2 4 3
My question is for each child variable: I have to recode occupation variable as employed and not employed and I would like to drop children who are born in 1999 or later because these children are not relevant for my research. My coding is as follows:
foreach var of varlist w6_ch_occupation_1- w6_ch_occupation_20 {
replace `var' = . if `var'==-2 | `var'==-1 | `var'==97
replace `var' = . if `w6_ch_yrbirth_1'>=1999
replace `var' = 1 if `var'==1 | `var'==2 | `var'==3
replace `var' = 0 if `var'==-9 | `var'==4 | `var'==5 | `var'==6 | `var'==7 | `var'==8 | `var'==9
}
however, Stata gives error for replace `var' = . if `w6_ch_yrbirth_1'>=1999. Do any of you know how to implement these 2 commands in a loop. Or is there an easy way to drop all children who are born in 1999 or older? Please keep in mind that i need to do this for all children related variables (e.g. marital status, education, gender etc.)
Thanks a lot for your help.
Kind regards,
Martin
I hope you are all doing well and safe. I have a question about entering my command in a loop for 2 different variable.
I have a data set that investigates parents and children. Parents are the respondents and they are giving info about their children. So parents are located in rows and children info located in column section. I will demonstrate the data structure below:
Parents w6_ch_occupation_1 w6_ch_occupation_2 w6_ch_occupation_3 ...... this variable indicates the occupation of child number one to child number 20
1 5 2 1
2 1 3 .
3 2 4 3
My question is for each child variable: I have to recode occupation variable as employed and not employed and I would like to drop children who are born in 1999 or later because these children are not relevant for my research. My coding is as follows:
foreach var of varlist w6_ch_occupation_1- w6_ch_occupation_20 {
replace `var' = . if `var'==-2 | `var'==-1 | `var'==97
replace `var' = . if `w6_ch_yrbirth_1'>=1999
replace `var' = 1 if `var'==1 | `var'==2 | `var'==3
replace `var' = 0 if `var'==-9 | `var'==4 | `var'==5 | `var'==6 | `var'==7 | `var'==8 | `var'==9
}
however, Stata gives error for replace `var' = . if `w6_ch_yrbirth_1'>=1999. Do any of you know how to implement these 2 commands in a loop. Or is there an easy way to drop all children who are born in 1999 or older? Please keep in mind that i need to do this for all children related variables (e.g. marital status, education, gender etc.)
Thanks a lot for your help.
Kind regards,
Martin
Comment