Hi, I have to create the average monthly exposure during the first trimester, second trimester, third trimester, exposure during 3 to 6 months, 6 to 9 months, and 1 to 9 months in utero.
I have written a loop that calculates temperature exposure during 1 to 3, 1 to 6 and 1 to 9 months and merges the files into one file. I am confused about how I can incorporate 3 to 6 months of exposure and 6 to 9 months of exposure.
foreach var in 3 6 9{
use hh ,clear
rename month month_child
rename year year_child
geonear ID LATNUM_hh LONGNUM_hh using "air_temp_all.dta", n(temp_ID LATNUM_temp LONGNUM_temp) miles genstub(ignore)
rename ignore temp_ID
gen time_var = ym(year_child, month_child)
expand `var'
sort ID
by ID: generate time_`var' = time_var + 1 - _n
format %tm time_`var'
gen month = month(dofm(time_`var'))
gen year = year(dofm(time_`var'))
egen UNID = group (ID month year)
save neardistance, replace
merge 1:1 temp_ID month year using air_temp_all
keep if _merge == 3
drop _merge
drop month
drop year
collapse (mean) temp`var'pre=temp precip`var'pre=precip , by(ID)
save hh_`var', replace
}
use hh_3, clear
foreach var in 6 9{
merge 1:1 ID using hh_`var'
drop _merge
save hh, replace
}
I have written a loop that calculates temperature exposure during 1 to 3, 1 to 6 and 1 to 9 months and merges the files into one file. I am confused about how I can incorporate 3 to 6 months of exposure and 6 to 9 months of exposure.
foreach var in 3 6 9{
use hh ,clear
rename month month_child
rename year year_child
geonear ID LATNUM_hh LONGNUM_hh using "air_temp_all.dta", n(temp_ID LATNUM_temp LONGNUM_temp) miles genstub(ignore)
rename ignore temp_ID
gen time_var = ym(year_child, month_child)
expand `var'
sort ID
by ID: generate time_`var' = time_var + 1 - _n
format %tm time_`var'
gen month = month(dofm(time_`var'))
gen year = year(dofm(time_`var'))
egen UNID = group (ID month year)
save neardistance, replace
merge 1:1 temp_ID month year using air_temp_all
keep if _merge == 3
drop _merge
drop month
drop year
collapse (mean) temp`var'pre=temp precip`var'pre=precip , by(ID)
save hh_`var', replace
}
use hh_3, clear
foreach var in 6 9{
merge 1:1 ID using hh_`var'
drop _merge
save hh, replace
}

Comment