Announcement

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

  • reshape long with many variables

    Hello everybody,

    I need to reshape wide data to long. The issue is that I have two variables, but they aren't both in the wide format. I try to be clearer: for each individual (country) I have two records: one for the variable A and the second one for the variable B, so I do not have the variables on the same record (that is I do not have the variables in the wide format). Obviously, when I run:


    reshape long newvar, i(country description) j(time)


    I get both the variable in the same column vector "newvar". How can I split these data in order to generate two variables (two vectors, two columns)?

    Thank you.

    (STATA 12 user)

  • #2
    Welcome to the Stata Forum/ Statalist.

    Please take some time to read the FAQ, particularly the topic about sharing data/command/ output.

    You may use a toy example as well.

    In short, data can be shared by using CODE delimiters or (in your case, since you have an earlier version of Stata) by installing the SSC dataex.

    The odds are you’ll get an insightful reply, provided you share data to work with.
    Best regards,

    Marcos

    Comment


    • #3
      Thank you, Marcos.
      This is how my data look like before reshaping:

      clear
      input str72 country str24 units float(v6 v7 v8 v9) double(v10 v11 v12 v13)
      "Austria" "Percent change" 2.048 -3.613 1.329 1.708 1.806 1.999 2.086 2.197
      "Austria" "Percent of potential GDP" -1.804 -2.958 -4.279 -4.092 . . . .
      "Belgium" "Percent change" .832 -3.006 1.153 1.336 1.605 1.707 1.83 1.876
      "Belgium" "Percent of potential GDP" -2.112 -4.786 -4.257 -3.425 . . . .
      "Czech Republic" "Percent change" 2.464 -4.287 1.675 2.629 3.5 3.5 3.5 3.5
      "Czech Republic" "Percent of potential GDP" . . . . . . . .
      "Denmark" "Percent change" -.87 -5.071 1.2 1.557 2.567 2.634 2.297 2.344
      "Denmark" "Percent of potential GDP" 3.692 .038 -1.726 -1.494 . . . .



      and, as I wrote before, I would like to have these two variables (the one measured in "percent change" and the one measured in "percent of potential GDP") in a long form and, most importantly, in two separate column vectors.

      Thanks to anyone will help.

      Comment


      • #4
        Looks horrible, is soluble. Your variable units clearly isn't suitable for variable names. What I do here is shunt the distinct values into value labels. But then they wouldn't survive my second reshape here unless I push them into a file for safe-keeping and then later copy each value label to a new variable label.

        You should know what constant to add to get the years correct.

        Code:
        clear
        input str72 country str24 units float(v6 v7 v8 v9) double(v10 v11 v12 v13)
        "Austria" "Percent change" 2.048 -3.613 1.329 1.708 1.806 1.999 2.086 2.197
        "Austria" "Percent of potential GDP" -1.804 -2.958 -4.279 -4.092 . . . .
        "Belgium" "Percent change" .832 -3.006 1.153 1.336 1.605 1.707 1.83 1.876
        "Belgium" "Percent of potential GDP" -2.112 -4.786 -4.257 -3.425 . . . .
        "Czech Republic" "Percent change" 2.464 -4.287 1.675 2.629 3.5 3.5 3.5 3.5
        "Czech Republic" "Percent of potential GDP" . . . . . . . .
        "Denmark" "Percent change" -.87 -5.071 1.2 1.557 2.567 2.634 2.297 2.344
        "Denmark" "Percent of potential GDP" 3.692 .038 -1.726 -1.494 . . . .
        end 
        
        encode units, gen(UNITS) 
        reshape long v , i(country units) j(year) 
        
        label save UNITS using units.do
        drop units 
        reshape wide v, i(country year) j(UNITS) 
        
        do units 
        local j = 1 
        foreach v of var v* { 
            label var `v' "`: label UNITS `j''" 
            local ++j 
        }
        
        list 
        
             +-------------------------------------------------+
             |        country   year           v1           v2 |
             |-------------------------------------------------|
          1. |        Austria      6    2.0480001       -1.804 |
          2. |        Austria      7   -3.6129999   -2.9579999 |
          3. |        Austria      8        1.329   -4.2789998 |
          4. |        Austria      9    1.7079999       -4.092 |
          5. |        Austria     10        1.806            . |
             |-------------------------------------------------|
          6. |        Austria     11        1.999            . |
          7. |        Austria     12        2.086            . |
          8. |        Austria     13        2.197            . |
          9. |        Belgium      6    .83200002       -2.112 |
         10. |        Belgium      7       -3.006   -4.7859998 |
             |-------------------------------------------------|
         11. |        Belgium      8        1.153       -4.257 |
         12. |        Belgium      9        1.336       -3.425 |
         13. |        Belgium     10        1.605            . |
         14. |        Belgium     11        1.707            . |
         15. |        Belgium     12         1.83            . |
             |-------------------------------------------------|
         16. |        Belgium     13        1.876            . |
         17. | Czech Republic      6        2.464            . |
         18. | Czech Republic      7   -4.2870002            . |
         19. | Czech Republic      8        1.675            . |
         20. | Czech Republic      9    2.6289999            . |
             |-------------------------------------------------|
         21. | Czech Republic     10          3.5            . |
         22. | Czech Republic     11          3.5            . |
         23. | Czech Republic     12          3.5            . |
         24. | Czech Republic     13          3.5            . |
         25. |        Denmark      6         -.87    3.6919999 |
             |-------------------------------------------------|
         26. |        Denmark      7   -5.0710001         .038 |
         27. |        Denmark      8          1.2       -1.726 |
         28. |        Denmark      9        1.557       -1.494 |
         29. |        Denmark     10        2.567            . |
         30. |        Denmark     11        2.634            . |
             |-------------------------------------------------|
         31. |        Denmark     12        2.297            . |
         32. |        Denmark     13        2.344            . |
             +-------------------------------------------------+

        Comment


        • #5
          Dear Nick, it works perfectly!! Thank you.

          Comment

          Working...
          X