Announcement

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

  • How to Calculating time difference between visits in a panel data

    I hope this finds you well.

    I kindly need your help to calculate the time difference(Months, Years or Day) in a longitudinal data set where I have patients with recorded visits up to 8 visit. Some patients have up to 3rd visit, 5 visit.. etc. I have date of each visit and I would like to get the time difference between each visit for each patient.

    Regards

    Humphrey

  • #2
    You need to provide some example data consisting of the patient identifier and time variable. Assuming that these are named patient_id and date in your data set, you can copy and paste the result of the following:

    Code:
    ssc install dataex
    sort patient_id date
    dataex patient_id date in 1/10

    Comment


    • #3
      input uniqueidentifier(Patient ID) , DateTested , Visit_order
      1 20508 1
      1 20508 2
      2 20457 1
      2 20545 2
      3 20456 1
      3 20458 2
      4 20460 1
      4 20468 2
      5 20464 1
      5 20464 2
      6 20879 1
      6 21243 2
      7 20908 1
      7 21135 2
      8 20502 1
      8 20968 2
      9 20746 1
      9 20891 2
      10 20746 1
      10 20891 2
      end
      format %tdD_m_Y DateTested
      [/CODE]

      Comment


      • #4
        Thanks for the data example.

        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input float(Patient_ID DateTested Visit_order)
         1 20508 1
         1 20508 2
         2 20457 1
         2 20545 2
         3 20456 1
         3 20458 2
         4 20460 1
         4 20468 2
         5 20464 1
         5 20464 2
         6 20879 1
         6 21243 2
         7 20908 1
         7 21135 2
         8 20502 1
         8 20968 2
         9 20746 1
         9 20891 2
        10 20746 1
        10 20891 2
        end
        format %tdD_m_Y DateTested
        
        bys Patient_ID (DateTested): gen time_lapse= DateTested- DateTested[_n-1] if _n>1
        Note that the time lapse will be in days.


        Result:

        Code:
        . l, sepby(Patient_ID)
        
             +--------------------------------------------+
             | Patien~D   DateTes~d   Visit_~r   time_l~e |
             |--------------------------------------------|
          1. |        1   24 Feb 16          1          . |
          2. |        1   24 Feb 16          2          0 |
             |--------------------------------------------|
          3. |        2   04 Jan 16          1          . |
          4. |        2   01 Apr 16          2         88 |
             |--------------------------------------------|
          5. |        3   03 Jan 16          1          . |
          6. |        3   05 Jan 16          2          2 |
             |--------------------------------------------|
          7. |        4   07 Jan 16          1          . |
          8. |        4   15 Jan 16          2          8 |
             |--------------------------------------------|
          9. |        5   11 Jan 16          1          . |
         10. |        5   11 Jan 16          2          0 |
             |--------------------------------------------|
         11. |        6   01 Mar 17          1          . |
         12. |        6   28 Feb 18          2        364 |
             |--------------------------------------------|
         13. |        7   30 Mar 17          1          . |
         14. |        7   12 Nov 17          2        227 |
             |--------------------------------------------|
         15. |        8   18 Feb 16          1          . |
         16. |        8   29 May 17          2        466 |
             |--------------------------------------------|
         17. |        9   19 Oct 16          1          . |
         18. |        9   13 Mar 17          2        145 |
             |--------------------------------------------|
         19. |       10   19 Oct 16          1          . |
         20. |       10   13 Mar 17          2        145 |
             +--------------------------------------------+

        Comment


        • #5
          It worked.
          Thank you very much

          Comment

          Working...
          X