Hi,
I'm trying to set up a foreach loop in Stata to merge and append 4 waves of data. The problem is that every time I try to do this I get the r(198) invalid error message relating to the second variable in the use list. At first I thought it was a problem with the particular variable that was second but I've tried lots of different combinations and no matter which variable I have 2nd on the list, I get this error message for it.
The code from the .do file is below. Any help would be greatly appreciated.
foreach w in a b c d {
use `w'_hidp `w'_sex `w'_jbstat `w'_aidhrs `w'_jbterm1///
`w'_jbterm2 `w'_jbsemp `w'_jbsect `w'_jbsectpub `w'_jbhrs `w'_j2has `w'_ccare ///
`w'_howlng `w'_livewith `w'_employ `w'_hhtype_dv `w'_jbseg_dv `w'_netinc1 ///
`w'_qfhigh `w'_racel pidp `w'_mastat_dv ///
using "$dir/`w'_indresp", clear
gen wave = strpos("abcd","`w'")
rename `w'_* *
save `w'junk.dta, replace
}
foreach w in a b c {
append using `w'junk.dta
}
sort pidp wave
save abcd_long.dta, replace
Thanks
I'm trying to set up a foreach loop in Stata to merge and append 4 waves of data. The problem is that every time I try to do this I get the r(198) invalid error message relating to the second variable in the use list. At first I thought it was a problem with the particular variable that was second but I've tried lots of different combinations and no matter which variable I have 2nd on the list, I get this error message for it.
The code from the .do file is below. Any help would be greatly appreciated.
foreach w in a b c d {
use `w'_hidp `w'_sex `w'_jbstat `w'_aidhrs `w'_jbterm1///
`w'_jbterm2 `w'_jbsemp `w'_jbsect `w'_jbsectpub `w'_jbhrs `w'_j2has `w'_ccare ///
`w'_howlng `w'_livewith `w'_employ `w'_hhtype_dv `w'_jbseg_dv `w'_netinc1 ///
`w'_qfhigh `w'_racel pidp `w'_mastat_dv ///
using "$dir/`w'_indresp", clear
gen wave = strpos("abcd","`w'")
rename `w'_* *
save `w'junk.dta, replace
}
foreach w in a b c {
append using `w'junk.dta
}
sort pidp wave
save abcd_long.dta, replace
Thanks
Comment