Announcement

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

  • Simple reshape help

    Hello friends,

    I have the following data structure:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str55 country int year float value str17 var
    "Afghanistan" 1971 100 "sptinc_p0p50_z_AF"
    "Afghanistan" 1972 100 "sptinc_p0p50_z_AF"
    "Afghanistan" 1973 100 "sptinc_p0p50_z_AF"
    "Afghanistan" 1971 100 "shweal_p0p50_z_AF"
    "Afghanistan" 1972 100 "shweal_p0p50_z_AF"
    "Afghanistan" 1973 100 "shweal_p0p50_z_AF"
    "Albania"     1971 100 "sptinc_p0p50_z_AF"
    "Albania"     1972 100 "sptinc_p0p50_z_AF"
    "Albania"     1973 100 "sptinc_p0p50_z_AF"
    "Albania"     1971 100 "shweal_p0p50_z_AF"
    "Albania"     1972 100 "shweal_p0p50_z_AF"
    "Albania"     1973 100 "shweal_p0p50_z_AF"
    "Algeria"     1971 100 "sptinc_p0p50_z_AF"
    "Algeria"     1972 100 "sptinc_p0p50_z_AF"
    "Algeria"     1973 100 "sptinc_p0p50_z_AF"
    "Algeria"     1971 100 "shweal_p0p50_z_AF"
    "Algeria"     1972 100 "shweal_p0p50_z_AF"
    "Algeria"     1973 100 "shweal_p0p50_z_AF"
    end
    And I wish to achieve the following structure:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str55 country int year float(value sptinc_p0p50_z_AF shweal_p0p50_z_AF)
    "Afghanistan" 1971 100 100 100
    "Afghanistan" 1972 100 100 100
    "Afghanistan" 1973 100 100 100
    "Albania"     1971 100 100 100
    "Albania"     1972 100 100 100
    "Albania"     1973 100 100 100
    "Algeria"     1971 100 100 100
    "Algeria"     1972 100 100 100
    "Algeria"     1973 100 100 100
    end

    Thank you!

  • #2
    Code:
    reshape wide value, i(country year) j(var) string
    rename value* *
    Note: this code will only work (and what you ask for is only possible) if, as is the case in your example data, all of the values of the variable var are legal Stata variable names. That is, they must contain only digits, letters, and underscore characters, and the first character cannot be a digit, and the length must be at most 32 characters. If that is not true in the real data, then you will have to make some compromises and edit the values of var so that they comply with these restrictions. The -strtoname()- function is very helpful for that.

    Comment


    • #3
      Sorry, it worked. Thanks
      Last edited by Gal Rakover; 11 Jun 2022, 12:41.

      Comment

      Working...
      X