/*********************************************
			Template Do File Stata

**********************************************/


set more off /*Untuk mematikan fitur read more*/


/*************************
Manajemen Input Output
**************************/
cd "E:\Kerja\Farmasi Unpad\Data_IFLS\Latihan\Post test"
global ifls5 "E:\Kerja\Farmasi Unpad\Data_IFLS\IFLS\IFLS5\DATA_IFLS5HH"
global simpan "E:\Kerja\Farmasi Unpad\Data_IFLS\Latihan\Post test"
log using "$simpan\post_test.log", replace
/**********************
Buku K
**********************/
use "$ifls5/bk_ar1.dta", clear

*mendifine dataset
keep if inlist(ar01a, 1,2,3,5,11)
keep if  ar01i==1

*Sex
recode ar07(3=0 "Perempuan")(1=1 "Laki-laki"), gen(sex)

*Pendidikan

keep if ar16!=99
gen educyear=0 if ar16==1

*SD
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

*Madrasah
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 = 0 if ar16 == 11 & ar17 > 7
replace educyear = 0 if ar16 == 11 & ar17 < 7


*SMP
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

*SMP Kejuruan
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 = 6 if ar16 == 12 & ar17 > 7
replace educyear = 6 if ar16 == 12 & ar17 < 7

*MTs
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

*SMA
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

*SMK
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

*MA
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 = 9 if ar16 == 15 & ar17 > 7
replace educyear = 9 if ar16 == 15 & ar17 < 7

*Vokasi
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

*S1 UT
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
replace educyear = 16 if ar16 == 62 & ar17 == 0
replace educyear = 17 if ar16 == 62 & ar17 == 1
replace educyear = 18 if ar16 == 62 & ar17 == 2
replace educyear = 18 if ar16 == 62 & ar17 == 7

*S2
replace educyear = 18 if ar16 == 63 & ar17 == 0
replace educyear = 19 if ar16 == 63 & ar17 == 1
replace educyear = 20 if ar16 == 63 & ar17 == 2
replace educyear = 21 if ar16 == 63 & ar17 == 3
replace educyear = 22 if ar16 == 63 & ar17 == 4
replace educyear = 20 if ar16 == 63 & ar17 == 7

*jumlah anggota keluarga
gen bantu=1
bys hhid*: egen famsize=total(bantu)

*Status Pernikahan
recode  ar13(2=1 "Married")(min/max=0 "Unmarried"), gen(marstat)

keep marstat educyear famsize sex pid* hhid*
tempfile data_master14
save `data_master14', replace

*Tempat tinggal
use "$ifls5/bk_sc1.dta", clear
recode sc05(2=0 "Pedesaan")(1=1 "Perkotaan"), gen(res)
keep res hhid*
tempfile residence14
save `residence14', replace

*Depresi
use "$ifls5/b3b_kp.dta", clear
keep hhid* pid* kp02 kptype
reshape wide kp02 , i(hhid14 pid14) j(kptype ) str
foreach i in kp02A kp02B kp02C kp02D kp02F kp02G kp02I {
recode `i' (1=0) (2=1) (3=2) (4=3)
}
egen dep14 = rowmean(kp02A kp02B kp02C kp02D kp02F kp02G kp02I )
keep pid* hhid* dep14
tempfile depresi14
save `depresi14', replace

*Religiusitas
use "$ifls5/b3a_tr", clear
recode  tr11(1 2 3 =1 "Religius")(4 7=0 "Tidak Religius"), gen(religius14)
keep pid* hhid* religius14
tempfile religiusitas14
save `religiusitas14', replace

*Perhiasan
use "$ifls5/b2_hr1", clear
keep if hr01!=9
keep if hrtype == "J"
recode hr01(1=1 "Memiliki")(3=0 "Tidak Memiliki"), gen(jel14)
keep hhid* jel14
tempfile perhiasan14
save `perhiasan14', replace

*Bahasa Indonesia
use "$ifls5/b3a_dl1", 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 bahasa_`y'= regexm( dl01a, "`y'")
}
recode bahasa_W(1=1 "Bahasa Indonesia")(0=0 "Bahasa Lainnya"), gen(bahasa_ind14)
keep pid* hhid* bahasa_ind14
tempfile bahasa14
save `bahasa14', replace

*Donasi
use "$ifls5/b1_ks3", clear
keep if ks3type=="D"
keep hhid* ks08
rename ks08 donasi
tempfile donasi14
save `donasi14', replace

use `data_master14', clear
merge m:1 hhid14_9 using `residence14'
drop if _merge ==2
drop _merge

merge 1:1 hhid14_9 pid14 using `depresi14'
drop if _merge ==2
drop _merge

merge 1:1 hhid14_9 pid14 using `religiusitas14'
drop if _merge ==2
drop _merge

merge m:1 hhid14_9 using `perhiasan14'
drop if _merge ==2
drop _merge

merge m:1 hhid14_9 pid14 using `bahasa14'
drop if _merge ==2
drop _merge

merge m:1 hhid14_9 using `donasi14'
drop if _merge ==2
drop _merge

save "$simpan\data_posttest", replace
capture log close /*Untuk menutup log file*/