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

Stata中基于Director条件跨时间标记treated字段的需求及代码修正

分组时间追踪的标记处理问题

需求说明

  • 核心规则:当入职董事的previous值小于企业sale值时,该董事在离职前的所有年份都需标记treated=1
  • 特殊场景:
    • group=4933首次出现于2008年,满足previous < sale & enter == year时,需将标记扩展至该dirid对应的后续所有年份(直到exit年),但2010年的group=4933除外
    • 2010年有新董事(dirid=9587,group=709)入职,符合条件的话,其2010年后的年份需标记treated=1

现有代码问题

尝试的Stata代码错误地将group=4933的2010年也标记为1,不符合需求,仅需对group=709等符合条件的条目正确标记。

现有代码:

sum dirid
forval i=9587 /238853{
    qui replace treated0 =1 if dirid == `i' & enter <year & !missing(previous) 
}

原始数据

clear

input treated   previous    enter   group   dirid   fid year    sale    exit
0   .   2006    2073    41610   11  2008    5.124932    2011
0   .   2007    1139    17558   11  2008    5.124932    2011
1   2.796215    2008    4933    238853  11  2008    5.124932    2011
0   .   2006    2070    41584   11  2008    5.124932    2011
0   .   2006    2075    41649   11  2008    5.124932    2011
0   2.796215    2008    4933    238853  11  2009    5.623706    2011
0   .   2007    1139    17558   11  2009    5.623706    2011
0   .   2006    2073    41610   11  2009    5.623706    2011
0   .   2006    2070    41584   11  2009    5.623706    2011
0   .   2006    2075    41649   11  2009    5.623706    2011
0   .   2009    2078    41712   11  2009    5.623706    2011
0   .   2006    2075    41649   11  2010    5.659729    2011
0   .   2006    2070    41584   11  2010    5.659729    2011
0   .   2007    1139    17558   11  2010    5.659729    2011
0   2.796215    2008    4933    238853  11  2010    5.659729    2011
1   5.123736    2010    709 9587    11  2010    5.659729    2011
0   .   2009    2078    41712   11  2010    5.659729    2011
0   .   2006    2073    41610   11  2010    5.659729    2011
0   .   2007    1139    17558   11  2011    5.513114    2011
0   .   2006    2073    41610   11  2011    5.513114    2011
0   .   2009    2078    41712   11  2011    5.513114    2011
0   2.796215    2008    4933    238853  11  2011    5.513114    2011
0   .   2006    2070    41584   11  2011    5.513114    2011
0   5.123736    2010    709 9587    11  2011    5.513114    2011
0   .   2006    2075    41649   11  2011    5.513114    2011
end

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 02:15:35