简体   繁体   English

如何在Stata中没有J变量的情况下从长到宽重塑特定数据集?

[英]How to reshape a specific dataset from long to wide without a J variable in Stata?

My dataset looks like the following:我的数据集如下所示:

identification number身份证号码 year indicator指标 Data数据
1112000 1112000 2000 2000年 JKL_ADS JKL_ADS 511 511
1112001 1112001 2001 2001年 JKL_ADS JKL_ADS 517 517
1112002 1112002 2002 2002年 JKL_ADS JKL_ADS 721 721
1112003 1112003 2003 2003年 JKL_ADS JKL_ADS 925 925
1112004 1112004 2004 2004年 JKL_ADS JKL_ADS 1092 1092
1112000 1112000 2000 2000年 KLS_DSAK KLS_DSAK 351 351
1112001 1112001 2001 2001年 KLS_DSAK KLS_DSAK 631 631
1112002 1112002 2002 2002年 KLS_DSAK KLS_DSAK 732 732
1112003 1112003 2003 2003年 KLS_DSAK KLS_DSAK 823 823
1112004 1112004 2004 2004年 KLS_DSAK KLS_DSAK 1092 1092

I want to reshape wide so it looks like this instead:我想重塑宽,所以它看起来像这样:

identification number身份证号码 year JKL_ADS JKL_ADS KLS_DSAK KLS_DSAK
1112000 1112000 2000 2000年 511 511 351 351
1112001 1112001 2001 2001年 517 517 631 631
1112002 1112002 2002 2002年 721 721 732 732
1112003 1112003 2003 2003年 925 925 823 823
1112004 1112004 2004 2004年 1092 1092 1092 1092

This is a fairly standard application.这是一个相当标准的应用程序。 You didn't give example data in recommended form, so the details here may need modification by you.您没有以推荐的形式提供示例数据,因此此处的详细信息可能需要您进行修改。

Contrary to the question, indicator serves as an argument to j() .与问题相反, indicator作为j()的参数。

* Example generated by -dataex-. For more info, type help dataex
clear
input long identificationnumber int year str8 indicator int data
1112000 2000 "JKL_ADS"   511
1112001 2001 "JKL_ADS"   517
1112002 2002 "JKL_ADS"   721
1112003 2003 "JKL_ADS"   925
1112004 2004 "JKL_ADS"  1092
1112000 2000 "KLS_DSAK"  351
1112001 2001 "KLS_DSAK"  631
1112002 2002 "KLS_DSAK"  732
1112003 2003 "KLS_DSAK"  823
1112004 2004 "KLS_DSAK" 1092
end

. reshape wide data , i(id year) j(indicator) string
(j = JKL_ADS KLS_DSAK)

Data                               Long   ->   Wide
-----------------------------------------------------------------------------
Number of observations               10   ->   5           
Number of variables                   4   ->   4           
j variable (2 values)         indicator   ->   (dropped)
xij variables:
                                   data   ->   dataJKL_ADS dataKLS_DSAK
-----------------------------------------------------------------------------

. rename (data*) (*)

. l

     +--------------------------------------+
     | identi~r   year   JKL_ADS   KLS_DSAK |
     |--------------------------------------|
  1. |  1112000   2000       511        351 |
  2. |  1112001   2001       517        631 |
  3. |  1112002   2002       721        732 |
  4. |  1112003   2003       925        823 |
  5. |  1112004   2004      1092       1092 |
     +--------------------------------------+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM