Hi STATA-Peeps,
I've got 10 waves of data from a panel. Each contains a series of health condition variables [hcondn1....hcondn5...], but they all contain a different number of variables in this series (ranging from hcondn5 to hcondn13).
I can nest loops within loops, but can someone please help me with the code for only running my foreach loop the right amount of times depending on the wave number please?
Here is my foreach loop code so far:
foreach w in b c d e f g h i j k {
// find the wave number using "String Position" count (strpos)
local wave_nr=strpos(" bcdefghijk","`w'") // Counts to string posn _234567891011
local varcount = 0
foreach hc of varlist `w'_hcondn*{
varcount =+1
}
/* The line below stops the code at the first wave that doesn't have 8 "hcondn" variables in it */
use id `w'_hcondno1 `w'_hcondno2 `w'_hcondno3 `w'_hcondno4 `w'_hcondno5 `w'_hcondno6 ///
`w'_hcondno7 `w'_hcondno8 using "$Originals/`w'_indresp", clear
rename `w'_* *
gen wave=`wave_nr'
save temp`w', replace
Thanks for your help. I have tried a bit, but not quite integrated the two loops quite right.
Cheers!
SCE
I've got 10 waves of data from a panel. Each contains a series of health condition variables [hcondn1....hcondn5...], but they all contain a different number of variables in this series (ranging from hcondn5 to hcondn13).
I can nest loops within loops, but can someone please help me with the code for only running my foreach loop the right amount of times depending on the wave number please?
Here is my foreach loop code so far:
foreach w in b c d e f g h i j k {
// find the wave number using "String Position" count (strpos)
local wave_nr=strpos(" bcdefghijk","`w'") // Counts to string posn _234567891011
local varcount = 0
foreach hc of varlist `w'_hcondn*{
varcount =+1
}
/* The line below stops the code at the first wave that doesn't have 8 "hcondn" variables in it */
use id `w'_hcondno1 `w'_hcondno2 `w'_hcondno3 `w'_hcondno4 `w'_hcondno5 `w'_hcondno6 ///
`w'_hcondno7 `w'_hcondno8 using "$Originals/`w'_indresp", clear
rename `w'_* *
gen wave=`wave_nr'
save temp`w', replace
Thanks for your help. I have tried a bit, but not quite integrated the two loops quite right.
Cheers!
SCE
Comment