Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • reshaping from long to wide

    Hi,
    I would like to reshape my dataset below to a wide format such that my first column contains only single values while the second column contains same values. I have done this only for one Age_Group_=1 as shown in the what I want example dataset.
    Thanks,
    Nader

    clear
    input float(sample_label Age_Group_ meanTLE_ meanHLE_ meanPLE_)
    10 1 34.345562 26.949627 .7846588
    10 2 25.502546 19.94874 .782223
    10 3 17.263601 13.420696 .7773955
    11 1 36.008446 28.711313 .7973411
    11 2 26.84264 21.37448 .7962812
    11 3 18.146757 14.363532 .7915174
    12 1 35.14976 26.588387 .7564103
    12 2 26.15379 19.72889 .7543169
    12 3 17.69569 13.256854 .7491353
    13 1 34.352455 26.53276 .7723663
    13 2 25.51025 19.64049 .7699032
    13 3 17.27008 13.210564 .7649361
    14 1 35.999706 29.11195 .8086639
    14 2 26.833727 21.67034 .8075716
    14 3 18.13978 14.565585 .8029606
    15 1 34.3562 26.222485 .7632507
    15 2 25.514814 19.410906 .7607672
    15 3 17.274118 13.054158 .7557027
    16 1 35.14116 27.348135 .7782167
    16 2 26.143465 20.291723 .7761453
    16 3 17.686651 13.640334 .7712016
    17 1 34.346046 26.931656 .7841246
    17 2 25.50309 19.935823 .7816998
    17 3 17.26402 13.411888 .7768664
    18 1 36.006386 28.772425 .7990835
    18 2 26.840286 21.416553 .7979179
    18 3 18.145027 14.392232 .7931737
    19 1 35.14572 26.8311 .7634027
    19 2 26.14876 19.903845 .7611521
    19 3 17.69166 13.375977 .7560384
    20 1 34.350166 26.595913 .7742561
    20 2 25.50756 19.68392 .7716869
    20 3 17.268095 13.24015 .7667372
    21 1 36.00021 29.09467 .8081727
    21 2 26.83426 21.65792 .8070929
    21 3 18.14018 14.557106 .8024755
    22 1 34.351376 26.45478 .7701202
    22 2 25.509 19.57796 .7674892
    22 3 17.269518 13.167942 .7624925
    23 1 35.141567 27.329313 .7776722
    23 2 26.143934 20.27816 .7756125
    23 3 17.687017 13.63109 .770663
    end


    *example what I want for only 50-51

    clear
    input float(sample_label Age_Group_ meanTLE_ meanHLE_ meanPLE_ sample_label_01 Age_Group__01 meanTLE__01 meanHLE__01 meanPLE__01)
    10 1 34.345562 26.949627 .7846588 17 1 34.346046 26.931656 .7841246
    11 1 36.008446 28.711313 .7973411 18 1 36.006386 28.772425 .7990835
    12 1 35.14976 26.588387 .7564103 19 1 35.14572 26.8311 .7634027
    13 1 34.352455 26.53276 .7723663 20 1 34.350166 26.595913 .7742561
    14 1 35.999706 29.11195 .8086639 21 1 36.00021 29.09467 .8081727
    15 1 34.3562 26.222485 .7632507 22 1 34.351376 26.45478 .7701202
    16 1 35.14116 27.348135 .7782167 23 1 35.141567 27.329313 .7776722
    end
    Last edited by Nader Mehri; 26 Mar 2020, 10:43.

  • #2
    This is what a typical reshape wide would give you, and what I would recommend for a wide layout.
    Code:
    . reshape wide meanTLE_ meanHLE_ meanPLE_, i(sample_label) j(Age_Group_)
    (note: j = 1 2 3)
    
    Data                               long   ->   wide
    -----------------------------------------------------------------------------
    Number of obs.                       42   ->      14
    Number of variables                   5   ->      10
    j variable (3 values)        Age_Group_   ->   (dropped)
    xij variables:
                                   meanTLE_   ->   meanTLE_1 meanTLE_2 meanTLE_3
                                   meanHLE_   ->   meanHLE_1 meanHLE_2 meanHLE_3
                                   meanPLE_   ->   meanPLE_1 meanPLE_2 meanPLE_3
    -----------------------------------------------------------------------------
    
    . describe
    
    Contains data
      obs:            14                          
     vars:            10                          
    --------------------------------------------------------------------------------
                  storage   display    value
    variable name   type    format     label      variable label
    --------------------------------------------------------------------------------
    sample_label    float   %9.0g                 
    meanTLE_1       float   %9.0g                 1 meanTLE_
    meanHLE_1       float   %9.0g                 1 meanHLE_
    meanPLE_1       float   %9.0g                 1 meanPLE_
    meanTLE_2       float   %9.0g                 2 meanTLE_
    meanHLE_2       float   %9.0g                 2 meanHLE_
    meanPLE_2       float   %9.0g                 2 meanPLE_
    meanTLE_3       float   %9.0g                 3 meanTLE_
    meanHLE_3       float   %9.0g                 3 meanHLE_
    meanPLE_3       float   %9.0g                 3 meanPLE_
    --------------------------------------------------------------------------------
    Sorted by: sample_label
    
    . list sample_label *_1, clean abbreviate(12)
    
           sample_label   meanTLE_1   meanHLE_1   meanPLE_1  
      1.             10    34.34556    26.94963    .7846588  
      2.             11    36.00845    28.71131    .7973411  
      3.             12    35.14976    26.58839    .7564103  
      4.             13    34.35246    26.53276    .7723663  
      5.             14    35.99971    29.11195    .8086639  
      6.             15     34.3562    26.22248    .7632507  
      7.             16    35.14116    27.34813    .7782167  
      8.             17    34.34605    26.93166    .7841246  
      9.             18    36.00639    28.77242    .7990835  
     10.             19    35.14572     26.8311    .7634027  
     11.             20    34.35017    26.59591    .7742561  
     12.             21    36.00021    29.09467    .8081727  
     13.             22    34.35138    26.45478    .7701202  
     14.             23    35.14157    27.32931    .7776722

    Comment

    Working...
    X