在Stata中筛选特定数据
我使用的是Stata13,在2000年到2003年的给定时间段内,我必须用不同ID清理面板格式的数据集。我的数据如下所示:在Stata中筛选特定数据,stata,Stata,我使用的是Stata13,在2000年到2003年的给定时间段内,我必须用不同ID清理面板格式的数据集。我的数据如下所示: id year ln_wage 1 2000 2.30 1 2001 2.31 1 2002 2.31 2 2001 1.89 2 2002 1.89 2 2003 2.10 3 2002 1.60 4 2002 2.46 4 2003 2
id year ln_wage
1 2000 2.30
1 2001 2.31
1 2002 2.31
2 2001 1.89
2 2002 1.89
2 2003 2.10
3 2002 1.60
4 2002 2.46
4 2003 2.47
5 2000 2.10
5 2001 2.10
5 2003 2.12
我希望每年只在我的数据集中保留t-1年中出现的个人。这样,我的样本的第一年(2000年)将被删除。我正在寻找如下输出:
2001年:
2002年:
2003年:
关于这一点,这是OP要求的,但现在没有2001年出现的个人也有2000年的数据。否则,
L.ln\u wage
将始终缺少一些值,因为观察值0始终不存在,对它的引用总是会导致缺少值。谢谢你,沃特。考虑到我的样本中有超过3000万个ID,还有其他方法可以做到这一点吗?@phill_9933我在底部包括了另一种方法,但我认为这不会更快。
* Example generated by -dataex-. To install: ssc install dataex
clear
input byte id int year float ln_wage
1 2000 2.3
1 2001 2.31
1 2002 2.31
2 2001 1.89
2 2002 1.89
2 2003 2.1
3 2002 1.6
4 2002 2.46
4 2003 2.47
5 2000 2.1
5 2001 2.1
5 2003 2.12
end
xtset id year
drop if missing(L.ln_wage)
sort year id
list, noobs sepby(year)
+---------------------+
| id year ln_wage |
|---------------------|
| 1 2001 2.31 |
| 5 2001 2.1 |
|---------------------|
| 1 2002 2.31 |
| 2 2002 1.89 |
|---------------------|
| 2 2003 2.1 |
| 4 2003 2.47 |
+---------------------+
// Alternatively, assuming no duplicate years within id exist
bysort id (year): gen todrop = year[_n-1] != year - 1
drop if todrop
id year ln_wage
1 2002 2.31
2 2002 1.89
id year ln_wage
2 2003 2.10
4 2003 2.47
* Example generated by -dataex-. To install: ssc install dataex
clear
input byte id int year float ln_wage
1 2000 2.3
1 2001 2.31
1 2002 2.31
2 2001 1.89
2 2002 1.89
2 2003 2.1
3 2002 1.6
4 2002 2.46
4 2003 2.47
5 2000 2.1
5 2001 2.1
5 2003 2.12
end
xtset id year
drop if missing(L.ln_wage)
sort year id
list, noobs sepby(year)
+---------------------+
| id year ln_wage |
|---------------------|
| 1 2001 2.31 |
| 5 2001 2.1 |
|---------------------|
| 1 2002 2.31 |
| 2 2002 1.89 |
|---------------------|
| 2 2003 2.1 |
| 4 2003 2.47 |
+---------------------+
// Alternatively, assuming no duplicate years within id exist
bysort id (year): gen todrop = year[_n-1] != year - 1
drop if todrop