clear
set more off
#delimit ;

**Helen's Paths;
if c(username)=="HelenW" {;
//PATH;
	local path "/Users/HelenW/Dropbox/BF";
//DEEFINE DATASET NAME;
	local dataset "/Users/HelenW/Dropbox/BF/dtafiles/expandedDHSGPS_mergedrecoded.dta";
};

*/;
if c(username)=="Kartini" | c(username)=="gshastry" {;
local name=c(username);
**Kartini's Paths;
//PATH;
local path "C:\Users\\`name'\Dropbox\BF";
//DEEFINE DATASET NAME;
local dataset "`path'/dtafiles/expandedDHSGPS_mergedrecoded.dta";
};

use "`path'/dtafiles/expandedDHSGPS_Mergedallyears.dta";

**renames additional variables**;
ren v627 idealnumboys;
ren v628 idealnumgirls;
ren v714 motherworks;
replace motherworks=. if motherworks==9;
ren v714a onleavefromjob;
ren v149 mothereduc;
ren v715 fathereducyrs;
ren v511 agefirstmarr;

ren v119 electricity;
ren v120 radio;
ren v121 television;
ren v122 refrigerator;
ren v123 bicycle;
ren v124 motorcycle;
ren v125 car;
ren v157 newspaper;
ren v130 religion;

ren h21 treatmentfordiarrhea;
ren v116 toiletfacility;
ren v012 age;
ren m14 numantenatal;
ren v312 contraception;
ren v024 region;

**HW: Renaming, recoding and generating variables for summary stats**;
ren v133 mothereducyrs;
replace mothereducyrs=. if mothereducyrs==99;
replace fathereducyrs=. if fathereduc==98|fathereduc==99;
replace electricity=. if electricity==7;

**GKS: missing or don't know shouldn't be assigned a religion;
gen muslim=1 if religion==1;
replace muslim=0 if muslim==. & !inlist(religion,99,.);

ren v025 urbanrural;
label variable urbanrural "cluster in urban or rural area";

/**
** GKS: Dropped this whole section because ageatdeath is created again below and
** that's the one we use later;
**HW: Child mortality variables**;
gen days=b6-100 if b6<200;
gen daystomonths=days/30;
**GKS: have to separate out conditions;
**gen months=b6-200 if 199<b6<300;
**replace months=daystomonths if months<0;
gen months=b6-200 if 199<b6 & b6<300;
replace months=daystomonths if daystomonths!=.;
gen years=b6-300 if b6>299;
gen yearstomonths=years*12;
**GKS: This next line only worked because missing is considered infinity;
**replace months=yearstomonths if months>99;
replace months=yearstomonths if yearstomonths!=.;
drop days daystomonths years yearstomonths;
ren months monthsatchilddeath;

**GKS: dropped this since variables are generated within table since lengths may change;
gen died12months=1 if monthsatchilddeath<12;
replace died12months=0 if died12months==.;
gen died24months=1 if monthsatchilddeath<24;
replace died24months=0 if died24months==.;
gen died60months=1 if monthsatchilddeath<60;
replace died60months=0 if died60months==.;
*/;

/*
**GKS: not sure we use this variable and we won't remember what it means anyway;
gen contaminated5=1 if pctcontaminated5>50;
replace contaminated5=0 if contaminated5==.;
replace contaminated5=. if as_walkable5==.;
*/;

gen childdied=1 if b5==0;
replace childdied=0 if childdied==.;

gen stillbreastfeeding=1 if durationbreast==95;
replace stillbreastfeeding=0 if stillbreastfeeding==. & !inlist(durationbreast,.,99,98);
replace stillbreastfeeding=. if childdied==1;

**HW: durationbreast includes codes for missing values and still breastfeeding- get rid of those but keep still breastfeeding info in another variable**;
gen durationbreast_orig=durationbreast;
replace durationbreast=. if durationbreast==95;
**GKS: if never breastfed, duration=0;
**replace durationbreast=. if durationbreast==94|durationbreast==99;
**replace durationbreast=. if durationbreast==98;
replace durationbreast=0 if durationbreast==94;
replace durationbreast=. if durationbreast==98|durationbreast==99;

replace monthsbreast=0 if monthsbreast==94;
replace monthsbreast=. if monthsbreast==99;
replace monthsbreast=. if monthsbreast==97;
replace monthsbreast=. if monthsbreast==98;

**HW: v221 is months from marriage to first birth- add to age at marriage to get age at first birth**;
replace v221=. if v221==996;
gen yrsmarrtobirth=v221/12;
gen agefirstbirth=agefirstmarr+yrsmarrtobirth;
drop yrsmarrtobirth;

#delimit ;
**HW: Age of child's death**;
gen ageatdeath=b6-100;
replace ageatdeath=ageatdeath/30; 
gen ageatdeathmonths=b6-200;
replace ageatdeath=ageatdeathmonths if ageatdeathmonths>0;
gen ageatdeathyears=b6-300;
replace ageatdeathyears=ageatdeathyears*12;
replace ageatdeath=ageatdeathyears if ageatdeathyears>0;
drop ageatdeathmonths;
drop ageatdeathyears;

**GKS: for some reason some ages are missing, so reset using month of survey and month of birth;
gen childage_old= childage ;

gen childpotentialage=(year-childbyear)*12+(v006-b1);
replace childpotentialage=(year+1-childbyear)*12+(v006-b1) if v007==0;

replace childage=childpotentialage if childage==.;

gen childagewithdied=childage;
replace childagewithdied=ageatdeath if childdied==1;

**HW: Use childpotentialage to find mother's age at birth of child;
gen childpageyrs=childpotentialage/12;
label variable childpageyrs "child potential age in years";
gen momagebirth=age-childpageyrs;
label variable momagebirth "mother's age at child's birth";

**HW: Cleaning variables for other liquids given to children;

replace plainwater=. if plainwater==8|plainwater==9;
replace babyformula=. if babyformula==8|babyformula==9;
replace otherliq=. if otherliq==8|otherliq==9;
replace sugarwater=. if sugarwater==8|sugarwater==9;
replace cowgoat=. if cowgoat==8|cowgoat==9;

**more plain water cleaning way down below;

/**GKS: Not sure whether we'll ever need the mean across previous and next births but the next interval may be an outcome;
**HW: Generating mean birth interval. b11 is preceding birth int, b12 is succeding birth int;
**If both have values, take mean, otherwise take whichever is there to find mean birth int**;
gen b11andb12=1 if b11!=.;
replace b11andb12=. if b12==.;
gen meanbirthintmonths=b11;
replace meanbirthintmonths=b12 if meanbirthintmonths==.;
gen avgb11b12=(b11+b12)/2 if b11andb12!=.;
replace meanbirthintmonths=avgb11b12 if avgb11b12!=.;
gen meanbirthinterval=meanbirthintmonths/12;
drop meanbirthintmonths;
drop avgb11b12;
drop b11andb12;
*/;

ren b12 birthinterval_next;
ren b11 birthinterval_previous;

gen birthinterval_next_imputed=birthinterval_next;
replace birthinterval_next_imputed=childage+10 if currentp==0 & birthinterval_next==.;
replace birthinterval_next_imputed=childage+(10-durationp) if currentp==1 & birthinterval_next==.;

**GKS: water source variable is coded differently in each year;
**GKS: missing should be still missing;

gen pipedwater=1 if watersource==10|watersource==11|watersource==12;
replace pipedwater=0 if pipedwater==. & !inlist(watersource,97,98,99,.);

gen tubewell=1 if year==1999 & inlist(watersource,20,21);
replace tubewell=1 if year==2004 & inlist(watersource,20,21,22,23,30);
replace tubewell=1 if year==2007 & inlist(watersource,20,21);
replace tubewell=0 if tubewell==. & !inlist(watersource,97,98,99,.);

gen surfacewater=1 if year==1999 & inlist(watersource,30,31,32);
replace surfacewater=1 if year==2004 & inlist(watersource,40,41,42);
replace surfacewater=1 if year==2007 & inlist(watersource,40,41,42,43);
replace surfacewater=0 if surfacewater==. & !inlist(watersource,97,98,99,.);

replace water_3d=. if water_3d==9;

/**GKS: Decided we didn't need this in the main dataset yet, merge in from hh data when necessary;
**HW: contaminated dummy for 2004 DHS testing**;
gen contaminated=1 if numarsenictest>=50;
replace contaminated=0 if contaminated==.;
replace contaminated=. if numarsenictest==.;
*/;

gen malechild=1 if b4==1;
replace malechild=0 if malechild==.;

**HW: generates continuous interaction term with birth year**;
tab childbyear, gen(childbyeardum);

**HW: Creates a unique identifier for clusters to use for fixed effects**;
gen dhsidyear=year*1000+dhsid07;
replace dhsidyear=year*1000+dhsid04 if year==2004;
replace dhsidyear=year*1000+dhsid99 if year==1999;

/*GKS: never used;
gen asbyear1994=as_walkable5*childbyeardum1;
gen asbyear1995=as_walkable5*childbyeardum2;
gen asbyear1996=as_walkable5*childbyeardum3;
gen asbyear1997=as_walkable5*childbyeardum4;
gen asbyear1998=as_walkable5*childbyeardum5;
gen asbyear1999=as_walkable5*childbyeardum6;
gen asbyear2000=as_walkable5*childbyeardum7;
gen asbyear2001=as_walkable5*childbyeardum8;
gen asbyear2002=as_walkable5*childbyeardum9;
gen asbyear2003=as_walkable5*childbyeardum10;
gen asbyear2004=as_walkable5*childbyeardum11;
gen asbyear2005=as_walkable5*childbyeardum12;
gen asbyear2006=as_walkable5*childbyeardum13;
gen asbyear2007=as_walkable5*childbyeardum14;

**HW: Generates interaction term with byear and arsenic dummy**;
gen contaminated1995= childbyeardum2*contaminated5;
gen contaminated1996= childbyeardum3*contaminated5;
gen contaminated1997= childbyeardum4*contaminated5;
gen contaminated1998= childbyeardum5*contaminated5;
gen contaminated1999= childbyeardum6*contaminated5;
gen contaminated2000= childbyeardum7*contaminated5;
gen contaminated2001= childbyeardum8*contaminated5;
gen contaminated2002= childbyeardum9*contaminated5;
gen contaminated2003= childbyeardum10*contaminated5;
gen contaminated2004= childbyeardum11*contaminated5;
gen contaminated2005= childbyeardum12*contaminated5;
gen contaminated2006= childbyeardum13*contaminated5;
gen contaminated2007= childbyeardum14*contaminated5;

**HW: Interaction terms with survey year**;
gen yeardum1999=1 if year==1999;
replace yeardum1999=0 if year!=1999;
gen yeardum2004=1 if year==2004;
replace yeardum2004=0 if year!=2004;
gen yeardum2007=1 if year==2007;
replace yeardum2007=0 if year!=2007;

gen arsenic5int1999=yeardum1999*as_walkable5;
gen arsenic5int2004=yeardum2004*as_walkable5;
gen arsenic5int2007=yeardum2007*as_walkable5;

gen contaminated5year1999=yeardum1999*contaminated5;
gen contaminated5year2004=yeardum2004*contaminated5;
gen contaminated5year2007=yeardum2007*contaminated5;
*/;

/**GKS: decided to leave these to make in HH-level data, not child level data;
gen redwell=1 if wellmarking==1;
replace redwell=0 if redwell==.;
replace redwell=. if wellmarking==.;

gen paintedwell=1 if wellmarking==1|wellmarking==2;
replace paintedwell=0 if paintedwell==.;
replace paintedwell=. if wellmarking==.;
*/;

**HW: cleaning health outcome variables**;
replace htasd=. if htasd==9999|htasd==9998;
gen htasdreal=htasd/100;
label variable htasdreal "z-score height for age";

replace wthtsd=. if wthtsd==9999|wthtsd==9998;
gen wthtsdreal=wthtsd/100;
label variable wthtsdreal "z-score weight for height";

/**GKS: never used;
**HW: Interaction terms with red painted well and painted well for 2004 data**;
gen redwell1999=redwell* childbyeardum6;
gen redwell2000=redwell* childbyeardum7;
gen redwell2001=redwell* childbyeardum8;
gen redwell2002=redwell* childbyeardum9;
gen redwell2003=redwell* childbyeardum10;
gen redwell2004=redwell* childbyeardum11;

gen paintedwell1999=paintedwell* childbyeardum6;
gen paintedwell2000=paintedwell* childbyeardum7;
gen paintedwell2001=paintedwell* childbyeardum8;
gen paintedwell2002=paintedwell* childbyeardum9;
gen paintedwell2003=paintedwell* childbyeardum10;
gen paintedwell2004=paintedwell* childbyeardum11;
*/;

/**GKS: renamed for ease of loops;
**Interaction terms of interes- pctcontaminated*byear**;
gen arsenicpct1995=pctcontaminated5*childbyeardum2;
gen arsenicpct1996=pctcontaminated5*childbyeardum3;
gen arsenicpct1997=pctcontaminated5*childbyeardum4;
gen arsenicpct1998=pctcontaminated5*childbyeardum5;
gen arsenicpct1999=pctcontaminated5*childbyeardum6;
gen arsenicpct2000=pctcontaminated5*childbyeardum7;
gen arsenicpct2001=pctcontaminated5*childbyeardum8;
gen arsenicpct2002=pctcontaminated5*childbyeardum9;
gen arsenicpct2003=pctcontaminated5*childbyeardum10;
gen arsenicpct2004=pctcontaminated5*childbyeardum11;
gen arsenicpct2005=pctcontaminated5*childbyeardum12;
gen arsenicpct2006=pctcontaminated5*childbyeardum13;
gen arsenicpct2007=pctcontaminated5*childbyeardum14;
*/;

ren childbyeardum2 byear1995;
ren childbyeardum3 byear1996;
ren childbyeardum4 byear1997;
ren childbyeardum5 byear1998;
ren childbyeardum6 byear1999;
ren childbyeardum7 byear2000;
ren childbyeardum8 byear2001;
ren childbyeardum9 byear2002;
ren childbyeardum10 byear2003;
ren childbyeardum11 byear2004;
ren childbyeardum12 byear2005;
ren childbyeardum13 byear2006;
ren childbyeardum14 byear2007;

**GKS: decided against this, because no wells is also no contaminated wells, but not sure...;
*replace numcontaminated5=. if numwalkable5==0;

forvalues i=1996(1)2007 {;
	local j=substr(string(`i'),-2,2);
	gen pctcontaminated5_b`j'=pctcontaminated5*byear`i';
	*gen fraction_mindistanceC_1m_G_b`j'=fraction_mindistanceC_1m_G*byear`i';
	*gen wfraction_mindistanceC_1m_G_b`j'=wfraction_mindistanceC_1m_G*byear`i';
	gen numcontaminated5_b`j'=numcontaminated5*byear`i';
	gen as_walkable5_b`j'=as_walkable5*byear`i';
};

/*GKS: renamed for ease of loops;
gen numbyear1995=numcontaminated5*byear1995;
gen numbyear1996=numcontaminated5*byear1996;
gen numbyear1997=numcontaminated5*byear1997;
gen numbyear1998=numcontaminated5*byear1998;
gen numbyear1999=numcontaminated5*byear1999;
gen numbyear2000=numcontaminated5*byear2000;
gen numbyear2001=numcontaminated5*byear2001;
gen numbyear2002=numcontaminated5*byear2002;
gen numbyear2003=numcontaminated5*byear2003;
gen numbyear2004=numcontaminated5*byear2004;
gen numbyear2005=numcontaminated5*byear2005;
gen numbyear2006=numcontaminated5*byear2006;
gen numbyear2007=numcontaminated5*byear2007;
*/;

/**GKS: renamed;
gen afterand2002=1 if childbyear==2002|childbyear==2003|childbyear==2004|childbyear==2005|childbyear==2006|childbyear==2007;
replace afterand2002=0 if childbyear==1994|childbyear==1995|childbyear==1996|childbyear==1997|childbyear==1998|childbyear==1999|childbyear==2000|childbyear==2001;
gen afterand2001=1 if childbyear==2001|childbyear==2002|childbyear==2003|childbyear==2004|childbyear==2005|childbyear==2006|childbyear==2007;
replace afterand2001=0 if childbyear==1994|childbyear==1995|childbyear==1996|childbyear==1997|childbyear==1998|childbyear==1999|childbyear==2000;
*/;

gen post2002=childbyear>=2002;
gen post2001=childbyear>=2001;
gen post2003=childbyear>=2003;

**GKS: dropped because very few observations relative to other years, don't want to use as omitted category;
drop if childbyear==1994;

foreach measure in "pctcontaminated5" "as_walkable5" "numcontaminated5" 
	{; 
	*"fraction_mindistanceC_1m_G" "wfraction_mindistanceC_1m_G" "fraction_mindistanceC_2m_G" "wfraction_mindistanceC_2m_G" {;

gen `measure'_2002=`measure'*post2002;
gen `measure'_2001=`measure'*post2001;
gen `measure'_2003=`measure'*post2003;

};

label variable post2002 "dummy born in 2002 or after";
label variable pctcontaminated5_2002 "dummy born in 2002 or after times pct. wells contaminated in 5 mi";
label variable byear1995 "dummy for birth year 1995";
label variable byear1996 "dummy for birth year 1996";
label variable byear1997 "dummy for birth year 1997";
label variable byear1998 "dummy for birth year 1998";
label variable byear1999 "dummy for birth year 1999";
label variable byear2000 "dummy for birth year 2000";
label variable byear2001 "dummy for birth year 2001";
label variable byear2002 "dummy for birth year 2002";
label variable byear2003 "dummy for birth year 2003";
label variable byear2004 "dummy for birth year 2004";
label variable byear2005 "dummy for birth year 2005";
label variable byear2006 "dummy for birth year 2006";
label variable byear2007 "dummy for birth year 2007";
label variable year "survey year";
label variable pipedwater "household water source is piped water";
label variable malechild "dummy for child is male";

**HW: Unique HH identifier**;
gen yr2=4 if year==2004;
replace yr2=7 if year==2007;
replace yr2=9 if year==1999;
gen hhid=yr2*1000000+dhsid07*1000+v002;
replace hhid=yr2*1000000+dhsid04*1000+v002 if year==2004;
replace hhid=yr2*1000000+dhsid99*1000+v002 if year==1999;
drop yr2;

**GKS: Not sure why this is here, but it doesn't do anything anyway;
**replace dhsid07=. if year!=2007;


**exclusive bf**;
**HW: I kept the code for the old exclusive breastfeeding measure in in case you ever wanted to look at it**;
*gen exclusivebf=1 if m39==0;
*replace exclusivebf=0 if exclusivebf==. & stillbreastfeeding==1;
*replace exclusivebf=1 if m37a!=1 & m37b!=1 & m37c!=1 & m37d!=1 & m37e!=1 
*& m37f!=1 & m37g!=1 & m37h!=1 & m37i!=1 & m37j!=1 & m37k!=1 & m37l!=1 & m37m!=1 & m37n!=1 
*& m37o!=1 & m37p!=1 & m37q!=1 & m37r!=1 & m37s!=1 & m37t!=1 & year==1999 & stillbreastfeeding==1;

** GKS: Fix 2004 plain water - otherliq variables since yesterday only asked if given in 7 days is a yes;
replace plainwater=0 if s449a==0 & year==2004;
replace sugarwater=0 if s449b==0 & year==2004;
replace babyformula=0 if s449c==0 & year==2004;
replace cowgoat=0 if s449d==0 & year==2004;
replace otherliq=0 if s449e==0 & year==2004;

gen grains=1 if s449h1==1;
replace grains=0 if s449h==0 & year==2004;
replace v414e=. if v414e==8|v414e==9;
replace grains=1 if v414e==1;
replace grains=0 if v414e==0;
label variable grains "child had grains in last 24 hours";

gen otherwaterbreast=1 if sugarwater==1|babyformula==1|cowgoat==1|otherliq==1;
replace otherwaterbreast=0 if sugarwater==0 & babyformula==0 & cowgoat==0 & otherliq==0;
label variable otherwaterbreast "had foods other than plain water and breastmilk";

gen anywater=1 if plainwater==1|sugarwater==1|babyformula==1;
replace anywater=0 if plainwater==0 & sugarwater==0 & babyformula==0;
label variable anywater "had water or foods made with water";


/** GKS: problem with this because plainwater sometimes missing and this makes it exclusive, also don't know why stillbreastfeeding instead of current;
** also if not still breastfeeding, not exclusively breastfeeding either, so should get a 0?;
** br exclusivebf* plainwater  babyformula otherliq  sugarwater  cowgoat year currentbreas stillbrea durationb* childage;
** to see the problems;
** note to self: use stillbf instead of current because current is for the woman, still is for the child;
gen exclusivebf_old=1 if plainwater!=1 & babyformula!=1 & otherliq!=1 & sugarwater!=1 & cowgoat!=1 & stillbreastfeeding==1 ;
replace exclusivebf_old=0 if exclusivebf_old==. & stillbreastfeeding==1;
*/;

gen exclusivebf=1 if plainwater==0 & babyformula==0 & otherliq==0 & sugarwater==0 & cowgoat==0 & stillbreastfeeding==1 ;
replace exclusivebf=0 if exclusivebf==. & stillbreastfeeding==1;
replace exclusivebf=. if plainwater==. | babyformula==. | otherliq==. | sugarwater==. | cowgoat==.;


gen exclusivebf2=exclusivebf;
replace exclusivebf2=0 if stillbreastfeeding==0;

gen exclusivebf_water=1 if babyformula==0 & otherliq==0 & sugarwater==0 & cowgoat==0 & stillbreastfeeding==1;
replace exclusivebf_water=0 if exclusivebf_water==. & stillbreastfeeding==1;
replace exclusivebf_water=. if plainwater==. | babyformula==. | otherliq==. | sugarwater==. | cowgoat==.;
gen exclusivebf_water2=exclusivebf_water;
replace exclusivebf_water2=0 if stillbreastfeeding==0;

foreach var of varlist exclusive* {;
	replace `var'=. if childdied==1;
};

label variable exclusivebf "child is exclusively breastfeeding";
label variable stillbreastfeeding "child is currently breastfeeding";
label variable currentbreastfeed "mother is currently breastfeeding some child";

/**GKS: created above with new names;
gen afterand2002avgas=afterand2002*as_walkable5;
gen afterand2002numwells=afterand2002*numcontaminated5;
replace afterand2002numwells=. if as_walkable5==.; ** decided against, leave 0;
*/;
label variable as_walkable5_2002 "interaction of post-campaign and avg As in 5 mi";
label variable numcontaminated5_2002 "interaction of post-campaign and number of contaminated wells in 5 mi";

/**GKS: never used;
gen contaminatedmedian=1 if pctcontaminated5>=11.11111;
replace contaminatedmedian=0 if pctcontaminated5<11.11111;
label variable contaminatedmedian "dummy for above median pct contaminated in 5 mi radius";
*/;

gen diarrhea_old=diarrhea;
replace diarrhea=. if inlist(diarrhea,8,9);
replace diarrhea=1 if diarrhea==2;

gen urban=1 if urbanrural==1;
label variable urban "dummy variable for in an urban area";
replace urban=0 if urbanrural==2;

label variable childage "age of child in months";
label variable childagewithdied "age of child in months with age at death if child died";
label variable childdied "dummy variable for child died";
gen hhweight=v005/1000000;
label variable hhweight "household weight- v005 divided by 1000000";

label variable age "mother's current age";
label variable mothereducyrs "mother's education in years";
label variable muslim "dummy for respondent is muslim";
label variable motherworks "mother currently working outside the home";
label variable agefirstmarr "mother's age at first marriage";
label variable agefirstbirth "mother's age at first birth";
label variable birthinterval_next "interval between this child and next";
label variable birthinterval_previous "interval between this child and previous";
label variable pipedwater "dummy for household's primary water source is piped water";
label variable tubewell "dummy for household's primary water source is tubewell";
label variable surfacewater "dummy for household's primary water source is surface water";
*label variable contaminated "2004 DHS household using water at or over As contamination limit";

forvalues i=1996(1)2007 {;
	local j=substr(string(`i'),-2,2);
	label variable pctcontaminated5_b`j' "pct of wells contaminated in 5 mi multiplied by given birth year";
	label variable as_walkable5_b`j' "average arsenic level in 5 mi multiplied by given birth year";
	label variable numcontaminated5_b`j' "number of contaminated wells in 5 mi multiplied by given birth year";
};

gen bcgvacc=1 if h2==1|h2==2|h2==3;
replace bcgvacc=0 if h2==0;
replace bcgvacc=. if h2==9;
label variable bcgvacc "Child received BCG vaccine";

gen dpt1vacc=1 if h3==1|h3==2|h3==3;
replace dpt1vacc=0 if h3==0;
replace dpt1vacc=. if h3==8 | h3==9;
label variable dpt1vacc "Child received DPT1 vaccine";

gen polio1vacc=1 if h4==1|h4==2|h4==3;
replace polio1vacc=0 if h4==0;
replace polio1vacc=. if h4==8 | h4==9;
label variable polio1vacc "Child received Polio 1 vaccine";

gen dpt2vacc=1 if h5==1|h5==2|h5==3;
replace dpt2vacc=0 if h5==0;
replace dpt2vacc=. if h5==8 | h5==9;
label variable dpt2vacc "Child received DPT2 vaccine";

gen polio2vacc=1 if h6==1|h6==2|h6==3;
replace polio2vacc=0 if h6==0;
replace polio2vacc=. if h6==8 | h6==9;
label variable polio2vacc "Child received Polio 2 vaccine";

gen dpt3vacc=1 if h7==1|h7==2|h7==3;
replace dpt3vacc=0 if h7==0;
replace dpt3vacc=. if h7==8 | h7==9;
label variable dpt3vacc "Child received DPT 3 vaccine";

gen polio3vacc=1 if h8==1|h8==2|h8==3;
replace polio3vacc=0 if h8==0;
replace polio3vacc=. if h8==8 | h8==9;
label variable polio3vacc "Child received Polio 3 vaccine";

gen measlesvacc=1 if h9==1|h9==2|h9==3;
replace measlesvacc=0 if h9==0;
replace measlesvacc=. if h9==8 | h9==9;
label variable measlesvacc "Child received measles vaccine";

gen polio0vacc=1 if h0==1|h0==2|h0==3;
replace polio0vacc=0 if h0==0;
replace polio0vacc=. if h0==8 | h0==9;
label variable polio0vacc "Child received Polio 0 vaccine at birth";

**HW: Note that this variable is ONLY for mothers who work outside the home, otherwise shows as missing;
gen seasonalemp=1 if v732==2;
replace seasonalemp=0 if v732==1;
replace seasonalemp=0 if v732==3;
replace seasonalemp=0 if v731==0;
label variable seasonalemp "mother works outside home seasonally";

gen seascurrent=seasonalemp*motherworks;
label variable seascurrent "mother currently works times mother works seasonally";

** HW: note that I didn't use the polio0 vaccine for the proportion measures
** becuase information on it wasn't gathered in every year;
gen anyvacc=1 if bcgvacc==1|dpt1vacc==1|polio1vacc==1|dpt2vacc==1|polio2vacc==1
|dpt3vacc==1|polio3vacc==1|measlesvacc==1|polio0vacc==1;
replace anyvacc=0 if bcgvacc==0 & dpt1vacc==0 & polio1vacc==0 & dpt2vacc==0 
& polio2vacc==0 & dpt3vacc==0 & polio3vacc==0 & measlesvacc==0 & polio0vacc==0;
label variable anyvacc "child received at least one vaccine from list";

gen propvacc= (bcgvacc+dpt1vacc+polio1vacc+dpt2vacc+polio2vacc+dpt3vacc+polio3vacc+
measlesvacc)/8;
label variable propvacc "proportion of 8 vaccines from list child has received";


**HW: Mother health outcome variables;
#delimit ;
gen momhtaperc=v439;
replace momhtaperc=. if momhtaperc==9998|momhtaperc==9999;
replace momhtaperc=momhtaperc/100;
label variable momhtaperc "Mother's height for age percentile";

gen momhtasd=v440;
replace momhtasd=. if momhtasd==9998|momhtasd==9999;
replace momhtasd=momhtasd/100;
label variable momhtasd "Mother's height for age z-score";


gen momwthtsd=v444a;
replace momwthtsd=. if momwthtsd==9998|momwthtsd==9999;
replace momwthtsd=momwthtsd/100;
label variable momwthtsd "Mother's weight for height z-score";

**HW: Note BMI not adjusted for pregnancy (it doesn't specify for the other variables in the description file);
gen mombmi=v445;
replace mombmi=. if mombmi==9998|mombmi==9999;
replace mombmi=mombmi/100;
label variable mombmi "Mother's BMI not adjusted for pregnancy";

gen exclusivebf6=exclusivebf if childagewithdied<6 & exclusivebf!=.;
gen exclusivebf614=exclusivebf if childagewithdied>=6 & childagewithdied<=14 & exclusivebf!=.;
gen exclusivebf12=exclusivebf if childagewithdied>12 & exclusivebf!=.;
gen monthsbf6=1 if monthsbreast>=6 & childagewithdied>=6 & monthsbreast!=.;
replace monthsbf6=0 if monthsbreast<6 & childagewithdied>=6 & monthsbreast!=.;
gen monthsbf12=1 if monthsbreast>=12 & childagewithdied>=12 & monthsbreast!=.;
replace monthsbf12=0 if monthsbreast<12 & childagewithdied>=12 & monthsbreast!=.;
gen monthsbf18=1 if monthsbreast>=18 & childagewithdied>=18 & monthsbreast!=. ;
replace monthsbf18=0 if monthsbreast<18 & childagewithdied>=18 & monthsbreast!=. ;
gen monthsbf24=1 if monthsbreast>=24 & childagewithdied>=24 & monthsbreast!=.;
replace monthsbf24=0 if monthsbreast<24 & childagewithdied>=24 & monthsbreast!=.;
gen monthsbf36=1 if monthsbreast>=36 & childagewithdied>=36 & monthsbreast!=. ;
replace monthsbf36=0 if monthsbreast<36 & childagewithdied>=36 & monthsbreast!=. ;

gen lnmonthsbf=ln(monthsbreast+1);

save "`dataset'", replace;




