// Creates w_nkids_dv - Number of children in household // INPUT FILE - w_indall.dta // OUTPUT FILE - w_nkids_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 iout015=(inrange(${wave}ivfio,20,24)) // age group to which applicable if age missing gen ${wave}chagemiss_ =((${wave}age_dv==. & iout015)) bysort ${wave}hidp: egen ${wave}chagemiss =sum(${wave}chagemiss_) bysort ${wave}hidp : egen ${wave}nchild_=sum(${wave}age_dv<=15) gen ${wave}nkids_dv=${wave}nchild_ +${wave}chagemiss lab var ${wave}nkids_dv "Number of children in household" label define ${wave}nkids_dv -9 "missing" -8 "inapplicable" 0 "none" lab val ${wave}nkids_dv ${wave}nkids_dv drop ${wave}chagemiss ${wave}chagemiss_ ${wave}nchild_ bysort ${wave}hidp: keep if _n==1 keep ${wave}hidp ${wave}nkids_dv save "${outpath}/${wave}nkids_dv", replace }