这个链接,让我成功了。内容如下:
在面板数据中,如果包含两个以上的标识变量,则数据有两种表现形式:一种是长数据,另一种是宽数据。在长宽数据的转换中,所使用到的命令为reshape命令,具体命令语句为:
reshape long stubnames, i(varlist) [options]
reshape wide stubnames, i(varlist) [options]
long表示将宽数据转化为长数据,wide表示将长数据转化成宽数据,stubnames表示需要转化的变量的名称前缀,i(varlist)表示识别变量。options最常用的为j(varname [values]),它表示用于进行长宽变换的变量,通常为时间变量。
我的原始数据格式是700多个城市面板(年份没截全哈):
id popdens_2002 popdens_2003 popdens_2004 popdens_2005 popdens_2006 popdens_2007 popdens_2008
北京 2099 2128 2623 937 1094 1132 1181
天津 831 841 852 863 2590 2912 2858
河北 2039 2057 2107 2132 2444 2376 2375
石家庄 4343 4419 4554 4704 10636 10831 11005
辛集 6141 6141 6245 6245 1621 1327 1309
藁城 3775 3799 3831 3831 1449 1423 1432
晋州 4641 4706 4902 5359 5882 991 1121
。。。。
操作:第一列 是自己加上去的“id popdens_2002 popdens_2003 popdens_2004 popdens_2005 popdens_2006 popdens_2007 popdens_2008 popdens_2009 popdens_2010 popdens_2011 popdens_2012 popdens_2013 popdens_2014 popdens_2015 popdens_2016 popdens_2017 popdens_2018 popdens_2019 popdens_2020”,主要是给各个年份数字,加一个前缀,这个前缀很关键,在后面要用到这个前缀,例如本例中的“popdens_”
代码:
reshape long popdens_, i(id) j(year)
运行结果:
id year popdens_
七台河 2002 279
七台河 2003 285
七台河 2004 294
七台河 2005 291
七台河 2006 6499
七台河 2007 6527
七台河 2008 6570
七台河 2009 6793
七台河 2010 6181
七台河 2011 1287
七台河 2012 1313
七台河 2013 1313
七台河 2014 1314
七台河 2015 5985
七台河 2016 2140
七台河 2017 6071
七台河 2018 6193.787
七台河 2019 6177.5148
七台河 2020 6177.5148
万宁 2002 5208
万宁 2003 5236
万宁 2004 5271
万宁 2005 1322
万宁 2006 1343
万宁 2007 1367
万宁 2008 1393
万宁 2009 1395
。。。等700多个城市