*===================================================
*                 LATIHAN IFLS
*                 PROYEK AKHIR
*===================================================

clear
set more off
capture log close

*----------------------------------------------------

*media penyimpanan*
global ifls5 "D:\Economic Data\LATIHAN IFLS\PROJECT\1.INPUT\HH"

*proses*
cd "D:\Economic Data\LATIHAN IFLS\PROJECT\2.PROSES"

*hasil*
global hasil "D:\Economic Data\LATIHAN IFLS\PROJECT\3.HASIL"

log using "$hasil\log file penelitian.log", replace
*-----------------------------------------------------



*==========================DATA SET========================


*----------------sex, maristat, famsize, educyear------------

*variabel jenis kelamin*
use "$ifls5\bk_ar1", replace
recode ar07(1=1 "male")(3=0 "female"), gen(sex)

*variabel maristat*
recode ar13 (1=0 "belum menikah")(4=1 "cerai")(2=2 "menikah"),gen(maristat)

*variabel ukuran RT*
gen bantu=1
bys hhid14_9: egen famsize=sum(bantu)

*variabel pendidikan*

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 = 7 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 = 6 if ar16 == 11 & ar17 > 7
replace educyear = 6 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 = 9 if ar16 == 12 & ar17 > 7
replace educyear = 9 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 = 12 if ar16 == 15 & ar17 > 7
replace educyear = 12 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 S3
replace educyear = 16 if ar16 == 62
replace educyear = 16 if ar16 == 63

//pendidikan berdasarkan jenjang edukasi
gen educ=0 if educyear <= 6
replace educ=1 if educyear >=7 & educyear <=9
replace educ=2 if educyear >=10 & educyear <=12
replace educ=3 if educyear >=13 & educyear <=14
la val educ educ
lab def educ 0 "SD/sederajat" 1 "SMP/sederajat" 2 "SMA/sederajat" 3 "Universitas/sederajat"

//pendidikan berdasarkan sekolah rendah dan sekolah tinggi
gen pendidikan=0 if educyear <=12
replace pendidikan=1 if educyear >=13 & educyear <=16
la val pendidikan pendidikan
lab def pendidikan 0 "0:pendidikan rendah" 1 "1:pendidikan tinggi"


keep sex maristat famsize educyear educ pendidikan hhid* pid*

save"$hasil\data1", replace


*_______________________________________________________________



*-------------------------tempat tinggal------------------------
*variabel tempat tinggal*
use "$ifls5\bk_sc1", replace
recode sc05 (1=1 "perkotaan")(2=0 "perdesaan"), gen(res)

keep res hhid*

save "$hasil\data2", replace

*_______________________________________________________________



*-------------------------relijiusitas------------------------
*variabel relijiusitas*
use "$ifls5\b3a_tr", replace
recode tr11 (1 2=1 "relijius")(3 4=0 "tidak relijius"), gen(relijius)

keep relijius hhid* pid*

save "$hasil\data3", replace

*_______________________________________________________________



*-------------------kepemilikan perhiasan------------------------
*variabel perhiasan*
use "$ifls5\b3a_hr1", replace
keep hr01 hr1type pid* hhid*
reshape wide hr01, i(pidlink) j(hr1type) string
recode hr01J (1=1 "memiliki perhiasan")(3=0 "tidak memiliki perhiasan"), gen(perhiasan)

keep perhiasan hhid* pid*

save "$hasil\data4", replace

*_______________________________________________________________



*---------------------------donasi-------------------------------
*variabel donasi*
use "$ifls5\b1_ks3", replace
keep ks3type ks08 hhid*
reshape wide ks08, i(hhid14_9) j(ks3type) string
gen donasi=(ks08D)

keep donasi hhid*

save "$hasil\data5", replace

*_______________________________________________________________



*---------------------------bahasa-------------------------------
*variabel bahasa*
use "$ifls5\b3a_dl1", replace
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 i in alfabet {
gen dummy_bahasa`i' = regexm(dl01a, "`i''")
}
recode dummy_bahasa (1=1 "Bahasa Inggris")(0=0 "tidak menggunakan Bahasa Inggris"), gen(bahasa)

keep bahasa hhid* pid*

save "$hasil\data6", replace

*_______________________________________________________________

*---------------------menggabungkan data set---------------------
use "$hasil\data1", replace
merge m:1 pidlink using "$hasil\data3"
keep if _m==3
save "$hasil\merge1&3", replace


use "$hasil\merge1&3", replace
drop _merge
merge m:1 pidlink using "$hasil\data4"
keep if _m==3
save "$hasil\merge1,3,4", replace


use "$hasil\merge1,3,4", replace
drop _merge
merge m:1 pidlink using "$hasil\data6"
keep if _m==3
drop _merge
save "$hasil\merge1,3,4,6", replace


use "$hasil\data2", replace
merge m:1 hhid14 using "$hasil\data5"
keep if _m==3
drop _merge
save "$hasil\merge2&5", replace


*_________________________?????????????????????________________
*variabel mental health*
use "$ifls5\b3b_kp", replace
keep kptype kp02 hhid* pid*

reshape wide kp02, i(pidlink) j(kptype) string


egen depresi = mean(pidlink)

foreach k in A B C D E F G I {
bys pidlink: egen depresi `k'=mean(kp02, "`k'")
}