这在stata初级视频第 2.7.2 小节有详细讲解。
可以采用 reshape 命令进行处理。
下面给一个简单的例子:
* 原始资料(可能来自网页等途径) -wide-型数据
income unemployee
-----------------------------------+----------------------
id sex 1980 1981 1982 | 1980 1981 1982
-----------------------------------+----------------------
1 0 5000 5500 6000 | 0 1 0
2 1 2000 2200 3300 | 1 0 0
3 0 3000 2000 1000 | 0 0 1
-----------------------------------+----------------------
* 整理后的资料 -wide-型数据
id sex inc80 inc81 inc82 ue80 ue81 ue82
1 0 5000 5500 6000 0 1 0
2 1 2000 2200 3300 1 0 0
3 0 3000 2000 1000 0 0 1
* 最终需要的资料(panel data) -long-型数据
id year sex inc ue
1 1980 0 5000 0
1 1981 0 5500 1
1 1982 0 6000 0
2 1980 1 2000 1
2 1981 1 2200 0
2 1982 1 3300 0
3 1980 0 3000 0
3 1981 0 2000 0
3 1982 0 1000 1
*========codes=============
*- wide --> long
use reshape1.dta, clear
list
reshape long inc ue, i(id) j(year) // sex 不发生变化,无需转换
// j() 选项中填写新的变量名称
list, sepby(id)
replace year = real("19" + string(year))
list, sepby(id)