Announcement

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

  • how do reshaping my data

    I have a panel data which is structured in the long format, however i want to transform it to a wide format where the time variable will be on the row while the cross sectional units with all the variables for each countries will be under their country clusters. just like the attachment added
    Attached Files

  • #2
    There are three different possible long arrangements for the kind of data shown. I can't tell which you have. Please post back with example data from your Stata data set. Be sure to use the -dataex- command to do that. If you are running version 18, 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment


    • #3

      The data is posted below


      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input int year str18 country byte cid float(co2kt co2percap) long efc_total_gha float(efc_total_percapita informal renewcon regquality corruption)
      1996 "Argentina" 1 122545.9 3.46279 124924291 3.53 25.5  8.6  .518067 -.101317
      1997 "Argentina" 1 126118.7  3.5213 114942205 3.21 24.9  9.7  .519706 -.152399
      1998 "Argentina" 1 132669.9 3.66156 123480790 3.41 24.6  9.6  .521346 -.203482
      1999 "Argentina" 1   134502  3.6696 117161992  3.2 25.2  8.8  .351819 -.187945
      2000 "Argentina" 1 132265.5 3.56792 116673328 3.15 25.4  9.8  .182292 -.172408
      2001 "Argentina" 1 125255.2 3.34188 117305163 3.13   26 11.5 -.349212 -.294732
      2002 "Argentina" 1 117462.1 3.10049 112160799 2.96 26.5 11.6 -.880717 -.417056
      2003 "Argentina" 1 127653.5 3.33489 121787215 3.18 25.8 10.8 -.691476 -.479977
      2004 "Argentina" 1 141376.4 3.65608 120226619 3.11 25.1  9.3 -.698035 -.414409
      2005 "Argentina" 1 145995.4 3.73672 124802049 3.19 24.7  9.6 -.525074 -.377738
      2006 "Argentina" 1 154917.5 3.92426 129793366 3.29 24.3  9.2 -.614567 -.331239
      2007 "Argentina" 1 162820.2 4.08315 141429570 3.55 23.7  7.7 -.635694 -.372122
      2008 "Argentina" 1 167231.8 4.15238 147014050 3.65 23.5  7.7 -.702629 -.434875
      2009 "Argentina" 1 156567.5 3.84835 103327085 2.54 24.4  8.6 -.830837 -.445475
      2010 "Argentina" 1 167226.3 4.09984 140778122 3.43 23.6  8.8 -.742941  -.35523
      2011 "Argentina" 1 176641.6 4.28103 141578532 3.41 23.3  8.8 -.701964 -.364098
      2012 "Argentina" 1 177955.3 4.26411 140959781 3.36 23.6  8.6 -.897687 -.445909
      2013 "Argentina" 1 183255.7 4.34225 154362839 3.64 23.5  8.9 -.928135 -.434016
      2014 "Argentina" 1 179600.7 4.20911 151518529 3.54 23.8  9.8 -1.06615 -.549443
      2015 "Argentina" 1   185550 4.30191 154895873 3.58 23.9  9.4 -.898022 -.580869
      2016 "Argentina" 1 183158.7 4.20182 137069557 3.14   24  9.4 -.347571 -.298965
      2017 "Argentina" 1 179267.3 4.07011 144210379 3.27 23.9 10.4   -.1963 -.308798
      2018 "Argentina" 1 176894.6 3.97565 136979487 3.08 24.1 10.5 -.195978 -.098668
      2019 "Argentina" 1   168162 3.74203 135365170 3.03 24.3 10.7  -.45308  -.10694
      2020 "Argentina" 1 154535.9 3.40562 124821797 2.77 25.3  9.8 -.498344 -.163781
      1996 "Brazil"    2 262414.1 1.58045 501273700 3.02 40.3 44.5  .296933 -.018579
      1997 "Brazil"    2   281335 1.66918 498014338 2.95 40.2 43.7  .312115  .011448
      1998 "Brazil"    2 290587.2 1.69894 497131629 2.91 40.9 43.8  .327298  .041475
      1999 "Brazil"    2 300958.7 1.73477 513028400 2.96 40.8   44   .29287  .027357
      2000 "Brazil"    2 313670.8  1.7835 529358077 3.01 39.8 42.7  .258442  .013239
      2001 "Brazil"    2 319375.7 1.79211 514012309 2.88 40.2 41.3  .220505  .047591
      2002 "Brazil"    2 317756.5 1.76065 505608234  2.8 40.4 42.9  .182567  .081942
      2003 "Brazil"    2 310808.1 1.70185 518405690 2.84 39.7 45.1  .196701  .090239
      2004 "Brazil"    2 328517.3 1.77844 488888125 2.65   39 45.2  .037414 -.027269
      2005 "Brazil"    2 331689.1 1.77566 474812658 2.54 39.2 46.1  .036901 -.153748
      2006 "Brazil"    2 335624.6 1.77748 482280478 2.55   39 46.7 -.041273  -.12048
      2007 "Brazil"    2 352555.8 1.84798 508840150 2.67 38.3 47.2 -.022835 -.083989
      2008 "Brazil"    2 373633.1 1.93922 543820830 2.82 37.6   47  .077027  .003437
      2009 "Brazil"    2 350000.9 1.79933 520557105 2.68 38.4 48.9  .127607 -.079986
      2010 "Brazil"    2 397931.1 2.02661 570933267 2.91 37.2 46.8   .24662  .043806
      2011 "Brazil"    2 418295.4 2.11063 607141103 3.06 36.5 45.2  .258022  .168382
      2012 "Brazil"    2 454232.9 2.27142 607187501 3.04 36.2 43.5  .173244 -.046854
      2013 "Brazil"    2 486844.7 2.41345 615999512 3.05 36.2 42.3   .14629   -.0977
      2014 "Brazil"    2   511618 2.51459 607387746 2.99 36.4 41.7  -.02822 -.365746
      2015 "Brazil"    2 485344.1 2.36536 579158778 2.82 37.8 43.7  -.18191 -.457373
      2016 "Brazil"    2 447077.3 2.16126 565004402 2.73 39.2 45.4 -.189952 -.445451
      2017 "Brazil"    2 455684.8 2.18549 590014222 2.83 39.2 45.3 -.130867 -.559336
      2018 "Brazil"    2 433838.8 2.06426 569242027 2.71 39.1 46.9 -.293126 -.480606
      2019 "Brazil"    2   434318 2.05077 551386524  2.6 38.9 47.5 -.121404 -.407679
      2020 "Brazil"    2 414138.8 1.94252 531925303  2.5   40   50 -.111011 -.428935
      1996 "Chile"     3  44896.3 3.07183  57234275 3.92 19.4 32.2  1.54276  1.45412
      1997 "Chile"     3  51285.2 3.46304  53303906  3.6 19.3   30  1.45934  1.37582
      1998 "Chile"     3  52013.7 3.46833  53774689 3.59 19.3 29.8  1.37592  1.29751
      1999 "Chile"     3  54430.3 3.58651  54354066 3.58   20 29.1  1.39032  1.41886
      2000 "Chile"     3  50424.5  3.2846  57801994 3.77 19.8 31.4  1.40473  1.54022
      2001 "Chile"     3  48425.1 3.11937  54099439 3.48 19.7 32.1  1.36161  1.50969
      2002 "Chile"     3  49121.4 3.12999  53768911 3.43 19.7 33.1   1.3185  1.47916
      2003 "Chile"     3  50200.6 3.16541  54931354 3.46 19.5 31.6  1.33344   1.1671
      2004 "Chile"     3  54972.1  3.4319  59818039 3.73 19.3 31.4  1.36798  1.30586
      2005 "Chile"     3  55968.9 3.46014  60915730 3.77   19 32.3  1.31246  1.41653
      2006 "Chile"     3  57148.1  3.4986  61986431 3.79 18.8 32.9   1.4239  1.41256
      2007 "Chile"     3    64574 3.91463  65660461 3.98 18.5 30.5  1.48559  1.34515
      2008 "Chile"     3  67991.6 4.08077  70376536 4.22 18.7 30.8  1.50249  1.31799
      2009 "Chile"     3  65450.6 3.88813  62567417 3.72 19.2 31.5   1.4274  1.32123
      2010 "Chile"     3  69749.1 4.10188  73908692 4.35 18.7   27  1.41517  1.44163
      2011 "Chile"     3  76470.5  4.4528  78600803 4.58 18.4 28.8  1.44053  1.47659
      2012 "Chile"     3  78412.6  4.5216  77553502 4.47 18.2 30.3   1.5362  1.54359
      2013 "Chile"     3  82998.5 4.74008  79115536 4.52   18 30.2  1.48287  1.49896
      2014 "Chile"     3  76560.3 4.32859  74899872 4.23 18.2   27  1.48459  1.44106
      2015 "Chile"     3  82260.4 4.60324  79182214 4.43 18.2 25.1  1.31984  1.18317
      2016 "Chile"     3  86504.7 4.78353  80765285 4.47 18.3 24.6  1.36391  1.02332
      2017 "Chile"     3  87136.9  4.7438  84288417 4.59 18.4 24.1   1.3252  1.00148
      2018 "Chile"     3  86581.3 4.62966  85415030 4.57 18.4 25.5  1.30045  .958149
      2019 "Chile"     3  91915.4 4.82762  87062798 4.57 18.4 25.3  1.15849  .979157
      2020 "Chile"     3  84827.8 4.39515  80270701 4.16 19.4 26.7  .948648  1.10673
      1996 "Colombia"  4  59253.3  1.6175  83053637 2.27 37.9 33.6 -.105488 -.512254
      1997 "Colombia"  4    64837 1.73863  83832977 2.25 37.7 25.7 -.136833 -.517978
      1998 "Colombia"  4  65071.4 1.71491  83821667 2.21 38.1 25.8 -.168178 -.523702
      1999 "Colombia"  4  55922.1 1.44932  79306511 2.06   39   29 -.038332 -.464982
      2000 "Colombia"  4  58995.5 1.50441  84043751 2.14 39.1 27.9  .091515 -.406262
      2001 "Colombia"  4  59588.3 1.49577  84281217 2.12 38.9 27.1  .034126 -.335297
      2002 "Colombia"  4    57373 1.41823  83302440 2.06 38.9 28.6 -.023263 -.264331
      2003 "Colombia"  4    56716 1.38137  84255015 2.05 38.2 30.6 -.126519 -.204286
      2004 "Colombia"  4  56608.9 1.35921  82350351 1.98   38   29 -.086416 -.140631
      2005 "Colombia"  4    58187 1.37815  85350359 2.02 37.5 28.9 -.016364 -.145389
      2006 "Colombia"  4    58193 1.36051  86097592 2.01   37 30.6  .106225 -.175138
      2007 "Colombia"  4  59709.9 1.37877  86640043    2 36.5   31  .284263  -.27916
      2008 "Colombia"  4  60298.1 1.37619  85966633 1.96 36.2 30.8   .27239 -.261543
      2009 "Colombia"  4  62670.6 1.41424  87474494 1.97 36.6 31.4  .088282 -.337012
      2010 "Colombia"  4  64146.1 1.43132  93164434 2.08 36.2 30.6  .157254 -.423333
      2011 "Colombia"  4  69771.1  1.5399  93046344 2.05 35.5 29.6  .279602 -.317528
      2012 "Colombia"  4  70223.2 1.53385  90374233 1.97 35.2 29.6  .326595 -.422298
      2013 "Colombia"  4  77725.4 1.68099  93670736 2.03 34.8   32  .339824 -.441053
      2014 "Colombia"  4    80091 1.71582  98071646  2.1 34.8 32.1  .444412 -.397179
      2015 "Colombia"  4  81017.2 1.71939  99456686 2.11 34.5 31.1   .40772  -.35224
      2016 "Colombia"  4  83526.2  1.7538  99766227 2.09 34.4 30.5  .339025 -.384033
      2017 "Colombia"  4    75674 1.56508 106849215 2.21 34.6 32.2  .257449 -.429445
      2018 "Colombia"  4  79355.2 1.61039 109966439 2.23 34.7 30.4  .224889 -.339971
      2019 "Colombia"  4  79186.7 1.57782 114308525 2.28 34.6 31.5  .317911 -.283157
      2020 "Colombia"  4  79057.6 1.55226 107742269 2.12 36.7   32  .225666 -.251916
      end
      format %ty cid

      Comment


      • #4
        The exact result you want is not possible in a Stata data set, because Stata does not support two levels of variable names, the parameter in one level and the country below it. The closest you can come is a sequence of variables whose names begin with the country followed by the name of the parameter, in alphabetical order. The following code will do that:
        Code:
        replace country = "_" + strtoname(country)
        drop cid
        
        ds year country, not
        reshape wide `r(varlist)', i(year) j(country) string
        
        ds year, not
        order `r(varlist)', alphabetic after(year)
        Note: I don't know if what you've shown includes all of the variables, are just some of them. It is possible that there will be some combinations of original variable name and country name that, when put together with an underscore between them, result in a proposed variable name that exceeds Stata's 32 character limit. The -reshape- command will give you an error message if that happens. The remedy will be to either shorten the name of the country or shorten the original variable name so that the combination fits within the 32 character limit, and then try again.

        Comment

        Working...
        X