如何用stata编程,寻找配对样本
(2014-05-19 10:46:29)分类: 3.重要问题全攻略大集合 |
比如说,有四个条件,年度、上市地(沪市或深市)、行业以及总资产(正负30%以内)
如何用stata找出比较理想的配对样本
如何用stata找出比较理想的配对样本
试试:
// 生成模拟数据,非平衡面板数据
clear
set more off
set obs 1000
gen stkcd = _n
gen industry = ceil(10*uniform())
// 10个行业
gen exchange = cond(uniform()<0.6, 0, 1) // 60%样本属于上交所SSE
label define lexch 0 "SSE" 1 "SZSE"
label value exchange lexch
gen asset = 100*exp(invnormal(uniform()))
gen control = 10*uniform() //
控制变量,测试用
expand 5
drop if uniform() < 0.1 //
非平衡面板数据
bysort stkcd: gen year = 2010 - _n
sort stkcd year
by stkcd: replace asset = asset * (1 + 0.1*uniform()) if _n > 1
save sample, replace
// 主程序
use sample, clear
sort stkcd year
// 假定为long
form数据,否则用reshape命令
save temp, replace
capture postclose match
postfile match group stkcd year dmatch using matchsample.dta, replace
local total = _N
local j = 0
forval i = 1/`total' {
preserve
local lstkcd
= stkcd[`i']
local lyear =
year[`i']
local
lexchange = exchange[`i']
local
lindustry = industry[`i']
local lasset
= asset[`i']
quietly drop
in `i'
// 删除自身
quietly keep
if industry == `lindustry'
quietly keep
if year == `lyear'
quietly keep
if exchange == `lexchange'
gen
assetratio = abs(asset / `lasset' - 1)
quietly keep
if assetratio <= 0.3
//
将条件分成四句keep if ,有助于加快程序
if _N < 1
{
//
若没有公司满足条件,则直接进入下一循环
restore
continue
}
sort
assetratio
local mstkcd
= stkcd[1]
//
若有多个公司满足条件,则用总资产数最接近的公司
local j = `j'
+ 1
post match
(`j') (`lstkcd') (`lyear') (0)
post match
(`j') (`mstkcd') (`lyear') (1)
restore
}
postclose match
use matchsample, clear
sort stkcd year
merge stkcd year using temp
keep if _merge == 3
drop _merge
sort group dmatch
label define ldmatch 0 "初始样本" 1 "配对样本"
label value dmatch ldmatch
list in 1/20
save lastsample, replace
// 生成模拟数据,非平衡面板数据
clear
set more off
set obs 1000
gen stkcd = _n
gen industry = ceil(10*uniform())
gen exchange = cond(uniform()<0.6, 0, 1) // 60%样本属于上交所SSE
label define lexch 0 "SSE" 1 "SZSE"
label value exchange lexch
gen asset = 100*exp(invnormal(uniform()))
gen control = 10*uniform()
expand 5
drop if uniform() < 0.1
bysort stkcd: gen year = 2010 - _n
sort stkcd year
by stkcd: replace asset = asset * (1 + 0.1*uniform()) if _n > 1
save sample, replace
// 主程序
use sample, clear
sort stkcd year
save temp, replace
capture postclose match
postfile match group stkcd year dmatch using matchsample.dta, replace
local total = _N
local j = 0
forval i = 1/`total' {
}
postclose match
use matchsample, clear
sort stkcd year
merge stkcd year using temp
keep if _merge == 3
drop _merge
sort group dmatch
label define ldmatch 0 "初始样本" 1 "配对样本"
label value dmatch ldmatch
list in 1/20
save lastsample, replace