// Creates w_nch1215_dv - Number of children aged 12-15 in household // INPUT FILE - w_indall.dta // OUTPUT FILE - w_nch1215_dv.dta // WRITE FILEPATH WHERE YOU HAVE DOWNLOADED THE DATA global in "" // WRITE FILEPATH WHERE YOU WOULD LIKE TO SAVE THE OUTPUT FILE global outpath "" // 1 is for BHPS, 2 is for UKHLS global cohort "2" foreach w in a b c d e f g h i j k l { // INCLUDE WAVE LETTERS FOR THE WAVES NEEDED global wave "`w'_" global num=strpos("abcdefghijklmnopqrstuvwxyz","`w'") global dta "" global waveno=$num global inpath "${in}/bhps_w${num}" if $cohort==2 { global waveno=$num+18 global inpath "${in}/ukhls" } use "${inpath}/${wave}indall${dta}", clear /*-------------ORIGINAL CODE--------------*/ replace ${wave}age_dv=. if ${wave}age_dv<0 gen ${wave}chagemiss_ =(${wave}age_dv==. & (inlist(${wave}ivfio,21,24,25,63,74))) bysort ${wave}hidp: egen ${wave}chagemiss =sum(${wave}chagemiss_) bysort ${wave}hidp: egen ${wave}nch1215_dv = sum(${wave}age_dv>=12 & ${wave}age_dv<=15) replace ${wave}nch1215_dv=-9 if (${wave}chagemiss>=1 & ${wave}chagemiss~=.) lab var ${wave}nch1215_dv "Number of children aged 12-15 in household" label define ${wave}nch1215_dv -9 "Missing or wild" 0 "None" lab val ${wave}nch1215_dv ${wave}nch1215_dv drop ${wave}chagemiss ${wave}chagemiss_ bysort ${wave}hidp: keep if _n==1 keep ${wave}hidp ${wave}nch1215_dv save "${outpath}/${wave}nch1215_dv", replace }