clear
set more off
set trace off
capture log close
//#delimit ;
//#delimit cr

qui { //global, log setting
global dir001 "C:\Users\Zaki Intan C\Documents\STATA files (DTA, DO, PDF)\DATA\DTA"
global dir002 "C:\Users\Zaki Intan C\Documents\STATA files (DTA, DO, PDF)\DATA\EXCEL"
global dir003 "C:\Users\Zaki Intan C\Documents\STATA files (DTA, DO, PDF)\PODES"
global dir004 "C:\Users\Zaki Intan C\Documents\STATA files (DTA, DO, PDF)\SUSENAS"
global dir005 "C:\Users\Zaki Intan C\Documents\STATA files (DTA, DO, PDF)\IFLS\IFLS5\hh14_all_dta"
global dir006 "C:\Users\Zaki Intan C\Documents\STATA files (DTA, DO, PDF)\IFLS\IFLS4\hh07_all_dta"
global dir007 "C:\Users\Zaki Intan C\Documents\STATA files (DTA, DO, PDF)\IFLS\IFLS3\hh00_all_dta"
global dir008 "C:\Users\Zaki Intan C\Documents\STATA files (DTA, DO, PDF)\IFLS\IFLS2\hh97dta"
global dir009 "C:\Users\Zaki Intan C\Documents\STATA files (DTA, DO, PDF)\IFLS\IFLS1\hh93dta"
global dir010 "C:\Users\Zaki Intan C\Documents\STATA files (DTA, DO, PDF)\IFLS\EAST-20210710T061528Z-001\EAST\data\hh"
global dir01 "C:\Users\Zaki Intan C\Documents\STATA files (DTA, DO, PDF)\LOGFILE"
global dir02 "C:\Users\Zaki Intan C\Documents\STATA files (DTA, DO, PDF)\RESULT"
global dir03 "C:\Users\Zaki Intan C\Documents\STATA files (DTA, DO, PDF)\TEMP"
global dir04 "C:\Users\Zaki Intan C\Documents\STATA files (DTA, DO, PDF)\FIGURES"
log using "$dir01/sekolahstata", text replace
}

qui { /*note pelatihan:
1. ifls: long format, susenas: wide format
2. commands for working w long format: reshape
3. each ifls books can have different total obs. that is why smtms merging results in unmatched obs
4. variabel utama biasanya dipakai sebagai data master ketika merging
5. u can just place ur cursor at a command line and run it :)
*/
}

/***************************************************************************
				ifls data cleaning & regression
***************************************************************************/
qui {
/*=================
*cleaning variabel
*=================

						///  master ///

//2007
u "$dir006/bk_ar1", clear
ta ar01i ar01a
keep if inlist(ar01a, 1,2,3,5,11)
keep if ar01i==1
*marstat, sex, age
cap drop marstat
recode  ar13(2=1 "Married") (min/max=0 "Unmarried"), gen(marstat)
recode ar07(1=1 "male")(3=0 "female"), gen(sex)
recode  ar09(999 998=.), gen(age)
*hhsize
gen helpvar = 1
bys hhid07: egen hhsize = total(helpvar)
//collapse (sum) helpvar, by(hhid07)
keep marstat age sex hhsize pid* hhid*
tempfile master07
save `master07', replace
//save "$dir001/sekolahstata_master07", replace

*2014
u "$dir005/bk_ar1.dta", clear
keep if inlist(ar01a, 1,2,3,5,11)
keep if  ar01i==1
*marstat, sex, age
cap drop marstat
recode  ar13(2=1 "Married") (min/max=0 "Unmarried"), gen(marstat)
recode ar07(1=1 "male")(3=0 "female"), gen(sex)
recode  ar09(999 998=.), gen(age)
*hhsize
gen helpvar = 1
bys hhid14: egen hhsize = total(helpvar)
//collapse (sum) helpvar, by(hhid14)
keep marstat age sex hhsize pid* hhid*
tempfile master14
save `master14', replace
//save "$dir001/sekolahstata_master14", replace
*contains: age, marstat, sex, age

						/// asuransi ///

*2007
u "$dir006\b3b_ak1.dta", clear
keep hhid* pid* ak02 aktype
drop if aktype == ""
reshape wide ak02, i(hhid07 pid07) j(aktype) str
for var ak02*: tab X
recode ak02A(1=1 "memiliki asuransi askes")(3 = 0 "tidak memiliki asuransi") (8=.), gen(asuransi_askes)
ta asuransi_askes
keep asuransi_askes hhid* pid*
tempfile asuransi07
save `asuransi07', replace
//save "$dir001/sekolahstata_asuransi07", replace

*2014
u "$dir005\b3b_ak1.dta", clear
duplicates report hhid14 pid14
keep hhid* pid* ak02 aktype
drop if aktype==""
reshape wide ak02 , i(hhid14 pid14) j( aktype ) str
recode ak02A(1=1 "memiliki asuransi askes")(3 =0 " Tidak memiliki asuransi") (8=.), gen(asuransi_askes)
keep asuransi_askes hhid* pid*
tempfile asuransi14
save `asuransi14', replace
//save "$dir001/sekolahstata_asuransi14", replace

						/// happy ///

*2007
u "$dir006/b3a_sw", clear
ta sw12
recode sw12(1 2=1 "happy")(3 4=0 "unhappy"), gen(happy)
keep happy hhid* pid*
tempfile happy07
save `happy07', replace
//save "$dir001/sekolahstata_happy07", replace

*2014
u "$dir005/b3a_sw", clear
ta sw12
recode sw12(1 2 = 1 "happy") (3 4 = 0 "unhappy"), gen(happy)
keep happy hhid* pid*
tempfile happy14
save `happy14', replace
//save "$dir001/sekolahstata_happy14", replace

						/// tanaman keluarga ///

*2007
use "$dir006\b2_ut1", clear
global  alfabet A B C D E F G H I J K L M N O P Q R S T U V W //himpunan
foreach y in $alfabet{
gen tanaman_`y'= regexm( ut07c, "`y'")
} //rujukan, membuat variabel tanaman a-w in one go
gen tanaman_A1= regexm( ut07c, "A1") //making dummy var for tanaman A1
gen tanaman_B1= regexm( ut07c, "B1")
replace tanaman_A=0 if tanaman_A1==1
keep tanaman* hhid*
tempfile datatanaman07
save `datatanaman07', replace

*2014
use "$dir005\b2_ut1", clear
global  alfabet A B C D E F G H I J K L M N O P Q R S T U V W
foreach y in $alfabet{
gen tanaman_`y'= regexm( ut07c, "`y'")
}
gen tanaman_A1= regexm( ut07c, "A1")
gen tanaman_B1= regexm( ut07c, "B1")
replace tanaman_A=0 if tanaman_A1==1
keep tanaman* hhid*
tempfile datatanaman14
save `datatanaman14', replace

*========================================
*merging (semua data individu, pakai 1:1)
*and logistic regression
*========================================

						/// merging and append ///

 foreach i in 14 07{
use `happy`i'', clear //karena variabel happy adalah variabel utama, maka dia dijadikan data master ketika merge
merge 1:1 hhid`i' pid`i' using `master`i''
drop if _merge ==2
drop _merge

merge 1:1 hhid`i' pid`i' using `asuransi`i''
drop if _merge ==2
drop _merge

merge m:1 hhid`i'  using `datatanaman`i''
drop if _merge ==2
drop _merge

cap drop pid
ren hhid`i' hhid
ren pid`i' pid
gen tahun="`i'"
save "$dir001\sekolahstata_data `i'", replace
}

use "$dir001\data 07", clear

*append data
append using "$dir001\data 14"

						/// regression ///

logistic happy asuransi_askes
outreg2 using "$dir002\sekolahstata_hasil.doc", replace ctitle(model 1)

logistic happy asuransi_askes age
outreg2 using "$dir002\sekolahstata_hasil.doc", append ctitle(model 2)

logistic happy asuransi_askes age sex
outreg2 using "$dir002\sekolahstata_hasil.doc", append ctitle(model 2)

logistic happy asuransi_askes age sex marstat
outreg2 using "$dir002\sekolahstata_hasil.doc", append ctitle(model 2)
*/
}



/***************************************************************************
					stunting (using zanthro)
***************************************************************************/
qui {
/*Variabel yg dibutuhkan untuk membuat agemonth dan gender
*ivwmthbk, ivwyrbk, ar08yr, ar08mth, ar07

use "$ifls5\bk_ar1.dta", clear
merge m:m hhid14 using "$ifls5\bk_time.dta"

*Variabel agemonth
gen agemonth = ((ivwyrbk-ar08yr)*12)+(ivwmthbk-ar08mth)
drop if agemonth < 0
drop if agemonth > 60

*Variabel Gender
recode ar07 (1=1 "Laki2") (3=0 "perempuan"), gen(gender)

keep pid* hhid* gender agemonth
tempfile genderagemonth
save `genderagemonth'

***************************************
*Variabel Tinggi badan
clear
use "$ifls5\bus_us.dta"
ren us04 heightanak
keep hhid* pid* heightanak
tempfile tinggianak
save `tinggianak'

**************************************
*Variabel Stunting

use `genderagemonth', clear
duplicates drop pidlink, force
merge 1:1 pidlink using `tinggianak'
keep if _m==3
drop _merge

egen zha14 = zanthro(heightanak,ha,WHO), xvar( agemonth ) gender( gender ) gencode(male=1, female=0) ageunit(month)
gen stunting = 0
replace stunting = 1 if zha14 < -2
save "$output\hasilstunting.dta", replace
*/
}



/***************************************************************************
							project akhir
***************************************************************************/

*sex, educyear
u "$dir005/bk_ar1.dta", clear
keep if inlist(ar01a, 1,2,3,5,11)
keep if  ar01i==1
recode ar07(1=1 "1:male")(3=0 "0:female"), gen(sex)
gen educyear=0 if ar16==1
{ //elementary school
replace educyear = 0 if ar16 == 2 & ar17 == 0
replace educyear = 1 if ar16 == 2 & ar17 == 1
replace educyear = 2 if ar16 == 2 & ar17 == 2
replace educyear = 3 if ar16 == 2 & ar17 == 3
replace educyear = 4 if ar16 == 2 & ar17 == 4
replace educyear = 5 if ar16 == 2 & ar17 == 5
replace educyear = 6 if ar16 == 2 & ar17 == 6
replace educyear = 6 if ar16 == 2 & ar17 == 7
}
{ //islamic elementary school
replace educyear = 0 if ar16 == 72 & ar17 == 0
replace educyear = 1 if ar16 == 72 & ar17 == 1
replace educyear = 2 if ar16 == 72 & ar17 == 2
replace educyear = 3 if ar16 == 72 & ar17 == 3
replace educyear = 4 if ar16 == 72 & ar17 == 4
replace educyear = 5 if ar16 == 72 & ar17 == 5
replace educyear = 6 if ar16 == 72 & ar17 == 6
replace educyear = 6 if ar16 == 72 & ar17 == 7
}
{ //paket a
replace educyear = 6 if ar16 == 11 & ar17 == 7
replace educyear = 6 if ar16 == 11 & ar17 > 7
replace educyear = 6 if ar16 == 11 & ar17 < 7
}
{ //junior high
replace educyear = 6 if ar16 == 3 & ar17 == 0
replace educyear = 7 if ar16 == 3 & ar17 == 1
replace educyear = 8 if ar16 == 3 & ar17 == 2
replace educyear = 9 if ar16 == 3 & ar17 == 3
replace educyear = 9 if ar16 == 3 & ar17 == 7
}
{ //vocational junior high
replace educyear = 6 if ar16 == 4 & ar17 == 0
replace educyear = 7 if ar16 == 4 & ar17 == 1
replace educyear = 8 if ar16 == 4 & ar17 == 2
replace educyear = 9 if ar16 == 4 & ar17 == 3
replace educyear = 9 if ar16 == 4 & ar17 == 7
}
{ //paket b
replace educyear = 9 if ar16 == 12 & ar17 == 7
replace educyear = 9 if ar16 == 12 & ar17 > 7
replace educyear = 9 if ar16 == 12 & ar17 < 7
}
{ //islamic junior high
replace educyear = 6 if ar16 == 73 & ar17 == 0
replace educyear = 7 if ar16 == 73 & ar17 == 1
replace educyear = 8 if ar16 == 73 & ar17 == 2
replace educyear = 9 if ar16 == 73 & ar17 == 3
replace educyear = 9 if ar16 == 73 & ar17 == 7
}
{ //high school
replace educyear = 9 if ar16 == 5 & ar17 == 0
replace educyear = 10 if ar16 == 5 & ar17 == 1
replace educyear = 11 if ar16 == 5 & ar17 == 2
replace educyear = 12 if ar16 == 5 & ar17 == 3
replace educyear = 12 if ar16 == 5 & ar17 == 7
}
{ //vocational high school
replace educyear = 9 if ar16 == 6 & ar17 == 0
replace educyear = 10 if ar16 == 6 & ar17 == 1
replace educyear = 11 if ar16 == 6 & ar17 == 2
replace educyear = 12 if ar16 == 6 & ar17 == 3
replace educyear = 12 if ar16 == 6 & ar17 == 7
}
{ //islamic high school
replace educyear = 9 if ar16 == 74 & ar17 == 0
replace educyear = 10 if ar16 == 74 & ar17 == 1
replace educyear = 11 if ar16 == 74 & ar17 == 2
replace educyear = 12 if ar16 == 74 & ar17 == 3
replace educyear = 12 if ar16 == 74 & ar17 == 7
}
{ //paket c
replace educyear = 12 if ar16 == 15 & ar17 == 7
replace educyear = 12 if ar16 == 15 & ar17 > 7
replace educyear = 12 if ar16 == 15 & ar17 < 7
}
{ //diploma
replace educyear = 12 if ar16 == 60 & ar17 == 0
replace educyear = 13 if ar16 == 60 & ar17 == 1
replace educyear = 14 if ar16 == 60 & ar17 == 2
replace educyear = 15 if ar16 == 60 & ar17 == 3
replace educyear = 14 if ar16 == 60 & ar17 == 7
}
{ //s1
replace educyear = 12 if ar16 == 61 & ar17 == 0
replace educyear = 13 if ar16 == 61 & ar17 == 1
replace educyear = 14 if ar16 == 61 & ar17 == 2
replace educyear = 15 if ar16 == 61 & ar17 == 3
replace educyear = 16 if ar16 == 61 & ar17 == 4
replace educyear = 16 if ar16 == 61 & ar17 == 5
replace educyear = 16 if ar16 == 61 & ar17 == 6
replace educyear = 16 if ar16 == 61 & ar17 == 7
}
{ //open university
replace educyear = 12 if ar16 == 13 & ar17 == 0
replace educyear = 13 if ar16 == 13 & ar17 == 1
replace educyear = 14 if ar16 == 13 & ar17 == 2
replace educyear = 15 if ar16 == 13 & ar17 == 3
replace educyear = 16 if ar16 == 13 & ar17 == 4
replace educyear = 16 if ar16 == 13 & ar17 == 5
replace educyear = 16 if ar16 == 13 & ar17 == 6
replace educyear = 16 if ar16 == 13 & ar17 == 7
}
{ //s2, s3
replace educyear = 16 if ar16 == 62
replace educyear = 16 if ar16 == 63
}
keep sex educyear pid* hhid*
sort hhid14 pid14
tempfile bkar1ifls5
save `bkar1ifls5', replace

*res
u "$dir005/bk_sc1.dta", clear
recode sc05 (2=0 "0:pedesaan")(1=1 "1:perkotaan"), gen(res)
keep res hhid*
sort hhid14
tempfile bksc1ifls5
save `bksc1ifls5', replace

*Marstat
u "$dir005/b3a_cov.dta", clear
recode marstat(1=0 "0:belum menikah")(4=1 "1:cerai")(2=2 "2:menikah")(3 5 6 = .), gen(Marstat)
keep Marstat hhid* pid*
sort hhid14 pid14
tempfile b3acovifls5
save `b3acovifls5', replace

*famsize
u "$dir005/b3a_tk1.dta", clear
gen helpvar = 1
bys hhid14: egen famsize = total(helpvar)
keep famsize hhid* pid*
sort hhid14 pid14
tempfile b3atk1ifls5
save `b3atk1ifls5', replace

*cesd10, depresi
u "$dir005/b3b_kp.dta", clear
reshape wide kp02, i(pidlink) j(kptype) string
foreach i in kp02A kp02B kp02C kp02D kp02F kp02G kp02I kp02J {
recode `i' (4=3) (3=2) (2=1) (1=0)
}
foreach i in kp02E kp02H {
recode `i' (1=3) (2=2) (3=1) (4=0)
}
egen cesd10 = rowtotal(kp02A kp02B kp02C kp02D kp02E kp02F kp02G kp02H kp02I kp02J)
g depresi = 1 if cesd10 >= 10 //based on typical cutoff
replace depresi = 0 if cesd10 <10
la define depresilab 0 "0:tidak depresi" 1 "1:depresi"
la values depresi depresilab
keep cesd10 depresi hhid* pid*
sort hhid14 pid14
tempfile b3bkpifls5
save `b3bkpifls5', replace

*religius
u "$dir005/b3a_tr.dta", clear
recode tr11(2 = 1 "1:religius")(3 4 =0 "0:tidak religius")(7=.), gen(religius)
keep religius hhid* pid*
sort hhid14 pid14
tempfile b3atrifls5
save `b3atrifls5', replace

*perhiasan
u "$dir005/b3a_hr1.dta", clear
recode hr01 (1=1 "1:memiliki")(3=0 "0:tidak memiliki")(8=.), g(perhiasan)
drop if hr1type != "J"
keep perhiasan hhid* pid*
sort hhid14 pid14
tempfile b3ahr1ifls5
save `b3ahr1ifls5', replace

*donasi
u "$dir005/b1_ks3.dta", clear
g donasi = ks08
drop if ks3type != "D"
keep donasi hhid*
sort hhid14
tempfile b1ks3ifls5
save `b1ks3ifls5', replace

*bahasa
u "$dir005/b3a_dl1", clear
gen bahasa= regexm( dl01a, "W")
//replace bahasa = 0 if !(strpos(dl01a), "W")
la define bahasalab 0 "0:bahasa lainnya"  1 "1:bahasa indonesia"
la values bahasa bahasalab
keep bahasa hhid* pid*
sort hhid14 pid14
tempfile b3adl1ifls5
save `b3adl1ifls5', replace

/***********************
		merging
************************/

u `b3bkpifls5', clear //depresi

/*merge hhid14 pid14 using `bkar1ifls5' `b3acovifls5' `b3atk1ifls5' `b3atrifls5' `b3ahr1ifls5' `b3adl1ifls5'
foreach x of varlist _merg* {
drop if `x' == 2
drop if `x' == 0
drop `x'
} //generates 11,362 observations
sort hhid14

merge hhid14 using `bksc1ifls5' `b1ks3ifls5'
foreach x of varlist _merg* {
drop if `x*' == 2
drop if `x' == 0
drop `x'
} //generates 16,885 observations */

merge 1:1 hhid14 pid14 using `bkar1ifls5', keep(1 3) //sex, educyear
drop _merge

merge 1:1 hhid14 pid14 using `b3acovifls5', keep(1 3) //Marstat
drop _merge

merge 1:1 hhid14 pid14 using `b3atk1ifls5', keep(1 3) //famsize
drop _merge

merge 1:1 hhid14 pid14 using `b3atrifls5', keep(1 3) //religius
drop _merge

merge 1:1 hhid14 pid14 using `b3adl1ifls5', keep(1 3) //bahasa
drop _merge

merge 1:1 hhid14 pid14 using `b3ahr1ifls5', keep(1 3) //perhiasan
drop _merge //generates 11,363 observations

merge m:1 hhid14 using `bksc1ifls5', keep(1 3) //res
drop _merge

merge m:1 hhid14 using `b1ks3ifls5', keep(1 3) //donasi
keep if _merge == 3
drop _merge //generates 31,371 observations */

drop pidprox
g lndonasi = ln(donasi)
save "$dir001/sekolahstataifls.dta", replace