/****************************************************************
							Post Test
****************************************************************/

set more off
capture log close
log using logfile_penelitian.log, replace

/*****************************************
			Manajemen Input Output
********************************/
global ifls5 "C:\Users\Jihan\Downloads\Pelatihan IFLS\IFLS\DATA\2014\HH"
global hasil "C:\Users\Jihan\Downloads\Pelatihan IFLS\Hasil"
cd "C:\Users\Jihan\Downloads\Pelatihan IFLS\Hasil"

/*****************************************
			Buku k - Seksi AR
********************************/
use "$ifls5\bk_ar1", clear
keep if inlist(ar01a, 1,2,5,11)
keep if ar01i==1

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


*marstat (Status Perkawinan)
recode ar13 (6=.) (1=0 "Belum Menikah") (3=1 "Cerai") (4=1 "Cerai") (5=1 "Cerai") (2=2 "Menikah"), gen (marstat)


*Tingkat Pendidikan

*Tidak/Belum Pernah Sekolah
gen yos = 0 if ar16 == 1

*SD
replace yos = 0 if ar16 == 2 & ar17 == 0
replace yos = 1 if ar16 == 2 & ar17 == 1
replace yos = 2 if ar16 == 2 & ar17 == 2
replace yos = 3 if ar16 == 2 & ar17 == 3
replace yos = 4 if ar16 == 2 & ar17 == 4
replace yos = 5 if ar16 == 2 & ar17 == 5
replace yos = 6 if ar16 == 2 & ar17 == 6
replace yos = 6 if ar16 == 2 & ar17 == 7
replace yos = 6 if ar16 == 11

*MI
replace yos = 0 if ar16 == 72 & ar17 == 0
replace yos = 1 if ar16 == 72 & ar17 == 1
replace yos = 2 if ar16 == 72 & ar17 == 2
replace yos = 3 if ar16 == 72 & ar17 == 3
replace yos = 4 if ar16 == 72 & ar17 == 4
replace yos = 5 if ar16 == 72 & ar17 == 5
replace yos = 6 if ar16 == 72 & ar17 == 6
replace yos = 6 if ar16 == 72 & ar17 == 7

*SMP Umum
replace yos = 7 if ar16 == 3 & ar17 == 1
replace yos = 8 if ar16 == 3 & ar17 == 2
replace yos = 9 if ar16 == 3 & ar17 == 3
replace yos = 9 if ar16 == 3 & ar17 == 7
replace yos = 9 if ar16 == 12

*SMP Kejuruan
replace yos = 7 if ar16 == 4 & ar17 == 1
replace yos = 8 if ar16 == 4 & ar17 == 2
replace yos = 9 if ar16 == 4 & ar17 == 3
replace yos = 9 if ar16 == 4 & ar17 == 7

*SMU
replace yos = 10 if ar16 == 5 & ar17 == 1
replace yos = 11 if ar16 == 5 & ar17 == 2
replace yos = 12 if ar16 == 5 & ar17 == 3
replace yos = 12 if ar16 == 5 & ar17 == 7
replace yos = 12 if ar16 == 15

*SMK
replace yos = 10 if ar16 == 6 & ar17 == 1
replace yos = 11 if ar16 == 6 & ar17 == 2
replace yos = 12 if ar16 == 6 & ar17 == 3
replace yos = 12 if ar16 == 6 & ar17 == 7

*D1/D2/D3
replace yos = 13 if ar16 == 60 & ar17 == 1
replace yos = 14 if ar16 == 60 & ar17 == 2
replace yos = 15 if ar16 == 60 & ar17 == 3
replace yos = 15 if ar16 == 60 & ar17 == 7

*S1
replace yos = 13 if ar16 == 61 & ar17 == 1
replace yos = 14 if ar16 == 61 & ar17 == 2
replace yos = 15 if ar16 == 61 & ar17 == 3
replace yos = 16 if ar16 == 61 & ar17 == 4
replace yos = 16 if ar16 == 61 & ar17 == 5
replace yos = 16 if ar16 == 61 & ar17 == 6
replace yos = 16 if ar16 == 61 & ar17 == 7

*S2
replace yos = 17 if ar16 == 62 & ar17 == 1
replace yos = 18 if ar16 == 62 & ar17 == 2
replace yos = 19 if ar16 == 62 & ar17 == 3
replace yos = 19 if ar16 == 62 & ar17 == 7

*S3
replace yos = 19 if ar16 == 63 & ar17 == 1
replace yos = 20 if ar16 == 63 & ar17 == 2
replace yos = 21 if ar16 == 63 & ar17 == 3
replace yos = 22 if ar16 == 63 & ar17 == 7


*Level Education

gen educ_year = 1 if yos == 0 /*0 tahun*/
replace educ_year = 2 if yos >=1 & yos <=6 /*6 tahun*/
replace educ_year = 3 if yos >=7 & yos <=9 /*9 tahun*/
replace educ_year = 4 if yos >=10 & yos <=12 /*12 tahun*/
replace educ_year = 5 if yos >=13 & yos <=22 /*22 tahun*/

la val educ_year educ_year
lab def educ_year 1 "0 tahun" 2 "6 tahun" 3 "9 tahun" 4 "12 tahun" 5 "22 tahun"


*High Level Educ

gen high_educ = 1 if yos >= 10 & yos <= 22 /*10-22*/
replace high_educ = 0 if yos >= 1 & yos <= 9 /*0-9*/

la val high_educ high_educ
lab def high_educ 1 "10-22" 0 "0-9"

*Jumlah Anggota Keluarga
gen num=1
bys hhid14: egen famsize = sum(num)

keep pid* hhid* high_educ educ_year yos marstat sex famsize
save "$hasil\bkar1_dataset14", replace



/*****************************************
			Buku k - Seksi SC
*****************************************/
use "$ifls5/bk_sc1", clear
keep hhid* sc05

*Res (Tempat Tinggal)
recode sc05 (2=0 "Perdesaan") (1=1 "Perkotaan"), gen (Res)

save "$hasil/tempat_tinggal", replace


/********************************
			Depresi
********************************/
use "$ifls5/b3b_kp", clear
*cek duplikasi
duplicates report hhid14 pid14
*mempertahankan variabel
keep hhid* pid* kp02 kptype
*reshaping data
reshape wide kp02 , i(hhid14 pid14) j( kptype ) str
gen rata_rata = (kp02A + kp02B + kp02C + kp02D + kp02F + kp02G + kp02I)/7

*simpan data
save "$hasil/depresi", replace



/********************************
			Relijiusitas
********************************/
use "$ifls5/b3a_tr", clear
keep pid* hhid* tr11

*relijius (Relijiusitas)

recode tr11 (7=.)(3=0 "Tidak Relijius") (4=0 "Tidak Relijius")(1=1 "Relijius") (2=1 "Relijius"), gen (relijius)

save "$hasil/relijius", replace




/********************************
		Kepemilikan Perhiasan
********************************/
use "$ifls5/b3a_hr1", clear
keep hhid* pid* hr01 hr1type
keep if hr1type=="J"
recode hr01 (1=1 "Memiliki") (3=0 "Tidak Memiliki") (8=.), gen(perhiasan)

save "$hasil/kepemilikan_perhiasan", replace




/********************************
			Donasi
********************************/
use "$ifls5/b1_ks3", clear
keep if ks3type=="D"
keep hhid* ks08
rename ks08 Donasi

save "$hasil/Donasi", replace



/********************************
			Bahasa
********************************/
use "$ifls5/b3a_dl1", clear
gen bahasa_indo = regexm(dl01a, "W")
lab def bahasa_indo 0 "Tidak Menggunakan Bahasa Indonesia" 1 "Menggunakan Bahasa Indonesia"
lab val bahasa_indo bahasa_indo


save "$hasil/bahasa", replace


/****************************************************************
							MERGING DATA
****************************************************************/
use "$hasil\bkar1_dataset14", clear
merge 1:1 hhid14 pid14 using "$hasil/relijius"
drop _merge

merge m:1 hhid14 using "$hasil/Donasi"
drop _merge

merge 1:1 hhid14 pid14 using "$hasil/bahasa"
drop _merge

merge 1:1 hhid14 pid14 using "$hasil/kepemilikan_perhiasan"
drop _merge

merge 1:1 hhid14 pid14 using "$hasil/depresi"
drop _merge

merge m:1 hhid14 using "$hasil/tempat_tinggal"

log close