【问题标题】:How do I subset a panel data set with three criteria in Stata?如何在 Stata 中使用三个标准对面板数据集进行子集化?
【发布时间】:2021-12-08 07:58:28
【问题描述】:

我有一个如下所示的面板数据集:

person_id year cash
222 2020q4 6,000
222 2021q1 7,000
222 2021q2 8,000
321 2020q4 4,000
321 2021q4 11,000
321 2021q2 15,000

我想对 2021 年第二季度拥有

所以在这个例子中,我想保留 person_id 222 的所有观察结果,但删除 321 的所有观察结果。

感谢任何帮助。谢谢!

【问题讨论】:

  • 交叉发帖reddit.com/r/stata/comments/qcswcb/… 提及交叉发帖总是礼貌的。在最坏的情况下,如果他们回答了已经回答的问题,你会浪费其他人的时间。

标签: dataframe subset stata panel-data


【解决方案1】:

可能有更简洁的方法来做到这一点,但我会将其分为以下三个步骤:

clear
input int person_id str6 year int cash
222 "2020q4"  6000
222 "2021q1"  7000
222 "2021q2"  8000
321 "2020q4"  4000
321 "2021q4" 11000
321 "2021q2" 15000
end

*Test if obs has cash>10000 in 2021 q2
gen subset_obs = (cash > 10000 & year == "2021q2")

*By ID, get the max value in subset_obs to copy 1s to all rows for the ID
bysort person_id : egen subset_id = max(subset_obs)

*Keep only IDs with subset_id is 1
keep if subset_id == 1

【讨论】:

  • 我认为这行得通。非常感谢!
  • 原文的措辞有点别扭。 OP想掉321而保留222,即OP想保留那些现金reddit.com/r/stata/comments/qcswcb/… 有一个并行解决方案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-18
  • 1970-01-01
  • 2017-11-21
  • 1970-01-01
  • 1970-01-01
  • 2020-07-03
  • 2019-09-26
相关资源
最近更新 更多