Announcement

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

  • change format of data (similar to transpose)

    Suppose I have a dataset like this:

    Code:
    clear
    input firm_id value_time1 value_time2 value_time3
    1 3 -5 3
    2 2 6 9
    3 5 3 1
    4 5 8 0
    5 3 -4 6
    end
    save temp1, replace
    but with many more time periods.

    And I would like a dataset with one firm-time period per observation like this.

    Code:
    clear
    input firm_id time value
    1 1 3
    2 1 2
    3 1 5
    4 1 5
    5 1 3
    
    1 2 -5
    2 2 6
    3 2 3
    4 2 8
    5 2 -4
    
    1 3 3
    2 3 9
    3 3 1
    4 3 0
    5 3 6
    end
    save temp2, replace
    This is sort of like transpose, but I need to retain the time period (which is the variable label in the first dataset). If it was only 3 time periods, it would be easy to do the long way (open the dataset 3 times, save the relevant time period, add the time value, then append the 3 files). But if there are 100 time periods, that way is not efficient (not that its efficient if there are only 3 time periods). What is an efficient way to convert the data into the second format?

    Thanks in advance.


  • #2
    The term format is overloaded in computing, meaning file format, data layout, display format, and perhaps more besides.

    Following Clyde Schechter I strongly recommend the term layout here; regardless of terminology reshape long is what you need in Stata terms.

    Code:
    clear
    input firm_id value_time1 value_time2 value_time3
    1 3 -5 3
    2 2 6 9
    3 5 3 1
    4 5 8 0
    5 3 -4 6
    end
    
    reshape long value_time, i(firm_id) j(time)
    
    rename value_time value 
    
    list, sepby(firm_id )
    
         +------------------------+
         | firm_id   time   value |
         |------------------------|
      1. |       1      1       3 |
      2. |       1      2      -5 |
      3. |       1      3       3 |
         |------------------------|
      4. |       2      1       2 |
      5. |       2      2       6 |
      6. |       2      3       9 |
         |------------------------|
      7. |       3      1       5 |
      8. |       3      2       3 |
      9. |       3      3       1 |
         |------------------------|
     10. |       4      1       5 |
     11. |       4      2       8 |
     12. |       4      3       0 |
         |------------------------|
     13. |       5      1       3 |
     14. |       5      2      -4 |
     15. |       5      3       6 |
         +------------------------+

    Comment


    • #3
      @Nick Cox: Thank you very much for the solution. And my apologies about the word "format". I will more carefully word my topics from now on.

      Comment


      • #4
        You don't need to apologise. Your usage is very common and very widely understood. It's just that I am fussy about such details.

        Comment

        Working...
        X