cd "/Users/ac/Desktop/工商注册信息匹配2026-04-14" use "匹配结果.dta", clear
di "===== 初始重复情况 =====" duplicates report myid
local nameA 公司名称_原始数据 local nameB 企业名称 local id myid
duplicates tag `id', gen(tags) gsort -tags `id'
gen temp1 = (`nameA' == `nameB') bysort `id': egen maxtemp1 = max(temp1) drop if temp1 == 0 & maxtemp1 > 0 di "===== 第1步后(名称完全一致)=====" duplicates report `id' cap drop temp1 maxtemp1
gen temp1 = (index(`nameA', "集团") & index(`nameB', "集团")) bysort `id': egen maxtemp1 = max(temp1) drop if temp1 == 0 & maxtemp1 > 0 cap drop temp1 maxtemp1
gen temp1 = (!index(`nameA', "集团") & !index(`nameB', "集团")) bysort `id': egen maxtemp1 = max(temp1) drop if temp1 == 0 & maxtemp1 > 0 di "===== 第3步后(集团层级过滤)=====" duplicates report `id' cap drop temp1 maxtemp1
foreach keyword in "支行" "分行" { gen temp1 = (!index(`nameA', "`keyword'") & !index(`nameB', "`keyword'")) bysort `id': egen maxtemp1 = max(temp1) drop if temp1 == 0 & maxtemp1 > 0 cap drop temp1 maxtemp1 } di "===== 第4-5步后(银行支行/分行)=====" duplicates report `id'
gen temp1 = (!index(`nameA', "站") & !index(`nameB', "站")) bysort `id': egen maxtemp1 = max(temp1) drop if temp1 == 0 & maxtemp1 > 0 cap drop temp1 maxtemp1
gen temp1 = (!index(`nameA', "部") & !index(`nameB', "部")) bysort `id': egen maxtemp1 = max(temp1) drop if temp1 == 0 & maxtemp1 > 0 di "===== 第7步后(站/部门过滤)=====" duplicates report `id' cap drop temp1 maxtemp1
foreach keyword in "子公司" "分公司" "支公司" { gen temp1 = (!index(`nameA', "`keyword'") & !index(`nameB', "`keyword'")) bysort `id': egen maxtemp1 = max(temp1) drop if temp1 == 0 & maxtemp1 > 0 cap drop temp1 maxtemp1 } di "===== 第7b步后(子/分/支公司层级)=====" duplicates report `id'
gen temp1 = (!ustrregexm(`nameA', "公司(.*)公司") & !ustrregexm(`nameB', "公司(.*)公司")) bysort `id': egen maxtemp1 = max(temp1) drop if temp1 == 0 & maxtemp1 > 0 cap drop temp1 maxtemp1
cap drop tags duplicates tag `id', gen(tags) foreach suffix in "公司" "局" "煤矿" "中心" "厂" "部" { drop if ustrregexm(`nameA', "公司(.*)`suffix'") & !ustrregexm(`nameB', "公司(.*)公司") drop if !ustrregexm(`nameA', "公司(.*)`suffix'") & ustrregexm(`nameB', "公司(.*)公司") drop if ustrregexm(`nameA', "公司(.*)公司") & !ustrregexm(`nameB', "公司(.*)`suffix'") drop if !ustrregexm(`nameA', "公司(.*)公司") & ustrregexm(`nameB', "公司(.*)`suffix'") } drop if ustrregexm(`nameA', "公司$") & !ustrregexm(`nameB', "公司$") di "===== 第8-9步后(嵌套结构过滤)=====" duplicates report `id' cap drop tags
gen nameB_tmp = subinstr(`nameB', "股份", "", .) gen temp1 = (`nameA' == nameB_tmp) bysort `id': egen maxtemp1 = max(temp1) drop if temp1 == 0 & maxtemp1 > 0 cap drop temp1 maxtemp1 nameB_tmp
duplicates drop `id' `nameB', force di "===== 第11步后(完全重复)=====" duplicates report `id'
gen nameB_tmp = subinstr(`nameB', "责任", "", .) gen temp1 = (`nameA' == nameB_tmp) bysort `id': egen maxtemp1 = max(temp1) drop if temp1 == 0 & maxtemp1 > 0 cap drop temp1 maxtemp1 nameB_tmp di "===== 第12步后(有限公司/有限责任公司)=====" duplicates report `id'
cap drop tags duplicates tag `id', gen(tags) cap ssc install strdist strdist `nameA' `nameB' if tags > 0, gen(dist) bysort `id': egen maxdist = max(dist) keep if dist == maxdist | mi(dist) di "===== 第13步后(字符串相似度兜底)=====" duplicates report `id'
duplicates drop `id', force cap drop dist maxdist temp1 maxtemp1 tags di "===== 最终去重结果 =====" duplicates report `id' count
save "去重结果.dta", replace di "去重完成,结果已保存至 去重结果.dta"
|
评论