/*******************************************************************
							AWALAN
******************************************************************/

clear
set more off
capture log close

/*******************************************************************
						MANAJEMEN INPUT-OUTPUT
******************************************************************/


global ifls5 "C:\qakhoiry\SEKOLAH STATA - IFLS\IFLS 5\IFLS 5 5HHH"
global hasil "C:\qakhoiry\SEKOLAH STATA - IFLS\LATIHAN\3. HASIL"
global log "C:\qakhoiry\SEKOLAH STATA - IFLS\LATIHAN\2. DATA"
cd "C:\qakhoiry\SEKOLAH STATA - IFLS\LATIHAN\2. DATA"
log using "$log\lampiran.log", replace

/*******************************************************************
					BUKU K SEKSI AR (VARIABEL JENIS KELAMIN,
						SIZE FAMILY, YEAR OF SCHOOL,
							MARITAL STATUS)
******************************************************************/


use "$ifls5\bk_ar1", clear
keep if inlist(ar01a, 1,2,3,5,11)
keep if ar01i==1
keep pid* hhid* ar07 ar16 ar17 ar13

*JENIS KELAMIN*
recode ar07 (1=1 "laki-laki") (3=0 "perempuan"), gen (sex)


*SIZE FAMILY*
gen member=1
bys hhid14: egen size_family=sum(member)

*YEAR OF SCHOOLING*
*Tidak/Belum Pernah Sekolah
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
replace educyear=6 if ar16==11


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


*SMP UMUM*
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
replace educyear=9 if ar16==12

*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

*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
replace educyear=12 if ar16==15


*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


*AKADEMI*
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=15 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=17 if ar16==61 & ar17==5
replace educyear=18 if ar16==61 & ar17==6
replace educyear=16 if ar16==61 & ar17==7

*UNIVERSITAS TERBUKA*
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==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=19 if ar16==62 & ar17==3
replace educyear=18 if ar16==62 & ar17==7

*S3*
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=21 if ar16==63 & ar17==7

*marital status
recode ar13 (1 6 =0 "Belum Menikah") (3 4 5 =1 "Cerai") (2=2 "menikah") (8 9=.), gen (marstat)



keep hhid* pid* sex size_family educyear marstat
save "$hasil\dataset_jk_yos_hhsize_marstat", replace

/*******************************************************************
					VARIABEL WILAYAH TEMPAT TINGGAL
******************************************************************/

use "$ifls5\bk_sc1", clear
keep hhid* sc05
recode sc05 (1=1 "perkotaan") (2=0 "perdesaan"), gen (res)
save "$hasil\res", replace


/*******************************************************************
						VARIABEL DEPRESI
******************************************************************/
use "$ifls5\b3b_kp", clear
keep hhid* pid* kptype kp02
reshape wide kp02, i(hhid14 pid14) j(kptype)str
keep hhid* pid* kp02A kp02B kp02C kp02D kp02F kp02G kp02I
recode kp02A (1=0)(2=1) (3=2) (4=3)
recode kp02B (1=0)(2=1) (3=2) (4=3)
recode kp02C (1=0)(2=1) (3=2) (4=3)
recode kp02D (1=0)(2=1) (3=2) (4=3)
recode kp02F (1=0)(2=1) (3=2) (4=3)
recode kp02G (1=0)(2=1) (3=2) (4=3)
recode kp02I (1=0)(2=1) (3=2) (4=3)
egen skor_kp=rowtotal(kp02A kp02B kp02C kp02D kp02F kp02G kp02I)
gen mean_skor_kp=skor_kp/7
gen mean_skor_kp_bulat=int(mean_skor_kp)
keep hhid* pid* skor_kp mean_skor_kp mean_skor_kp_bulat
save "$hasil\depresi", replace

/*******************************************************************
					VARIABEL RELIJIUSITAS
******************************************************************/
use "$ifls5\b3a_tr", clear
keep hhid* pid* tr11
recode tr11 (3 4 = 0 "tidak relijius") (1 2 = 1 "relijius") (7=.), gen(relijius)
save "$hasil\relijius", replace


/*******************************************************************
				VARIABEL KEPEMILIKIAN PERHIASAN
******************************************************************/
use "$ifls5\b3a_hr1", clear
duplicates report hhid14
duplicates drop hhid14, force
keep hhid* pid* hr01
recode hr01 (3 = 0 "tidak memiliki") (1 = 1 "memiliki") (8=.), gen(perhiasan)
save "$hasil\perhiasan", replace

/*******************************************************************
							VARIABEL DONASI
******************************************************************/
use "$ifls5\b1_ks3", clear
keep if ks3type=="D"
ren ks08 donasi
keep hhid* donasi
tab donasi
save "$hasil\donasi", replace

/*******************************************************************
							VARIABEL BAHASA
******************************************************************/
use "$ifls5\b3a_dl1", clear
keep hhid* pid* dl01a
gen bahasa_indonesia= regexm(dl01a, "W")
lab value bahasa_indonesia bahasa_indonesia
lab def bahasa_indonesia 1 "Bahasa Indonesia" 0 "Tidak Menggunakan Bahasa Indonesia"
keep hhid* pid* bahasa_indonesia
save "$hasil\bahasa", replace



// foreach y in A B C D E F G H I J L M N O P Q R S T U V W {
// gen bahasa_`y'= regexm(dl01a, "`y'")
// }

/*******************************************************************
							MERGING DATA
******************************************************************/
use "$hasil\dataset_jk_yos_hhsize", clear
merge m:1 hhid14 using "$hasil\res"
drop _merge

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


merge 1:1 hhid14 pid14 using "$hasil\relijius"
drop _merge


merge m:1 hhid14 pid14 using "$hasil\perhiasan"
drop _merge

merge m:1 hhid14 using "$hasil\donasi"
drop _merge

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

log close