clear
set more off
#delimit ;
/*****************************************************************************************************************;
**This dofile starts from expandedDHSGPS_mergedrecoded and generates mergedallrecoded;
other pre-generated datasets this dofile also uses:
DHSGPS_matchedwith2004
hh_simulatedmindistance_YYYY_grid
some more raw files
*****************************************************************************************************************/;

*****************************************************************************************************************;
*Paths;
*****************************************************************************************************************;

*access files in subdirectory generated by data creating dofiles;

local outdataset "dtafiles/womenallyears.dta";

local path=subinstr(c(pwd),"Documents","Dropbox\BF\data replication",.);
cap cd "`path'";
cap cd "C:\Users\gshastry\Dropbox\BF\data replication";

*****************************************************************************************************************;
*Main
*****************************************************************************************************************;

use "rawdta/DHS2007/BDIR51DT/BDIR51FL.DTA";

*HW: generating because survey year missing for some reason;
gen year=2007;

ren v001 dhsid07;
ren v002 hhnum;
label variable hhnum "household number within cluster";
label variable dhsid07 "cluster id 2007";

ren v106 educ;
label variable educ "highest level of education";
replace educ=. if educ==9;

ren v743a sayhealth;
label variable sayhealth "who has final say on woman's health care";
replace sayhealth=. if sayhealth==.;

ren v743b sayhhpurchases;
label variable sayhhpurchases "who has final say on large hh purchases";
replace sayhhpurchases=. if sayhhpurchases==9;

ren v743c saydailyneeds;
label variable saydailyneeds "who has final say on daily purchases";
replace saydailyneeds=. if saydailyneeds==9;

ren v743d sayvisits;
label variable sayvisits "who has final say on visits to relatives";
replace sayvisits=. if sayvisits==9;

ren v743e sayfood;
label variable sayfood "who has final say on food to be cooked";
replace sayfood=. if sayfood==9;

keep year hhnum dhsid07 v003 educ sayhealth sayhhpurchases saydailyneeds sayvisits sayfood;

tempfile data2007;
save `data2007', replace;

use "rawdta/DHS2004/BDIR4JDT/BDIR4JFL.DTA";

gen year=2004;

ren v001 dhsid04;
ren v002 hhnum;
label variable hhnum "household number within cluster";
label variable dhsid04 "cluster id 2004";

ren v106 educ;
label variable educ "highest level of education";
replace educ=. if educ==9;

ren v743a sayhealth;
label variable sayhealth "who has final say on woman's health care";
replace sayhealth=. if sayhealth==.;
ren v743b sayhhpurchases;
label variable sayhhpurchases "who has final say on large hh purchases";
replace sayhhpurchases=. if sayhhpurchases==9;
ren v743c saydailyneeds;
label variable saydailyneeds "who has final say on daily purchases";
replace saydailyneeds=. if saydailyneeds==9;
ren v743d sayvisits;
label variable sayvisits "who has final say on visits to relatives";
replace sayvisits=. if sayvisits==9;
ren v743e sayfood;
label variable sayfood "who has final say on food to be cooked";
replace sayfood=. if sayfood==9;

keep year dhsid04 hhnum v003 educ sayhealth sayhhpurchases saydailyneeds sayvisits sayfood;

tempfile data2004;
save `data2004', replace;

use "rawdta/DHS1999/BDIR41DT/BDIR41FL.DTA";

gen year=1999;
ren v001 dhsid99;
ren v002 hhnum;
label variable hhnum "household number within cluster";

ren v106 educ;
label variable educ "highest level of education";
replace educ=. if educ==9;

keep year v003 hhnum dhsid99 educ;

append using `data2004';
append using `data2007';

foreach var in health hhpurchases dailyneeds visits food {;
	gen autonomy_`var'=inlist(say`var',1,2,3) if say`var'!=9 & say`var'!=.;
	gen autonomy_`var'2=inlist(say`var',1)*3+inlist(say`var',2,3)*2+inlist(say`var',4,5)*1
		if say`var'!=9 & say`var'!=.;
	gen autonomy_`var'3=inlist(say`var',1)*1+inlist(say`var',2,3)*.5+inlist(say`var',4,5)*0
		if say`var'!=9 & say`var'!=.;
};

gen double motherid=7*1000000000+dhsid07*1000000+hhnum*1000+v003;
replace motherid=4*1000000000+dhsid04*1000000+hhnum*1000+v003 if year==2004;
replace motherid=9*1000000000+dhsid99*1000000+hhnum*1000+v003 if year==1999;

sort motherid;

keep autonomy_* motherid educ;

saveold "`outdataset'", replace;


