其实我要说的问题比较复杂,一个非平衡面板数据如下
country year value
china 1993 100
china 1994 99
china 1995 98
japan 1993 100
由于无法对数值变量进行xtset,所以要encode country ,gen (country1),然后xtset county1 year
然后再 tsfill ,full,生成平行面板
就会产生country等其它变量的缺漏值,那么如何填充这个country的缺漏值是一个比较麻烦的事情,特别是当处理较多的数据,例如如果数据年份从1960到2010年,而日本只有1993-1999年数据的时候,用replace命令是很难达到效果的
. clear
. input ///
> str20 country year value
country year value
1. china 1993 100
2. china 1994 99
3. china 1995 98
4. japan 1993 100
5. end
.
. egen countryid=group(country)
. xtset countryid year
panel variable: countryid (unbalanced)
time variable: year, 1993 to 1995
delta: 1 unit
.
. tsfill ,full
. list
+-----------------------------------+
| country year value countr~d |
|-----------------------------------|
1. | china 1993 100 1 |
2. | china 1994 99 1 |
3. | china 1995 98 1 |
4. | japan 1993 100 2 |
5. | 1994 . 2 |
|-----------------------------------|
6. | 1995 . 2 |
+-----------------------------------+
. by countryid: replace country=country[_n-1] if country==""
(2 real changes made)
. list
+-----------------------------------+
| country year value countr~d |
|-----------------------------------|
1. | china 1993 100 1 |
2. | china 1994 99 1 |
3. | china 1995 98 1 |
4. | japan 1993 100 2 |
5. | japan 1994 . 2 |
|-----------------------------------|
6. | japan 1995 . 2 |
+-----------------------------------+