Announcement

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

  • Reshape problem

    Hello,

    I am trying to reshape my data to long; it is currently organized as follows: each observation is a certain variable for a certain country and the variables (columns) are years (see before dataex).
    I want to achieve a long data with variables country, year, GDP per capita, Natural Rents (see desired dataex); but instead I get variables: country, seriescode, year, value (see after dataex).

    Thank you!

    This is the code I used:
    Code:
    reshape long yr, i(countrycode seriescode) j(year)
    Before dataex:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str3 countrycode str17 seriescode float(yr1971 yr1972 yr1973 yr1974)
    "AFG" "GDPPC" .3739563  .4352624  .8714833 1.1527195
    "AFG" "NR"    159.5675 135.31723 143.14465 173.65363
    "ALB" "GDPPC"        .         .         .         .
    "ALB" "NR"           .         .         .         .
    end
    After dataex:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str3 countrycode str17 seriescode int year float yr
    "AFG" "GDPPC" 1971  .3739563
    "AFG" "GDPPC" 1972  .4352624
    "AFG" "GDPPC" 1973  .8714833
    "AFG" "GDPPC" 1974 1.1527195
    "AFG" "NR"    1971  159.5675
    "AFG" "NR"    1972 135.31723
    "AFG" "NR"    1973 143.14465
    "AFG" "NR"    1974 173.65363
    "ALB" "GDPPC" 1971         .
    "ALB" "GDPPC" 1972         .
    "ALB" "GDPPC" 1973         .
    "ALB" "GDPPC" 1974         .
    "ALB" "NR"    1971         .
    "ALB" "NR"    1972         .
    "ALB" "NR"    1973         .
    "ALB" "NR"    1974         .
    end
    Desired dataex:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str3 countrycode int year float(gdppc nr)
    "AFG" 1971  .3739563  159.5675
    "AFG" 1972  .4352624 135.31723
    "AFG" 1973  .8714833 143.14465
    "AFG" 1974 1.1527195 173.65363
    "ALB" 1971         .         .
    "ALB" 1972         .         .
    "ALB" 1973         .         .
    "ALB" 1974         .         .
    end
    Last edited by Gal Rakover; 25 Mar 2022, 12:56.

  • #2
    You were a third of the way there:
    Code:
    reshape long yr, i(countrycode seriescode) j(year)
    reshape wide yr, i(countrycode year) j(seriescode) string
    rename yr* *
    Now, it is possible that you were really only a fourth of the way there. Perhaps some of the seriescode values are not legal Stata variable names. For example, if they contain embedded blanks, or non-alphanumeric characters (other than _), or start with a number. In that case, you have to add a line of code
    Code:
    replace seriescode = strtoname(seriescode)
    before the second -reshape- command.

    Comment


    • #3
      Many thanks

      Comment

      Working...
      X