Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.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 - Fatal编程技术网

Stata:将数据集从宽改为长

Stata:将数据集从宽改为长,stata,Stata,假设我有一组国家GDP数据,格式如下: --------------------------------- |年份|国家A |国家B| | 1990 | 128 | 243 | | 1991 | 130 | 212 | | 1992 | 187 | 207 | 我如何使用Stata的重塑命令将其更改为包含国家年份行的长表,如下所示 ---------------------- |国家|年| GDP| |A |

假设我有一组国家GDP数据,格式如下:

---------------------------------
|年份|国家A |国家B|
|  1990  |    128    |    243    |
|  1991  |    130    |    212    |
|  1992  |    187    |    207    |

我如何使用Stata的
重塑
命令将其更改为包含国家年份行的长表,如下所示

----------------------
|国家|年| GDP|
|A | 1990 | 128|
|A | 1991 | 130|
|A | 1992 | 187|
|B | 1990 | 243|
|B | 1991 | 212|

|B | 1992 | 207 |

建议您先尝试自己解决问题。虽然你可能已经试过了,但没有任何迹象表明你已经试过了。对于将来的问题,请发布您尝试的代码,以及为什么它不适合您

下面给出了您的要求:

clear all
set more off

input ///
 Year  CountryA  CountryB  
 1990  128  243  
 1991  130  212  
 1992  187  207 
end

list

reshape long Country, i(Year) j(country) string
rename Country GDP

order country Year GDP
sort country Year
list, sep(0)

注意:这里需要
string
选项,因为存根后缀是字符串(即“A”和“B”)。有关详细信息,请参见
帮助重塑

谢谢!我只是自己想出来的。我遗漏了“字符串”部分:)