clear all cd ..\data use observation sort observeid save, replace use membership.dta gen observeid = mem_sobserveid gen sround = substr( mem_sobserveid, 10,3) destring sround, replace drop mem_sobserveidGuid mem_eobserveidGuid socialgpidGuid gen eround ="023" if mem_eobserveid=="" replace eround = substr( mem_eobserveid, 10,3) if mem_eobserveid !="" destring(eround), replace duplicates drop individid mem_sobserveid,force duplicates drop individid eround,force gen period = eround - sround expand period sort individid socialgpid sround by individid socialgpid sround: gen step=_n-1 gen round = sround + step duplicates drop individid round, force drop if round > 22 tostring round, replace replace round = "00" + round if strlen(round) == 1 replace round ="0" + round if strlen(round)==2 gen individkey = individid + round gen householdkey = socialgpid + round save ..\newcode\data\std_membership1, replace merge m:1 observeid using observation, keepusing(locationid) keep if _merge ==3 drop _merge observeid mem_episodeid mem_sobserveid mem_seventtype mem_eobserveid mem_eeventtype mem_no sround eround period step mem_sdate mem_edate /* get individual core information */ sort individid merge m:1 individid using ..\newcode\data\std_individual keep if _merge ==3 drop _merge /* get education information */ sort individkey merge 1:1 individkey using ..\newcode\data\std_education.dta keep if _merge!=2 drop _merge /* get occupation information */ sort individkey merge 1:1 individkey using ..\newcode\data\std_occupation.dta keep if _merge!=2 drop _merge /* get marial status information */ sort individkey merge 1:1 individkey using ..\newcode\data\std_marstatus.dta keep if _merge!=2 drop _merge /* get religion information */ sort individkey merge 1:1 individkey using ..\newcode\data\std_religion.dta keep if _merge!=2 drop _merge /* get marriage information */ sort individkey merge 1:1 individkey using ..\newcode\data\std_relationship.dta, keepusing(partnerid marriagedate marriagetype) keep if _merge!=2 drop _merge replace marstatusid = 1 if marriagetype =="MAR" /* get Insurance information */ sort individkey merge 1:1 individkey using ..\newcode\data\std_medinsurance.dta, keepusing(hinsid) keep if _merge!=2 drop _merge /* get househead information */ sort individkey merge 1:1 individkey using ..\newcode\data\std_househeadlist keep if _merge!=2 gen ishousehead =0 replace ishousehead =0 if _merge ==1 replace ishousehead =1 if _merge==3 drop _merge sort individid merge m:1 individid using ..\newcode\data\std_enterexit keep if _merge!=2 drop _merge label drop _merge gen CommuneId = substr(locationid, 1,2) /* recode */ label variable sex "Gioi tinh" label variable socialgpid "Ma ho" label variable fatherid "Ma so bo" label variable motherid "Ma so me" label variable hinsid "Bao hiem y te" label variable ishousehead "La chu ho" label define chuho 1 "La chu ho" 0 "Khong la chu ho" label values ishousehead chuho label variable marriagetype "Hinh thuc hon nhan" label variable CommuneId "Ma xa" save ..\newcode\data\std_membership, replace