【问题标题】:How to reshape a specific dataset from long to wide without a J variable in Stata?如何在Stata中没有J变量的情况下将特定数据集从长到宽重塑?
【发布时间】:2021-12-14 17:17:20
【问题描述】:

我的数据集如下所示:

identification number year indicator 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

我想改宽,所以它看起来像这样:

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

【问题讨论】:

    标签: stata stata-macros readstata13


    【解决方案1】:

    这是一个相当标准的应用程序。您没有以推荐的形式提供示例数据,因此此处的详细信息可能需要您修改。

    与问题相反,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 |
         +--------------------------------------+
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多