forvalues i = 1998/2005{
local j = `i'+1
local k = `i'+2
**Step 70 创造一个三年的平衡样本:
disp "Step 70 "
use m`i'-m`j'.dta,clear
keep if match_status_`i'_`j' == "1"
keep *`i'
compress
saveold unmatched`i'.10.dta,replace
use m`i'-m`j'.dta,clear
drop if match_status_`i'_`j' == "1"
gen code = id`j'+string(revenue`j')+string(employment`j')+string(profit`j')+province`j'
sort code
compress
saveold m`i'-m`j'.10.dta,replace
use m`j'-m`k'.dta,clear
keep if match_status_`j'_`k' == "2"
keep *`k'
compress
saveold unmatched`k'.10.dta,replace
use m`j'-m`k'.dta,clear
drop if match_status_`j'_`k' == "2"
gen code = id`j'+string(revenue`j')+string(employment`j')+string(profit`j')+province`j'
sort code
compress
saveold m`j'-m`k'.10.dta,replace
use m`i'-m`j'.10.dta,clear
merge 1:1 code using m`j'-m`k'.10.dta
drop _m code
keep if match_status_`i'_`j'=="3" & match_status_`j'_`k'=="3"
gen match_status_`i'_`k'="3"
gen match_method_`i'_`k'="`j'"
compress
saveold balanced.m`i'-m`j'-m`k'.dta,replace
**Step 80 对未匹配成功的i年公司和j年公司单独保存:
disp "Step 80"
use m`i'-m`j'.10.dta,clear
merge 1:1 code using m`j'-m`k'.10.dta
drop _m code
drop if match_status_`i'_`j'=="3" & match_status_`j'_`k'=="3"
drop if id`i'==""
gen code = id`i'+string(revenue`i')+string(employment`i')+string(profit`i')+province`i'
sort code
compress
saveold unmatched`i'.15.dta,replace
use unmatched`i'.15.dta,clear
keep *`i'
append using unmatched`i'.10.dta
compress
saveold unmatched`i'.20.dta,replace
use m`i'-m`j'.10.dta,clear
merge 1:1 code using m`j'-m`k'.10.dta
drop _m code
drop if match_status_`i'_`j'=="3" & match_status_`j'_`k'=="3"
drop if id`k'== ""
gen code = id`k'+string(revenue`k')+string(employment`k')+string(profit`k')+province`k'
sort code
compress
saveold unmatched`k'.15.dta,replace
use unmatched`k'.15.dta,clear
keep *`k'
append using unmatched`k'.10.dta
compress
saveold unmatched`k'.20.dta,replace
use m`i'-m`j'.10.dta,clear
merge 1:1 code using m`j'-m`k'.10.dta
drop _m code
drop if match_status_`i'_`j'=="3" & match_status_`j'_`k'=="3"
gen code = id`j'+string(revenue`j')+string(employment`j')+string(profit`j')+province`j'
sort code
compress
saveold unmatched`j'.15.dta,replace
**Step 90 对i年公司和第三年k的公司以法人代码(id)和公司名称(name)进行匹配:
disp "Step 90"
use unmatched`i'.20.dta,clear
bysort id`i': keep if _N>1
compress
saveold duplicates_ID`i'.dta,replace
use unmatched`i'.20.dta,clear
bysort id`i': drop if _N>1
rename id`i' id
keep *`i' id
sort id
compress
saveold match`i'.1.dta,replace
use unmatched`k'.20.dta,clear
bysort id`k': keep if _N>1
compress
saveold duplicates_ID`k'.dta,replace
use unmatched`k'.20.dta,clear
bysort id`k': drop if _N>1
rename id`k' id
keep *`k' id
sort id
compress
saveold match`k'.1.dta,replace
use match`i'.1.dta,clear
merge 1:1 id using match`k'.1.dta
keep if _m==3
gen id`i'=id
rename id id`k'
drop _m
gen match_method_`i'_`k'="`j'"
gen match_status_`i'_`k'="3"
compress
saveold matched_by_ID`i'_`k'.dta,replace
*公司名称(name)*
use match`i'.1.dta, clear
merge 1:1 id using match`k'.1.dta
keep if _merge==1
rename id id`i'
append using duplicates_ID`i'.dta
bysort name`i': keep if _N>1
keep *`i'
compress
saveold duplicates_name`i'.dta, replace
use match`i'.1.dta, clear
merge 1:1 id using match`k'.1.dta
keep if _merge==1
rename id id`i'
append using duplicates_ID`i'.dta
bysort name`i': drop if _N>1
rename name`i' name
sort name
keep name *`i'
compress
saveold unmatched_by_ID`i'.dta, replace
use match`i'.1.dta, clear
merge 1:1 id using match`k'.1.dta
keep if _merge==2
rename id id`k'
append using duplicates_ID`k'.dta
bysort name`k': keep if _N>1
keep *`k'
compress
saveold duplicates_name`k'.dta, replace
use match`i'.1.dta, clear
merge 1:1 id using match`k'.1.dta
keep if _merge==2
rename id id`k'
append using duplicates_ID`k'.dta
bysort name`k': drop if _N>1
rename name`k' name
sort name
keep name *`k'
compress
saveold unmatched_by_ID`k'.dta, replace
use unmatched_by_ID`i'.dta, clear
merge 1:1 name using unmatched_by_ID`k'.dta
keep if _merge==3
gen name`i'=name
rename name name`k'
drop _merge
gen match_method_`i'_`k'="firm name"
gen match_status_`i'_`k'="3"
compress
saveold matched_by_name`i'_`k'.dta, replace
use unmatched_by_ID`i'.dta, clear
merge 1:1 name using unmatched_by_ID`k'.dta
keep if _merge==1
rename name name`i'
keep *`i'
append using duplicates_name`i'.dta
gen match_method_`i'_`k'=""
gen match_status_`i'_`k'="1"
compress
saveold unmatched_by_ID_and_name_`i'.dta, replace
use unmatched_by_ID`i'.dta, clear
merge 1:1 name using unmatched_by_ID`k'.dta
keep if _merge==2
rename name name`k'
keep *`k'
append using duplicates_name`k'.dta
gen match_method_`i'_`k'=""
gen match_status_`i'_`k'="2"
compress
saveold unmatched_by_ID_and_name_`k'.dta, replace
**step 100 将上述所有样本再进行大合并:
disp "Step 100"
use matched_by_ID`i'_`k'.dta, clear
append using matched_by_name`i'_`k'.dta
append using unmatched_by_ID_and_name_`i'.dta
append using unmatched_by_ID_and_name_`k'.dta
compress
saveold m`i'-m`k'.dta, replace
use m`i'-m`k'.dta, clear
gen code = id`i'+string(revenue`i')+string(employment`i')+string(profit`i')+province`i'
sort code
*drop if code == "..."
merge code using unmatched`i'.15.dta
drop code _merge
sort id`i'
compress
saveold m`i'-m`k'.05.dta, replace
*处理不一致情况(disagreement ) (_merge==5 if "update" is used):
use m`i'-m`k'.05.dta, clear
gen code = id`k'+string(revenue`k')+string(employment`k')+string(profit`k')+province`k'
sort code
*这里用到了老式merge命令格式,但STATA14可以识别,但会显示提示新格式:
merge code using unmatched`k'.15.dta, update
keep if _merge==5
drop *`k'
drop code _merge
sort id`i'
compress
compress
saveold m`i'-m`k'.disagree.dta, replace
use m`i'-m`k'.05.dta, clear
merge id`i' using m`i'-m`k'.disagree.dta
drop if _merge==3
drop _merge
append using m`i'-m`k'.disagree.dta
gen code = id`k'+string(revenue`k')+string(employment`k')+string(profit`k')+province`k'
sort code
merge code using unmatched`k'.15.dta, update
drop code _merge
gen code = id`j'+string(revenue`j')+string(employment`j')+string(profit`j')+province`j'
sort code
merge code using unmatched`j'.15.dta, update
drop code _merge
compress
saveold m`i'-m`k'.dta.10.dta, replace
use m`i'-m`k'.dta.10.dta, clear
append using balanced.m`i'-m`j'-m`k'.dta
drop match_status_`i'_`j'
drop match_status_`j'_`k'
drop match_status_`i'_`k'
drop match_method_`i'_`j'
drop match_method_`j'_`k'
drop match_method_`i'_`k'
gen match_status_`i'_`j'_`k'="`i'-`j'-`k'" if id`i'!=""&id`j'!=""&id`k'!=""
replace match_status_`i'_`j'_`k'="`i'-`j' only" if id`i'!=""&id`j'!=""&id`k'==""
replace match_status_`i'_`j'_`k'="`j'-`k' only" if id`i'==""&id`j'!=""&id`k'!=""
replace match_status_`i'_`j'_`k'="`i'-`k' only" if id`i'!=""&id`j'==""&id`k'!=""
replace match_status_`i'_`j'_`k'="`i' no match" if id`i'!=""&id`j'==""&id`k'==""
replace match_status_`i'_`j'_`k'="`j' no match" if id`i'==""&id`j'!=""&id`k'==""
replace match_status_`i'_`j'_`k'="`k' no match" if id`i'==""&id`j'==""&id`k'!=""
compress
saveold unbalanced.`i'-`j'-`k'.dta, replace
}
forval i = 1998(1)2005{
local j=`i'+1
local k=`i'+2
use unbalanced.`i'-`j'-`k'.dta, clear
tab match_status_`i'_`j'_`k'
}
请问运行到 step 70
use m`i'-m`j'.10.dta,clear
merge 1:1 code using m`j'-m`k'.10.dta这个命令的时候出现了variable _merge already defined这样的问题怎么解决。
我尝试着在合并之前对需要合并的文件进行了如下操作:
use m`i'-m`j'.10.dta,clear
drop _merge
use m`j'-m`k'.10.dta,clear
drop _merge
但是再次运行依然出现来了variable _merge already defined