Announcement

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

  • Calculating time to relapse

    Hello!

    I have a dataset of patients with opioid use disorder (OUD) and would like to calculate the time to relapse (in days) for each patient given the data below. Note that some patients have multiple relapses -- what would be the most appropriate way to record the time to relapse for such instances? Your feedback is most welcome.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(id date) byte relapse
     3 22122 0
     3 22123 1
     3 22133 0
     3 22134 0
     3 22135 0
     3 22136 0
     3 22139 0
     3 22140 0
     3 22141 0
     3 22142 0
     3 22143 0
     3 22144 0
     3 22145 0
     3 22146 0
     3 22147 0
     3 22148 0
     5 20442 0
     5 20443 0
     5 20444 0
     5 20447 0
     5 20557 0
     5 20558 0
     5 20559 0
     5 20560 0
     5 20561 0
     5 20566 0
     5 20571 0
     5 20572 0
     5 20577 0
     5 20586 0
     5 20588 0
     5 20589 1
     8 19200 0
     8 19201 0
     8 19202 0
     8 19204 0
     8 19205 0
     8 19206 0
     8 19211 0
     8 19214 0
     8 19215 0
     8 19220 0
     8 19222 0
     8 19224 1
     8 19225 0
     8 19226 1
     8 19227 0
     8 19289 1
     8 19292 0
     8 19314 1
     8 19315 0
     8 19335 0
     8 19336 1
     8 19937 0
     8 20213 0
     9 21520 0
     9 21521 0
     9 21522 0
     9 21524 0
     9 21526 0
     9 21529 0
     9 21530 0
     9 21544 0
     9 21573 0
     9 21574 0
     9 21575 1
     9 21576 0
     9 21578 0
     9 21579 0
     9 21593 0
     9 21616 0
     9 21627 0
     9 21644 0
    10 22165 0
    10 22166 0
    10 22168 0
    10 22169 0
    10 22170 0
    10 22171 0
    10 22173 0
    10 22175 0
    10 22176 0
    10 22177 0
    10 22178 0
    10 22179 0
    10 22180 0
    10 22181 0
    10 22182 0
    10 22189 1
    10 22211 1
    10 22212 0
    10 22214 0
    10 22289 0
    10 22290 0
    10 22291 0
    10 22292 0
    12 20442 0
    12 20443 0
    12 20445 0
    12 20446 0
    12 20448 0
    12 20449 0
    12 20473 0
    12 20474 0
    12 20478 0
    12 20482 0
    12 20489 0
    12 20495 0
    12 20499 0
    12 20500 0
    12 20502 0
    12 20506 0
    12 20507 0
    12 20508 0
    12 20509 0
    12 20521 0
    12 20522 0
    12 20525 0
    12 20531 0
    12 20533 0
    12 20534 0
    12 20537 0
    12 20538 0
    12 20541 0
    12 20542 0
    12 20543 0
    12 20545 0
    12 20550 0
    12 20554 0
    12 20555 0
    12 20562 0
    12 20570 0
    12 20597 0
    12 20614 0
    12 20616 0
    12 20618 0
    12 20643 0
    12 20644 0
    12 20666 1
    12 20667 0
    12 20674 0
    12 20675 0
    12 20677 0
    12 20679 0
    12 20680 0
    12 20681 0
    12 20682 0
    12 20684 0
    12 20685 0
    12 20686 0
    12 20688 0
    12 20689 0
    12 20691 0
    12 20692 0
    12 20695 0
    12 20696 0
    12 20699 0
    12 20706 0
    12 20712 0
    12 20713 0
    12 20716 0
    12 20718 0
    12 20719 0
    12 20726 0
    12 20733 0
    12 20737 0
    12 20739 0
    12 20740 0
    12 20744 0
    12 20745 0
    12 20746 0
    12 20747 0
    12 20751 0
    12 20752 0
    end
    format %td date
    Last edited by Anton Ivanov; 07 Sep 2023, 12:47.

  • #2
    This should give you the number of days since the day of the first observation:

    Code:
    sort id date
    bysort id: gen time_to_relapse = datediff(date[1], date, "day") if relapse == 1
    list if _n <= 35
    Code:
    . list if _n <= 35
    
         +-------------------------------------+
         | id        date   relapse   time_t~e |
         |-------------------------------------|
      1. |  3   26jul2020         0          . |
      2. |  3   27jul2020         1          1 |
      3. |  3   06aug2020         0          . |
      4. |  3   07aug2020         0          . |
      5. |  3   08aug2020         0          . |
         |-------------------------------------|
      6. |  3   09aug2020         0          . |
      7. |  3   12aug2020         0          . |
      8. |  3   13aug2020         0          . |
      9. |  3   14aug2020         0          . |
     10. |  3   15aug2020         0          . |
         |-------------------------------------|
     11. |  3   16aug2020         0          . |
     12. |  3   17aug2020         0          . |
     13. |  3   18aug2020         0          . |
     14. |  3   19aug2020         0          . |
     15. |  3   20aug2020         0          . |
         |-------------------------------------|
     16. |  3   21aug2020         0          . |
     17. |  5   20dec2015         0          . |
     18. |  5   21dec2015         0          . |
     19. |  5   22dec2015         0          . |
     20. |  5   25dec2015         0          . |
         |-------------------------------------|
     21. |  5   13apr2016         0          . |
     22. |  5   14apr2016         0          . |
     23. |  5   15apr2016         0          . |
     24. |  5   16apr2016         0          . |
     25. |  5   17apr2016         0          . |
         |-------------------------------------|
     26. |  5   22apr2016         0          . |
     27. |  5   27apr2016         0          . |
     28. |  5   28apr2016         0          . |
     29. |  5   03may2016         0          . |
     30. |  5   12may2016         0          . |
         |-------------------------------------|
     31. |  5   14may2016         0          . |
     32. |  5   15may2016         1        147 |
     33. |  8   26jul2012         0          . |
     34. |  8   27jul2012         0          . |
     35. |  8   28jul2012         0          . |
         +-------------------------------------+
    But for cases where someone has had multiple relapses, you may actually want the number of days since the last relapse, which is a little more tricky. There's a fairly straightforward looping solution but I'd rather give you something vectorized. I'll think about it, and in the mean time someone else will probably post the correct solution.

    Comment


    • #3
      Here is one possibility based on some technique Clyde Schechter demonstrated in another thread.

      Code:
      sort id date
      bysort id: gen time_to_relapse = datediff(date[1], date, "day") if relapse == 1
      gen uniqueid = _n
      
      quietly capture frame drop relapse_dates
      frame put uniqueid id time_to_relapse if relapse == 1, into(relapse_dates) 
      frame relapse_dates {
          bysort id: gen time_between_relapse = time_to_relapse - time_to_relapse[_n-1]
          bysort id: replace time_between_relapse = time_to_relapse if time_to_relapse[_n-1] == .
      }
      frlink m:1 uniqueid, frame(relapse_dates)
      frget time_between_relapse, from(relapse_dates)
      drop uniqueid relapse_dates
      time_to_relapse gives the number of days to relapse from the start date. time_between_relapse gives the number of days from the start date, or the last relapse.

      Comment


      • #4
        Just to pick a small nit, the -quietly- before -capture- is not necessary: -capture- is inherently done quietly. In fact, if you don't want a -capture-d command to be executed quietly, you how to explicitly code -capture noisily-. On the other hand, that extra -quietly- does no harm either.

        Comment


        • #5
          Thank you, I'm here to learn as much as anyone else. I picked up quietly capture early on when I was studying programs. Someone else posted:

          Code:
          quietly capture program drop <programname>
          and I've been quietly capture-ing things by rote ever since.

          Comment


          • #6
            Daniel, Clyde, thank you very much for your responses. Both solutions worked perfectly.

            Comment

            Working...
            X