Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Stata中以多种方式重塑数据?_Stata_Reshape_Transpose - Fatal编程技术网

如何在Stata中以多种方式重塑数据?

如何在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

我使用的数据集涵盖多个国家、变量和年份。目前,它是这样组织起来的(实际上是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 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 |
     +----------------------------+