You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在含重复观测的非平衡面板中识别企业年度报告缺口?

识别企业年度报告缺口并创建虚拟变量(Stata环境)

我有一份企业-产品-年度层面的数据集,需要识别1994-2004年间存在年度报告缺口的企业,并为这类企业创建虚拟变量。由于数据存在id-年度重复值,tsfill工具无法直接适用。

示例数据

clear       
input       id  year    sales   product
14  1994    28.9    2
14  1994    67.9    3
14  1994    12.5    9
14  1994    451.8   34
14  1994    27.5    44
14  1994    647.6   45
14  1995    9.7 2
14  1995    33.5    3
14  1995    112.4   9
14  1995    712.2   15
14  1995    902.3   41
14  1995    67.3    45
14  1995    15.1    50
14  1996    6.5 2
14  1996    24.6    3
14  1996    1009.4  5
14  1996    77.1    9
14  1996    76.9    17
14  1996    12.4    45
14  1996    946.3   88
14  1996    15.4    92
14  1997    .7  2
14  1997    63.2    2
14  1997    91.7    3
14  1997    860.8   9
14  1997    12.4    21
14  1997    800.8   32
14  1997    33.7    45
14  1997    41  95
15  1999    .1  44
15  2000    .1  58
15  2001    .4  27
15  2001    .1  95
15  2002    .5  5
15  2002    .1  58
15  2003    .1  17
15  2004    3.5 28
15  2004    .1  39
16  2000    .8  2
16  2001    .6  2
16  2003    .2  2
16  2004    .1  2
16  2004    .1  8
16  2004    2.5 8
end

示例情况说明

  • 企业14在1994-1997年连续报告,无年度缺口;
  • 企业16在2000、2001年报告后,2002年缺失,2003年恢复报告,属于存在报告缺口的企业。

解决方案(Stata代码)

步骤1:标记企业年度报告状态

为每个企业-年度生成唯一标识,标记该年度是否有报告:

bysort id year: gen has_report = 1 if _n == 1
bysort id: replace has_report = has_report[1] if missing(has_report)

步骤2:保留企业-年度唯一观测

由于原数据存在重复的id-year记录,先保留每个组合的唯一行,方便后续时间序列处理:

bysort id year: keep if _n == 1

步骤3:填充目标时间段的所有年度

设置面板数据后,用tsfill填充1994-2004的所有年度,并标记填充的年度为无报告:

xtset id year
tsfill, full
replace has_report = 0 if missing(has_report)

步骤4:识别存在报告缺口的企业

计算每个企业在1994-2004期间的实际报告年度数,与应报告年度数对比,判断是否存在缺口:

bysort id: gen has_gap = 1 if min(year) <= 2004 & max(year) >= 1994 & sum(has_report) != (min(max(year),2004) - max(min(year),1994) + 1)
bysort id: replace has_gap = 0 if missing(has_gap)

步骤5:合并结果回原始数据集

将生成的has_gap虚拟变量合并回原始的企业-产品-年度数据:

preserve
keep id has_gap
bysort id: keep if _n == 1
tempfile gap_info
save `gap_info'
restore
merge m:1 id using `gap_info'
drop _merge

执行完以上步骤后,has_gap变量即为所需的虚拟变量:1表示该企业在1994-2004年间存在报告缺口,0表示无缺口。

内容的提问来源于stack exchange,提问作者user173192

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 14:50:22