如何在Stata中以多种方式重塑数据?
我使用的数据集涵盖多个国家、变量和年份。目前,它是这样组织起来的(实际上是30年,每个国家有5个不同的变量): 我希望将数据重新排列,如下所示:如何在Stata中以多种方式重塑数据?,stata,reshape,transpose,Stata,Reshape,Transpose,我使用的数据集涵盖多个国家、变量和年份。目前,它是这样组织起来的(实际上是30年,每个国家有5个不同的变量): 我希望将数据重新排列,如下所示: country year A B C USA 1995 5 1 0 USA 1996 4 2 4 USA 1997 1 1 2 UK 1995 2 2 2 UK 1996 4 8 4 UK 1997 9 4 1 我尝试使用重塑长年,I(国家)j(年份)但收到以下错误消息: variable id does not uniquely identify
country year A B C
USA 1995 5 1 0
USA 1996 4 2 4
USA 1997 1 1 2
UK 1995 2 2 2
UK 1996 4 8 4
UK 1997 9 4 1
我尝试使用重塑长年,I(国家)j(年份)
但收到以下错误消息:
variable id does not uniquely identify the observations
Your data are currently wide. You are performing a reshape long. You specified i(country) and j(year). In
the current wide form, variable country should uniquely identify the observations.
我认为这是因为国家
不是唯一的长变量?(measure
也是吗?)
除了解决这个问题和安排年份长度而不是宽度之外,我认为这个命令不会完成将不同变量(A、B、C)作为列标题移动到宽格式的其他任务
我是否需要使用单独的
重塑宽度
命令?或者是否有某种方法可以扩展命令以同时执行这两项操作 这是一个双重的重塑
。至少可以这样做;此外,这似乎很重要,因为年份需要很长,而不是很长,衡量标准需要很宽,而不是很长,所以这两个问题都有味道
经济发展数据通常是这样到达的。事实上,这个问题已经产生了至少一篇专门的短文
.
您的数据示例很有帮助,几乎立即就可以使用,但请阅读Stata标签和help dataex
(如有必要,请先使用ssc install dataex
安装dataex
)
另请参见,其中包括Stata帮助和手动输入之外的一些提示
Stata中的搜索重塑会指向这些资源
clear
input str3 country str1 measure yr1995 yr1996 yr1997
USA A 5 4 1
USA B 1 2 1
USA C 0 4 2
UK A 2 4 9
UK B 2 8 4
UK C 2 4 1
end
reshape long yr, i(country measure) j(year)
reshape wide yr, i(country year) j(measure) string
rename (yr*) *
list, sepby(country)
+----------------------------+
| country year A B C |
|----------------------------|
1. | UK 1995 2 2 2 |
2. | UK 1996 4 8 4 |
3. | UK 1997 9 4 1 |
|----------------------------|
4. | USA 1995 5 1 0 |
5. | USA 1996 4 2 4 |
6. | USA 1997 1 1 2 |
+----------------------------+
clear
input str3 country str1 measure yr1995 yr1996 yr1997
USA A 5 4 1
USA B 1 2 1
USA C 0 4 2
UK A 2 4 9
UK B 2 8 4
UK C 2 4 1
end
reshape long yr, i(country measure) j(year)
reshape wide yr, i(country year) j(measure) string
rename (yr*) *
list, sepby(country)
+----------------------------+
| country year A B C |
|----------------------------|
1. | UK 1995 2 2 2 |
2. | UK 1996 4 8 4 |
3. | UK 1997 9 4 1 |
|----------------------------|
4. | USA 1995 5 1 0 |
5. | USA 1996 4 2 4 |
6. | USA 1997 1 1 2 |
+----------------------------+