Announcement

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

  • Reshaping data

    Dear All,

    Below an example of my dataset:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str42 country str105 name float(y1970 y1971 y1972 y1973 y1974 y1975)
    "Afghanistan"         "clean_fuel"           .         .         .         .         .         .
    "Afghanistan"         "agr_for_fish"         .         .         .         .         .         .
    "Afghanistan"         "article"              .         .         .         .         .         .
    "Afghanistan"         "agr_land"      58.27085  58.31685  58.33525  58.33709  58.33878  58.33878
    "Albania"             "agr_land"      44.89051  43.79562  42.84671  41.82482  40.94891  39.41606
    "Albania"             "agr_for_fish"         .         .         .         .         .         .
    "Albania"             "clean_fuel"           .         .         .         .         .         .
    "Albania"             "article"              .         .         .         .         .         .
    "Algeria"             "agr_for_fish"         .         .         .         .         .         .
    "Algeria"             "clean_fuel"           .         .         .         .         .         .
    "Algeria"             "agr_land"     18.564579  19.07555 19.060015 18.601526 18.614962 18.370184
    "Algeria"             "article"              .         .         .         .         .         .
    "Andorra"             "article"              .         .         .         .         .         .
    "Andorra"             "agr_land"      55.31915  53.19149  53.19149  51.06383  51.06383  51.06383
    "Andorra"             "clean_fuel"           .         .         .         .         .         .
    "Andorra"             "agr_for_fish"         .         .         .         .         .         .
    "Angola"              "clean_fuel"           .         .         .         .         .         .
    "Angola"              "article"              .         .         .         .         .         .
    "Angola"              "agr_land"     36.259727 36.241276  36.22363  36.20518 36.187534 36.169888
    "Angola"              "agr_for_fish"         .         .         .         .         .         .
    "Antigua and Barbuda" "agr_for_fish"         .         .         .         .         .         .
    "Antigua and Barbuda" "clean_fuel"           .         .         .         .         .         .
    "Antigua and Barbuda" "agr_land"     20.454546  22.72727 20.454546 20.454546 20.454546 20.454546
    "Antigua and Barbuda" "article"              .         .         .         .         .         .
    "Argentina"           "agr_land"      46.54638  46.54638  46.54638  46.54638  46.54638  46.54638
    "Argentina"           "article"              .         .         .         .         .         .
    "Argentina"           "clean_fuel"           .         .         .         .         .         .
    "Argentina"           "agr_for_fish"  9.637561 10.865882  10.98487 11.952522 10.230838   6.58391
    "Armenia"             "clean_fuel"           .         .         .         .         .         .
    "Armenia"             "agr_land"             .         .         .         .         .         .
    "Armenia"             "agr_for_fish"         .         .         .         .         .         .
    "Armenia"             "article"              .         .         .         .         .         .
    "Australia"           "agr_land"      64.22979   65.0243  65.06059  64.81093  65.13115  65.02571
    "Australia"           "agr_for_fish"         .         .         .         .         .         .
    "Australia"           "clean_fuel"           .         .         .         .         .         .
    "Australia"           "article"              .         .         .         .         .         .
    "Austria"             "agr_for_fish"         .         .         .         .         .         .
    "Austria"             "agr_land"      40.42414  40.24358  40.07634   39.0475  38.86694  38.65124
    "Austria"             "clean_fuel"           .         .         .         .         .         .
    "Austria"             "article"              .         .         .         .         .         .
    end
    I cannot figurate our how to reshape it in a penel format. I do not think reshape long works in this case. Do you have any suggestion?

    Thanks in advance for your help.

    Best,

    Dario

  • #2
    Your data is in a hybrid layout. It is wide in year but long in name. If you just want to make it long in both, -reshape long- will work just fine:
    Code:
    reshape long y, i(country name) j(year)
    But if you want to now go wide in name, you should follow that with:
    Code:
    rename y _
    reshape wide _, i(country year) j(name) string
    rename _* *
    Note: In order for this second block of code to run, it is necessary that the values of the variable name all be suitable Stata variable names. This is true in the example data. If it is not true in the full data set, you will get an error message from -reshape wide-. In that case, you will have to change some of the values of names. See -help strtoname()- for a function that specifically does this.

    Comment


    • #3
      Clyde Schechter thanks a lot. Yes the format is quite wired. I need all the dataset in a long form. So I will go for your first suggestion.

      Comment

      Working...
      X