Announcement

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

  • Reshape a data

    Year ID sector100 sector200 sector300 sector400
    2005 1 63 5 55 31
    2005 2 51.79 86 38.22 46.23
    2006 1 35.6 35.6 22 23
    2006 2 30 30 22 43
    2007 1 5 52.93 2 23
    2007 2 33 58.46 60.96 12
    2008 1 33 76 28.96 64
    2008 2 35 78 12 35
    How to reshape the above data in a long format, so I can get the sector-specific value for each ID in a long format?
    something like below.
    Year State Sector Value
    2005 1 100 63
    2005 1 200 5
    2005 1 300 55
    2005 1 400 31
    2005 2 100 51.79
    2005 2 200 86
    2005 2 300 38.22
    2005 2 400 46.23
    2006 1
    2006 1
    & so on
    Last edited by kamalesh pahurkar; 03 Feb 2024, 15:57.

  • #2
    Code:
    rename sector* value*
    reshape long value, i(id year) j(sector)

    Comment


    • #3
      By now, you know what dataex is. Use it to present data examples in the future.

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input float(Year ID sector100 sector200 sector300 sector400)
      2005 1    63     5    55    31
      2005 2 51.79    86 38.22 46.23
      2006 1  35.6  35.6    22    23
      2006 2    30    30    22    43
      2007 1     5 52.93     2    23
      2007 2    33 58.46 60.96    12
      2008 1    33    76 28.96    64
      2008 2    35    78    12    35
      end
      
      rename sector* value*
      reshape long value, i(ID Year) j(sector)
      Res.:

      Code:
      . l, sepby(ID)
      
           +----------------------------+
           | ID   Year   sector   value |
           |----------------------------|
        1. |  1   2005      100      63 |
        2. |  1   2005      200       5 |
        3. |  1   2005      300      55 |
        4. |  1   2005      400      31 |
        5. |  1   2006      100    35.6 |
        6. |  1   2006      200    35.6 |
        7. |  1   2006      300      22 |
        8. |  1   2006      400      23 |
        9. |  1   2007      100       5 |
       10. |  1   2007      200   52.93 |
       11. |  1   2007      300       2 |
       12. |  1   2007      400      23 |
       13. |  1   2008      100      33 |
       14. |  1   2008      200      76 |
       15. |  1   2008      300   28.96 |
       16. |  1   2008      400      64 |
           |----------------------------|
       17. |  2   2005      100   51.79 |
       18. |  2   2005      200      86 |
       19. |  2   2005      300   38.22 |
       20. |  2   2005      400   46.23 |
       21. |  2   2006      100      30 |
       22. |  2   2006      200      30 |
       23. |  2   2006      300      22 |
       24. |  2   2006      400      43 |
       25. |  2   2007      100      33 |
       26. |  2   2007      200   58.46 |
       27. |  2   2007      300   60.96 |
       28. |  2   2007      400      12 |
       29. |  2   2008      100      35 |
       30. |  2   2008      200      78 |
       31. |  2   2008      300      12 |
       32. |  2   2008      400      35 |
           +----------------------------+
      Note: Crossed with #2.

      Comment

      Working...
      X