Announcement

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

  • How can I reshape my data in this case?

    Hello everyone,

    I have the following data:

    clear

    input str5 id year str5 varname data
    A 1960 GDP 10
    A 1990 GDP 20
    A 2021 GDP 30
    A 1960 Pop 40
    A 1990 pop 50
    A 2021 pop 60
    end

    list, sepby(id)

    And I wonder how I can make it look like this:

    clear

    input str5 id year GDP pop
    A 1960 10 40
    A 1990 20 50
    A 2021 30 60
    end

    I tried several things that however didn't work. Any thoughts?

    Many thanks!



  • #2
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str5 id float year str5 varname float data
    "A" 1960 "GDP" 10
    "A" 1990 "GDP" 20
    "A" 2021 "GDP" 30
    "A" 1960 "Pop" 40
    "A" 1990 "pop" 50
    "A" 2021 "pop" 60
    end
    
    replace varname= lower(varname)
    reshape wide data, i(id year) j(varname) string
    rename data* *
    Res.:

    Code:
    . l
    
         +-----------------------+
         | id   year   gdp   pop |
         |-----------------------|
      1. |  A   1960    10    40 |
      2. |  A   1990    20    50 |
      3. |  A   2021    30    60 |
         +-----------------------+

    Comment


    • #3
      Thanks so much Andrew!! It worked perfectly!!!

      Comment

      Working...
      X