clear all cd c:\chililabdata use individres sort individid res_sdate gen thisround = substr(res_sobserveid, 1,2) drop res_episodeid res_seventtype res_sobserveid res_eobserveid res_sobserveidGuid res_eobserveidGuid by individid: gen p=_n by individid: gen last=_N cd data save individres_base, replace keep if p==1 duplicates drop individid, force gen sdate = res_sdate/86400000 keep individid sdate thisround format sdate %td sort individid save beginRes, replace use individres_base keep if p == last gen edate = res_edate/86400000 replace edate = mdy(12,31,2013) if edate ==. rename res_eeventtype event format edate %td keep individid locationid edate event duplicates drop individid, force sort individid merge 1:1 individ using beginRes drop _merge gen peryear = (edate-sdate)/365.25 gen endpoint =0 replace endpoint =1 if event == "DTH" gen year = year(edate) gen h = halfyear(edate) gen round = yh(year,h)-89 tostring round, generate(rounds) replace rounds = "0" + rounds if strlen(rounds) ==1 gen individkey = individid + rounds drop rounds h round sort individkey cd death merge 1:1 individkey using rMembership, keepusing(socialgpid) sort individid drop _merge /*get gender*/ merge m:1 individid using individual, keepusing(gender birthdate) gen birthyear = year(birthdate/86400000) keep if _merge ==3 drop _merge /*get Education*/ merge 1:m individkey using rEducation, keepusing(eduid) keep if _merge ==3 drop _merge /*get Occupation*/ merge 1:m individkey using rOccupation, keepusing(occupationid) keep if _merge ==3 drop _merge /*get Marital Status*/ merge 1:m individkey using rMarstatus, keepusing(marstatusid) keep if _merge ==3 drop _merge tostring year, replace destring gender , replace rename gender sex gen socialkey = socialgpid + year sort socialkey merge m:1 socialkey using houseinfo, keepusing(quintile) keep if _merge ==3 drop _merge gen bod = birthdate/86400000 drop event year individkey socialgpid birthdate socialkey sort individid merge 1:1 individid using 60inds, keepusing(individid) keep if _merge==3 drop _merge gen eAge = ceil((edate-bod)/365.25) gen sAge = ceil((sdate -bod)/365.25) format bod %td gen date60 = mdy(month(bod), day(bod), year(bod) + 60) format date60 %td replace peryear = (edate -date60)/365.25 if date60>sdate gen duration = round(peryear) drop if duration <= 0 drop if duration > 10 destring occupationid , replace destring edu, replace drop if edu==0 gen communeid = substr(locationid, 1,2) destring communeid, replace recode eduid (1/3 = 1) (8/11 = 1) (12/14 = 2) (15/19 = 3) (4/7=4) recode occupationid (1= 1) (6 = 2) (5 = 3) (2/4=4) (7/13=4) recode marstatusid (0=0)(1 6 = 1) (2/4 = 2) (5=3) recode communeid (1 = 1) ( 2= 2) (3=1) (4= 2) (5 =2) (6 = 1) (7 =2), generate(region) label define Region 1 "Đô thị" 2 "Nông thôn" label values region Region label variable marstatusid "Tinh trang hon nhan" label variable eduid "Trinh do hoc van" label variable occupationid "Nghe nghiep" label variable sex "Gioi tinh" label variable quintile "Muc do giau ngheo" label variable communeid "Xã" label define sexes 1 "Nam" 2 "Nu" label values sex sexes label define Occupation 1 "1. Làm ruong" 2 "2. Huu tri" 3 "3. Buon ban dich vu" 4 "4. Khac" label values occupationid Occupation label define edu 1 "1. Duoi THCS" 2 "2. THSC" 3 "3. THPT" 4 "4. Tren THPT" label values eduid edu label define marstatus 0 "0. Khong biet" 1 "1. Song cung vo/chong" 2 "2. Ly than/Ly di/Goa" 3 "3. Doc than" label values marstatusid marstatus label define CommnueLabel 1 "Sao Đỏ" 2 "An Lạc" 3 "Phả Lại" 4 "Văn An" 5 "Lê Lợi" 6 "Bến Tắm" 7 "Hoàng Tiến" label values communeid CommnueLabel saveold indperiod, replace