Announcement

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

  • Reshaping data from wide to long format

    Dear all,
    I have panel data on carbon emissions for 183 countries covering 1990-2018. A small part of the dataset is as below:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str6 year double(Australia Austria Armenia Azerbaijan Turkey Benin Bhutan Laos Burundi CentralAfricanRepublic Yemen)
    "Income"                  1                  1                    2                  2                   2                   3                   3                   3                   4                 4                   4
    "1990"   1.4009580754860715 .31158345073054244 -.056947682281464636 .38808782107675716  -.6428645836574215 -2.0291057127961887 -1.2286454701669927 -1.3713493476998375  -2.599403276842627  .531356793310118 -1.9293939370740694
    "1991"   1.4017048778023369  .3688622593648091 -.008365352730666272  .4973178793456069  -.6342507225365416  -2.022281290978517 -1.1788831083665428  -1.403634081988587  -2.575200337901745 .5411225396088792 -1.8398612148659843
    "1992"    1.422018875273942 .31132435433944267   -.4827371985156023 .18598752230323617  -.5872644527564024  -2.004678326561717 -1.1590274235047704 -1.3708956876935683  -2.555425696384746 .5553293440340423 -1.8092141920964184
    "1993"   1.4264428996717564  .3168462347564737   -.9852057131846497  .3089575446556281  -.5588529514185019  -2.003127426157066 -1.1665642934901275  -1.386053980402758 -2.5537047319034545  .554478616712387 -1.9068302641720674
    "1994"    1.427731181930419  .3201372622873946   -1.300818725792398  .2035469114656782   -.590832974786879 -1.9764934253920454  -1.133475547040578 -1.3679445383421476 -2.5847044486765953 .6090933049624437 -1.9216177762478075
    "1995"    1.440412182419488  .3601232980999477   -1.177761055248301  .1682641769988848  -.5329656673650858  -2.026666167328151 -1.0475866232078348 -1.3820555318439447 -2.5581774227323324 .6048705009940843 -1.8537820642355318
    "1996"    1.416024159128561  .3989527790216808  -1.3412229823840942  .0729209285039712 -.48217376915098337 -1.8537160201827614 -1.0438671815524536 -1.4139843340443485  -2.583603410846141 .6300820148214902 -1.8564165053837538
    "1997"   1.4212725506053254 .38583921821620504   -1.226961662593643 .06543942114615722  -.4615944381428231 -1.8237837247721291  -.9950841360959963 -1.4103413405920122  -2.583630051212485 .6186672414955544 -1.8098678215448663
    "1998"   1.4667634623856478 .38853486851753277  -1.2279612356916474 .07426742040153854 -.46866325966101835 -1.8040570863833933 -1.0137424983944028 -1.4836358604216402 -2.5867248957247053 .6533745367396131 -1.7584319333921492
    end
    The first row shows the countries and the second row shows the income level of that country (e.g., 1 high-income countries, 4 for least-income countries). I am trying to convert the data into long format. If I did not have the income levels, I would convert the data as below:
    Code:
     foreach x of varlist Australia- Yemen {
            rename `x' co2`x'
            }    
            reshape long co2, i(year) j(Country, string)
    My question is how can I also add the second row (income levels) in the above coding so that it also gets converted into long format. In the long format, I want to have the years, countries and also income levels in columns.
    Many thanks,
    Last edited by yilmaz kemal; 24 May 2023, 10:16. Reason: Reshape, long form

  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str6 year double(Australia Austria Armenia Azerbaijan Turkey Benin Bhutan Laos Burundi CentralAfricanRepublic Yemen)
    "Income"                  1                  1                    2                  2                   2                   3                   3                   3                   4                 4                   4
    "1990"   1.4009580754860715 .31158345073054244 -.056947682281464636 .38808782107675716  -.6428645836574215 -2.0291057127961887 -1.2286454701669927 -1.3713493476998375  -2.599403276842627  .531356793310118 -1.9293939370740694
    "1991"   1.4017048778023369  .3688622593648091 -.008365352730666272  .4973178793456069  -.6342507225365416  -2.022281290978517 -1.1788831083665428  -1.403634081988587  -2.575200337901745 .5411225396088792 -1.8398612148659843
    "1992"    1.422018875273942 .31132435433944267   -.4827371985156023 .18598752230323617  -.5872644527564024  -2.004678326561717 -1.1590274235047704 -1.3708956876935683  -2.555425696384746 .5553293440340423 -1.8092141920964184
    "1993"   1.4264428996717564  .3168462347564737   -.9852057131846497  .3089575446556281  -.5588529514185019  -2.003127426157066 -1.1665642934901275  -1.386053980402758 -2.5537047319034545  .554478616712387 -1.9068302641720674
    "1994"    1.427731181930419  .3201372622873946   -1.300818725792398  .2035469114656782   -.590832974786879 -1.9764934253920454  -1.133475547040578 -1.3679445383421476 -2.5847044486765953 .6090933049624437 -1.9216177762478075
    "1995"    1.440412182419488  .3601232980999477   -1.177761055248301  .1682641769988848  -.5329656673650858  -2.026666167328151 -1.0475866232078348 -1.3820555318439447 -2.5581774227323324 .6048705009940843 -1.8537820642355318
    "1996"    1.416024159128561  .3989527790216808  -1.3412229823840942  .0729209285039712 -.48217376915098337 -1.8537160201827614 -1.0438671815524536 -1.4139843340443485  -2.583603410846141 .6300820148214902 -1.8564165053837538
    "1997"   1.4212725506053254 .38583921821620504   -1.226961662593643 .06543942114615722  -.4615944381428231 -1.8237837247721291  -.9950841360959963 -1.4103413405920122  -2.583630051212485 .6186672414955544 -1.8098678215448663
    "1998"   1.4667634623856478 .38853486851753277  -1.2279612356916474 .07426742040153854 -.46866325966101835 -1.8040570863833933 -1.0137424983944028 -1.4836358604216402 -2.5867248957247053 .6533745367396131 -1.7584319333921492
    end
    
    rename (Australia-Yemen) (x=)
    
    reshape long x, i(year) j(country) string 
    
    gen income = x if year == "Income"
    bysort country (income) : replace income = income[1]
    drop if year == "Income"
    destring year, replace 
    
    sort country year 
    list

    Comment


    • #3
      Nick, this is beautiful. I cannot thank you enough.

      Comment

      Working...
      X