老师您好!
学生在改编一个程序,目的是在同行业、同年度中,寻找与原样本ROA在+-20%的配对样本(如果有多个符合条件的配对样本,则选择ROA最接近的),原样本的范围是fs1=1,配对样本的范围是fs1=0
但是运行时,好像进入了死循环。程序如下:
// 生成模拟数据,非平衡面板数据
clear
set more off
set obs 1000
gen id = _n
gen indus = ceil(10*uniform()) // 10个行业
gen ROA = 100*exp(invnormal(uniform())) - 1
gen a001000000 = 100*exp(invnormal(uniform()))
gen control = 10*uniform() // 控制变量,测试用
expand 5
drop if uniform() < 0.1 // 非平衡面板数据
bysort id: gen year = 2010 - _n
sort id year
by id: replace a001000000 = a001000000 * (1 + 0.1*uniform()) if _n > 1
tempfile befor_match //定义暂时性文件befor_match,表示配对之前的文件
gen fs1 = cond(_n>200, 0, 1)
gsort -fs1
count if fs1==1
local num = r(N)
save "`befor_match'", replace
// 主程序
capture postclose match
postfile match group id year dmatch using matchsample.dta, replace
local total = _N
local j = 0
forval k = 1/`num' { //定位原样本的范围
forval i = `num'/`total' { //定位配对样本的范围
preserve
local lid = id[`i'+1]
local lyear = year[`i'+1]
local lindus = indus[`i'+1]
local lROA = ROA[`i'+1]
local la001000000 = a001000000[`i'+1] //资产
quietly keep if indus[`k'] == `lindus'
quietly keep if year[`k'] == `lyear'
gen assetratio = abs(a001000000[`k'] / `la001000000' - 1)
quietly keep if assetratio <= 0.2
// 将条件分成四句keep if ,有助于加快程序
if _N < 1 { //若没有一个公司满足条件,则直接进入下一循环
restore
continue
}
sort ROA // 对ROA排序;如果对assetration排序,则是选取资产最接近的
local mid = id[1] //若有多个公司满足条件,则用ROA最接近的公司
local j = `j' + 1
post match (`j') (`lid') (`lyear') (0)
post match (`j') (`mid') (`lyear') (1)
restore
}
}
postclose match
use matchsample, clear
sort id year
merge id year using "`befor_match'"
keep if _merge == 3
drop _merge
sort group dmatch
label define ldmatch 0 "初始样本" 1 "配对样本"
label value dmatch ldmatch
list in 1/30
请问这个程序需要如何改正,才能达到预期目的?谢谢!