Announcement

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

  • Reshape on month and year simultaneously

    I have data for each month of the year across about 2 decades, but it is currently in wide format, where the end of each variable name is YEAR_MONTH, as you can see in the example below. How can I simultaneously reshape on both month and year together, so that the final dataset ends up in month-year observations (12 rows per year, across all 20ish years).


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(STATUS_1992_01 STATUS_1992_02 STATUS_1992_03 STATUS_1992_04 STATUS_1992_05 STATUS_1992_06) int resp_id
    1 1 1 -4 -4 -4  1
    -4 -4 -4 -4 -4 -4  2
    -4 -4 -4 -4 -4 -4  3
    -4 -4 -4 -4 -4 -4  4
    -4 1 1 1 1 1  5
    -4 -4 -4 -4 -4 -4  6
    -4 -4 -4 -4 -4 -4  7
    -4 -4 -4 -4 -4 -4  8
    -4 -4 -4 -4 -4 -4  9
    -4 -4 -4 -4 -4 -4 10
    end

  • #2
    Code:
    clear
    input byte(STATUS_1992_01 STATUS_1992_02 STATUS_1992_03 STATUS_1992_04 STATUS_1992_05 STATUS_1992_06) int resp_id
    1 1 1 -4 -4 -4  1
    -4 -4 -4 -4 -4 -4  2
    -4 -4 -4 -4 -4 -4  3
    -4 -4 -4 -4 -4 -4  4
    -4 1 1 1 1 1  5
    -4 -4 -4 -4 -4 -4  6
    -4 -4 -4 -4 -4 -4  7
    -4 -4 -4 -4 -4 -4  8
    -4 -4 -4 -4 -4 -4  9
    -4 -4 -4 -4 -4 -4 10
    end
    
    reshape long STATUS_, i(resp_id) j(sdate) string 
    
    gen mdate = monthly(sdate, "YM")
    
    list, sepby(resp_id)
    
         +-------------------------------------+
         | resp_id     sdate   STATUS_   mdate |
         |-------------------------------------|
      1. |       1   1992_01         1     384 |
      2. |       1   1992_02         1     385 |
      3. |       1   1992_03         1     386 |
      4. |       1   1992_04        -4     387 |
      5. |       1   1992_05        -4     388 |
      6. |       1   1992_06        -4     389 |
         |-------------------------------------|
      7. |       2   1992_01        -4     384 |
      8. |       2   1992_02        -4     385 |
      9. |       2   1992_03        -4     386 |
     10. |       2   1992_04        -4     387 |
     11. |       2   1992_05        -4     388 |
     12. |       2   1992_06        -4     389 |
         |-------------------------------------|
     13. |       3   1992_01        -4     384 |
     14. |       3   1992_02        -4     385 |
     15. |       3   1992_03        -4     386 |
     16. |       3   1992_04        -4     387 |
     17. |       3   1992_05        -4     388 |
     18. |       3   1992_06        -4     389 |
         |-------------------------------------|
     19. |       4   1992_01        -4     384 |
     20. |       4   1992_02        -4     385 |
     21. |       4   1992_03        -4     386 |
     22. |       4   1992_04        -4     387 |
     23. |       4   1992_05        -4     388 |
     24. |       4   1992_06        -4     389 |
         |-------------------------------------|
     25. |       5   1992_01        -4     384 |
     26. |       5   1992_02         1     385 |
     27. |       5   1992_03         1     386 |
     28. |       5   1992_04         1     387 |
     29. |       5   1992_05         1     388 |
     30. |       5   1992_06         1     389 |
         |-------------------------------------|
     31. |       6   1992_01        -4     384 |
     32. |       6   1992_02        -4     385 |
     33. |       6   1992_03        -4     386 |
     34. |       6   1992_04        -4     387 |
     35. |       6   1992_05        -4     388 |
     36. |       6   1992_06        -4     389 |
         |-------------------------------------|
     37. |       7   1992_01        -4     384 |
     38. |       7   1992_02        -4     385 |
     39. |       7   1992_03        -4     386 |
     40. |       7   1992_04        -4     387 |
     41. |       7   1992_05        -4     388 |
     42. |       7   1992_06        -4     389 |
         |-------------------------------------|
     43. |       8   1992_01        -4     384 |
     44. |       8   1992_02        -4     385 |
     45. |       8   1992_03        -4     386 |
     46. |       8   1992_04        -4     387 |
     47. |       8   1992_05        -4     388 |
     48. |       8   1992_06        -4     389 |
         |-------------------------------------|
     49. |       9   1992_01        -4     384 |
     50. |       9   1992_02        -4     385 |
     51. |       9   1992_03        -4     386 |
     52. |       9   1992_04        -4     387 |
     53. |       9   1992_05        -4     388 |
     54. |       9   1992_06        -4     389 |
         |-------------------------------------|
     55. |      10   1992_01        -4     384 |
     56. |      10   1992_02        -4     385 |
     57. |      10   1992_03        -4     386 |
     58. |      10   1992_04        -4     387 |
     59. |      10   1992_05        -4     388 |
     60. |      10   1992_06        -4     389 |
         +-------------------------------------+
    Follow with some variant on

    Code:
    format mdate %tm

    Comment

    Working...
    X