红线为可精确匹配的亲子代关系,仅需使用ind库,以2017年为例,得到亲子匹配关系29,588条。
**************提取亲子关系
use "$CHFS2017/chfs2017_ind.dta", clear
keep hhid pline a2001 a2003 a2005
rename a2001 relation
rename a2003 sex
rename a2005 birth
recode sex (2=0)
save "$temp_data/family.dta",replace
**当辈
use "$temp_data/family.dta",replace
keep if relation==2
rename * *_part
rename hhid* hhid
drop if sex_part==.
duplicates list hhid
duplicates tag hhid,gen(dup)
drop if dup==1
drop dup
save "$temp_data/partner.dta", replace
use "$temp_data/family.dta",replace
keep if relation==1
save "$temp_data/responser.dta", replace
use "$temp_data/family.dta",replace
keep if relation==10
save "$temp_data/sibl.dta", replace
**子代,子女有多个,无法匹配子代与孙代间亲子关系
// use "$temp_data/family.dta",replace
// keep if relation==7
// rename * *_cpart
// rename hhid* hhid
// drop if sex_cpart==.
// duplicates list hhid
// duplicates tag hhid,gen(dup)
// drop if dup==1
// drop dup
// save "$temp_data/childpartner.dta",replace
use "$temp_data/family.dta",replace
keep if relation==6
// merge 1:1 hhid using "$temp_data/childpartner.dta",nogen
rename * *_c
rename hhid* hhid
save "$temp_data/child.dta",replace
**当代和子代亲子关系
use "$temp_data/responser.dta",replace
merge 1:1 hhid using "$temp_data/partner.dta"
keep if _merge == 3
drop _merge
save "$temp_data/relation1.dta",replace
use "$temp_data/child.dta",replace
merge m:1 hhid using "$temp_data/relation1.dta"
keep if _merge == 3
drop _merge
gen check =1 if birth_c< min(birth,birth_part) //检查
// tag check
drop if check==1
drop check
rename * *_r
rename hhid* hhid
rename *_c_r *
keep hhid pline* sex*
gen check = sex_r + sex_part
keep if check == 1
drop check
gen pline_f = .
gen pline_m = .
foreach i in "r" "part"{
replace pline_f = pline_`i' if sex_`i' == 1
replace pline_m = pline_`i' if sex_`i' == 0
}
keep hhid pline pline_f pline_m
save "$temp_data/generation1.dta",replace
// **孙代
// use "$temp_data/family.dta",replace
// keep if relation==8
// rename * *_grandc
// rename hhid* hhid
// save "$temp_data/grandchild.dta",replace
**受访者父代
use "$temp_data/family.dta",replace
keep if relation==3
duplicates tag hhid,gen(dup)
drop if dup!=1
drop dup
sort hhid
by hhid:egen check = sum(sex)
drop if check != 1
drop check
save "$temp_data/parent.dta",replace
use "$temp_data/parent.dta",replace
keep if sex == 0
rename * *_m
rename hhid* hhid
save "$temp_data/m.dta",replace
use "$temp_data/parent.dta",replace
keep if sex == 1
rename * *_f
rename hhid* hhid
merge 1:1 hhid using "$temp_data/m.dta"
keep if _merge == 3
drop _merge
save "$temp_data/relation21.dta",replace
*受访者父代匹配
use "$temp_data/responser.dta",replace
merge m:1 hhid using "$temp_data/relation21.dta"
keep if _merge == 3
drop _merge
gen check =1 if birth< min(birth_f,birth_m) //检查
// tag check
drop if check==1
drop check
save "$temp_data/generation21a.dta",replace
*受访者兄弟姐妹父代匹配
use "$temp_data/sibl.dta",replace
merge m:1 hhid using "$temp_data/relation21.dta"
keep if _merge == 3
drop _merge
gen check =1 if birth< min(birth_f,birth_m) //检查
// tag check
drop if check==1
drop check
append using "$temp_data/generation21a.dta"
save "$temp_data/generation21.dta",replace
**受访者配偶父代匹配
use "$temp_data/family.dta",replace
keep if relation==4
duplicates tag hhid,gen(dup)
drop if dup!=1
drop dup
sort hhid
by hhid:egen check = sum(sex)
drop if check != 1
drop check
save "$temp_data/pparent.dta",replace
use "$temp_data/pparent.dta",replace
keep if sex == 0
rename * *_m
rename hhid* hhid
save "$temp_data/pm.dta",replace
use "$temp_data/pparent.dta",replace
keep if sex == 1
rename * *_f
rename hhid* hhid
merge 1:1 hhid using "$temp_data/pm.dta"
keep if _merge == 3
drop _merge
save "$temp_data/relation22.dta",replace
*受访者配偶父代匹配
use "$temp_data/partner.dta",replace
merge 1:1 hhid using "$temp_data/relation22.dta"
keep if _merge == 3
drop _merge
rename *_part *
gen check =1 if birth< min(birth_f,birth_m) //检查
// tag check
drop if check==1
drop check
save "$temp_data/generation22.dta",replace
use "$temp_data/generation22.dta",replace
append using "$temp_data/generation21.dta"
keep hhid pline pline_f pline_m
save "$temp_data/generation2.dta",replace
use "$temp_data/generation2.dta",replace
append using "$temp_data/generation1.dta"
save "$temp_data/generation.dta",replace
// *祖代,由于不区分祖/外祖,无法直接对应祖代和父代亲子关系
// use "$temp_data/family.dta",replace
// keep if relation==5
// rename * *_ppparent
// rename hhid* hhid
// save "$temp_data/parentsparent.dta",replace