Announcement

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

  • Reshape long with multiple variables

    Hello,
    I have wide-structured data for answers on a 4 item scale at 2 time points. Please see data details below
    promis_pc43 is 1 of 4 questions
    m1 or m3 indicates the timepoint
    Is it possible to reshape this to long data where I have a time variable (1 or 3) and the answer for each question at each time point per study ID?
    Thank you!



    Contains data
    obs: 310
    vars: 9
    ----------------------------------------------------------------------------------------------
    storage display value
    variable name type format label variable label
    ----------------------------------------------------------------------------------------------
    record_id str8 %9s record_id
    promis_pc43_2~1 byte %10.0g promis_pc43_2_m1
    promis_pc44_2~1 byte %10.0g promis_pc44_2_m1
    promis_pc45_2~1 byte %10.0g promis_pc45_2_m1
    promis_pc47_2~1 byte %10.0g promis_pc47_2_m1
    promis_pc43_2~3 byte %10.0g promis_pc43_2_m3
    promis_pc44_2~3 byte %10.0g promis_pc44_2_m3
    promis_pc45_2~3 byte %10.0g promis_pc45_2_m3
    promis_pc47_2~3 byte %10.0g promis_pc47_2_m3
    ----------------------------------------------------------------------------------------------



  • #2
    can you create a time variable = 1,2 and then reshape on that and id?

    Comment


    • #3
      I appreciate your response. Do you know how I could do this?

      Comment


      • #4
        Please give a data example, making sure that your variable names are shown in full, and not abbreviated. https://www.statalist.org/forums/help#stata explains.

        Comment


        • #5
          [CODE]. dataex

          ----------------------- copy starting from the next line -----------------------
          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input str8 record_id byte(promis_pc43_2_m1 promis_pc44_2_m1 promis_pc45_2_m1 promis_pc47_2_m1 promis_pc43_2_m3 promis_pc44_2_m3 promis_pc45_2_m3 promis_pc47_2_m3)
          "W4-0001"  1 1 1 2 1 1 1 1
          "W4-0002"  . . . . . . . .
          "W4-0003"  5 4 6 3 . . . .
          "W4-0004"  3 3 7 3 3 3 3 3
          "W4-0005"  5 4 9 5 . . . .
          "W4-0006"  . . . . 4 2 4 4
          "W4-0007"  2 2 3 7 3 2 3 4
          "W4-0008"  1 2 2 2 2 3 3 3
          "W4-0009"  . . . . . . . .
          "W4-0010"  4 5 6 5 . . . .
          "W4-0011"  3 4 4 8 4 4 4 4
          "W4-0012"  3 3 2 9 4 3 2 3
          "W4-0013"  4 4 4 5 . . . .
          "W4-0014"  4 3 4 7 5 5 5 5
          "W4-0015"  5 5 5 5 9 4 4 4
          "W4-0016"  1 2 4 5 . . . .
          "W4-0017"  5 8 5 5 5 5 5 5

          Comment


          • #6
            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input str8 record_id byte(promis_pc43_2_m1 promis_pc44_2_m1 promis_pc45_2_m1 promis_pc47_2_m1 promis_pc43_2_m3 promis_pc44_2_m3 promis_pc45_2_m3 promis_pc47_2_m3)
            "W4-0001"  1 1 1 2 1 1 1 1
            "W4-0002"  . . . . . . . .
            "W4-0003"  5 4 6 3 . . . .
            "W4-0004"  3 3 7 3 3 3 3 3
            "W4-0005"  5 4 9 5 . . . .
            "W4-0006"  . . . . 4 2 4 4
            "W4-0007"  2 2 3 7 3 2 3 4
            "W4-0008"  1 2 2 2 2 3 3 3
            "W4-0009"  . . . . . . . .
            "W4-0010"  4 5 6 5 . . . .
            "W4-0011"  3 4 4 8 4 4 4 4
            "W4-0012"  3 3 2 9 4 3 2 3
            "W4-0013"  4 4 4 5 . . . .
            "W4-0014"  4 3 4 7 5 5 5 5
            "W4-0015"  5 5 5 5 9 4 4 4
            "W4-0016"  1 2 4 5 . . . .
            "W4-0017"  5 8 5 5 5 5 5 5
            end
            
            reshape long promis_pc43 promis_pc44 promis_pc45 promis_pc47, i(record_id) j(time) string
            destring time, replace ignore(_2_m)
            Res.:

            Code:
            . l, sep(0)
            
                 +-------------------------------------------------------------+
                 | record~d   time   promi~43   promi~44   promi~45   promi~47 |
                 |-------------------------------------------------------------|
              1. |  W4-0001      1          1          1          1          2 |
              2. |  W4-0001      3          1          1          1          1 |
              3. |  W4-0002      1          .          .          .          . |
              4. |  W4-0002      3          .          .          .          . |
              5. |  W4-0003      1          5          4          6          3 |
              6. |  W4-0003      3          .          .          .          . |
              7. |  W4-0004      1          3          3          7          3 |
              8. |  W4-0004      3          3          3          3          3 |
              9. |  W4-0005      1          5          4          9          5 |
             10. |  W4-0005      3          .          .          .          . |
             11. |  W4-0006      1          .          .          .          . |
             12. |  W4-0006      3          4          2          4          4 |
             13. |  W4-0007      1          2          2          3          7 |
             14. |  W4-0007      3          3          2          3          4 |
             15. |  W4-0008      1          1          2          2          2 |
             16. |  W4-0008      3          2          3          3          3 |
             17. |  W4-0009      1          .          .          .          . |
             18. |  W4-0009      3          .          .          .          . |
             19. |  W4-0010      1          4          5          6          5 |
             20. |  W4-0010      3          .          .          .          . |
             21. |  W4-0011      1          3          4          4          8 |
             22. |  W4-0011      3          4          4          4          4 |
             23. |  W4-0012      1          3          3          2          9 |
             24. |  W4-0012      3          4          3          2          3 |
             25. |  W4-0013      1          4          4          4          5 |
             26. |  W4-0013      3          .          .          .          . |
             27. |  W4-0014      1          4          3          4          7 |
             28. |  W4-0014      3          5          5          5          5 |
             29. |  W4-0015      1          5          5          5          5 |
             30. |  W4-0015      3          9          4          4          4 |
             31. |  W4-0016      1          1          2          4          5 |
             32. |  W4-0016      3          .          .          .          . |
             33. |  W4-0017      1          5          8          5          5 |
             34. |  W4-0017      3          5          5          5          5 |
                 +-------------------------------------------------------------+
            
            .

            Comment


            • #7
              Here is a tweak on Andrew Musau's very helpful answer showing how to automate identification of the stubs: This procedure is documented at https://www.stata.com/support/faqs/d...-with-reshape/

              Code:
              unab stubs : *_m1 
              local stubs : subinstr local stubs "_m1" "", all 
              
              reshape long `stubs', i(record_id) j(time) string
              destring time, replace ignore(_2_m)

              Comment


              • #8
                Wow. Thank you both so much.

                Comment

                Working...
                X