我有一個如下所示的面板資料集:
| person_id | 年 | 現金 |
|---|---|---|
| 222 | 2020Q4 | 6,000 |
| 222 | 2021Q1 | 7,000 |
| 222 | 2021Q2 | 8,000 |
| 321 | 2020Q4 | 4,000 |
| 321 | 2021Q4 | 11,000 |
| 321 | 2021Q2 | 15,000 |
我想對 2021q2 中 <10,000 的 person_id 組的資料集進行子集化,并保留他們之前的所有觀察結果,并洗掉到 2021q2 沒有那么多現金的 person_id。如何通過上述三個變數對這些資料進行子集化?
所以在這個例子中,我想保留 person_id 222 的所有觀察,但洗掉 321 的所有觀察。
感謝任何幫助。謝謝!
uj5u.com熱心網友回復:
可能有一種更簡潔的方法來做到這一點,但我將其分為以下三個步驟:
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/331181.html
