全部版块 我的主页
论坛 计量经济学与统计论坛 五区 数据交流中心
15394 36
2017-08-10

     匹配完相邻两年的样本,生成mi_mj.dta数据文件后,相邻三年间的匹配较为容易,不需要改动。需要注意的是,在这一阶段程序中,处理不一致情况的时候BRANDT用到了merge命令形式较老,stata13之前的版本可以直接识别,但stata14会提示应当用新式命令,即merge后应该加1:1;m:1;1:m或m:m,但stata14依旧能够识别老式命令,可以运行无阻。其程序如下:

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'  
}  

这一步的最终结果是生成unbalanced.`i'-`j'-`k'.dta数据文件,为下一步做好准备。


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2017-8-10 19:40:13
谢谢分享
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2017-9-13 08:55:08

谢谢分享
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2017-12-26 10:59:53
thx for sharing
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2017-12-27 14:13:16
请教楼主一个问题。step 70 需要merge 1:1 code,但运行时发现code不唯一。是否需要先drop掉code重复的值再merge?
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2017-12-29 15:54:33
942759615@qq.co 发表于 2017-12-27 14:13
请教楼主一个问题。step 70 需要merge 1:1 code,但运行时发现code不唯一。是否需要先drop掉code重复的值再 ...
是的,应该保持一致的
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

点击查看更多内容…
相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群