Announcement

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

  • Reshaping into wide data

    Dear Stata developers and users,
    I encountered a problem in transforming the following long data into something like wide data structure.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int unique_ID float season int crop_ float(plant_M1 plant_M2 plant_M3)
    10101 1  1  6 . .
    10101 1 23  6 . .
    10101 1 10  6 . .
    10101 2 10 11 . .
    10101 1  3  6 7 .
    10101 1  5  6 7 .
    10101 2  5 10 . .
    end
    label values crop_ A1_2
    label def A1_2 1 "Tomato", modify
    label def A1_2 3 "Cauliflower", modify
    label def A1_2 5 "Cabbage", modify
    label def A1_2 10 "Beans", modify
    label def A1_2 23 "Pumpkin", modify
    label values plant_M1 months
    label values plant_M2 months
    label values plant_M3 months
    label def months 6 "Sep-Oct", modify
    label def months 10 "Jan-Feb", modify
    label def months 11 "Feb-Mar", modify
    label def months 7 "Oct-Nov", modify
    The data is unique in unique_ID season crop_
    I want to transform plant_M1, plant_M2, plant_M3 into a wide form i.e., plant_M1_season1, plant_M1_season2, plant_M1_season3, plant_M2_season1................
    However, I also need the names of the vegetable crop under variable "crop_" to be repeated only once and its corresponding plant_M* values to be adjusted in the appropriate "plant_M*_season*" column.
    For example for unique_ID 10101 "Beans" is repeated twice. I want only one observation for "Beans" under the variable "crop_" and the month Sep-Oct under variable "plant_M1_season1" and the month Feb-Mar under variable "plant_M1_season2" and so on.

  • #2
    Is this what you are looking for:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int unique_ID float season int crop_ float(plant_M1 plant_M2 plant_M3)
    10101 1  1  6 . .
    10101 1 23  6 . .
    10101 1 10  6 . .
    10101 2 10 11 . .
    10101 1  3  6 7 .
    10101 1  5  6 7 .
    10101 2  5 10 . .
    end
    label values crop_ A1_2
    label def A1_2 1 "Tomato", modify
    label def A1_2 3 "Cauliflower", modify
    label def A1_2 5 "Cabbage", modify
    label def A1_2 10 "Beans", modify
    label def A1_2 23 "Pumpkin", modify
    label values plant_M1 months
    label values plant_M2 months
    label values plant_M3 months
    label def months 6 "Sep-Oct", modify
    label def months 10 "Jan-Feb", modify
    label def months 11 "Feb-Mar", modify
    label def months 7 "Oct-Nov", modify
    
    
    rename plant* =_season
    reshape wide plant* , i(unique_ID crop_) j(season)

    Comment


    • #3
      Thanks Fahad

      Comment

      Working...
      X