Announcement

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

  • Counting number of days between dates among different observations excluding overlap by group

    Hello,

    I am working on Windows 10, Stata 14.2 for a project that asks to count the number of days a person received an antibiotic. The data is sorted by UPN (person-level identifier) and contains an observation for each different antibiotic order. Some antibiotics are administered on the same day as others, so there is overlap between days and antibiotic administration. In the case there is overlap between 2+ antibiotics, this still counts as a single day of coverage. I am trying to generate a number of how many days an individual was given at least one antibiotic.

    At first, I tried counting days between the start and stop between the first and last administration for each observation. I am stuck at this point, because if I were to sum the days for each antibiotic, I would be adding days where overlap may have occurred. I have also tried using the first day administered and the last day administered within each UPN, however, this accounts for days no antimicrobial was given if there was a break in therapy.

    In summary, I am looking for help with coding a solution to my question: How many days did an individual (UPN) receive at least 1 antibiotic?

    Thank you.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long UPN int(First_AdministrationDate Last_AdministrationDate) byte DaysAntimicrobial
    160175 20551 20555  5
    160175 20551 20571 21
    160175 20556 20564  9
    160175 20556 20564  9
    160175 20558 20564  7
    160175 20558 20559  2
    160175 20560 20564  5
    160175 20576 20576  1
    160192 20562 20591 30
    160192 20562 20564  3
    160192 20565 20577 13
    160192 20566 20568  3
    160192 20567 20570  4
    160192 20567 20578 12
    160192 20570 20583 14
    160192 20570 20570  1
    160192 20571 20577  7
    160192 20579 20584  6
    160192 20583 20590  8
    160192 20590 20591  2
    160202 20569 20575  7
    160202 20570 20586 17
    160202 20576 20583  8
    160202 20576 20581  6
    160202 20578 20582  5
    160202 20578 20579  2
    160202 20584 20585  2
    160224 20576 20582  7
    160224 20576 20595 20
    160224 20582 20590  9
    160224 20583 20594 12
    160224 20583 20586  4
    160224 20585 20605 21
    160224 20585 20605 21
    160224 20597 20597  1
    160225 20580 20593 14
    160225 20580 20592 13
    160225 20589 20592  4
    160225 20599 20599  1
    160226 20583 20594 12
    160226 20583 20593 11
    160226 20583 20594 12
    160226 20601 20601  1
    160229 20590 20600 11
    160229 20590 20606 17
    160229 20590 20605 16
    160229 20590 20605 16
    160229 20600 20604  5
    160229 20600 20601  2
    160229 20601 20601  1
    160229 20601 20606  6
    160229 20602 20606  5
    160230 20590 20605 16
    160230 20590 20605 16
    160230 20597 20604  8
    160230 20597 20601  5
    160230 20608 20608  1
    160271 20621 20627  7
    160271 20621 20634 14
    160271 20627 20632  6
    160271 20628 20633  6
    160281 20628 20641 14
    160281 20628 20646 19
    160281 20638 20642  5
    160281 20638 20638  1
    160281 20638 20641  4
    160281 20644 20644  1
    160292 20632 20642 11
    160292 20632 20645 14
    160292 20639 20640  2
    160292 20639 20643  5
    160292 20641 20644  4
    160292 20643 20645  3
    160304 20640 20654 15
    160304 20640 20645  6
    160304 20641 20642  2
    160304 20646 20651  6
    160304 20646 20652  7
    160304 20647 20649  3
    160304 20647 20651  5
    160304 20661 20661  1
    160305 20640 20655 16
    160305 20640 20654 15
    160312 20643 20655 13
    160312 20643 20656 14
    160317 20649 20652  4
    160317 20649 20669 21
    160317 20649 20669 21
    160317 20652 20654  3
    160317 20653 20661  9
    160317 20653 20661  9
    160317 20654 20656  3
    160317 20656 20658  3
    160317 20656 20667 12
    160317 20658 20665  8
    160317 20665 20668  4
    160317 20668 20669  2
    160333 20653 20666 14
    160333 20653 20668 16
    160333 20657 20668 12
    end
    format %tdnn/dd/CCYY First_AdministrationDate
    format %tdnn/dd/CCYY Last_AdministrationDate

  • #2
    This is the most straightforward approach, if you don't have a bazillion observations.
    Code:
    generate record = _n
    expand Last_AdministrationDate-First_AdministrationDate+1
    by record, sort: generate AdministrationDate = First_AdministrationDate + _n -1
    drop record First_AdministrationDate Last_AdministrationDate
    duplicates drop UPN AdministrationDate, force
    collapse (count) N=AdministrationDate, by(UPN)
    list, clean noobs
    Code:
    . list, clean noobs
    
           UPN    N  
        160175   22  
        160192   30  
        160202   18  
        160224   30  
        160225   15  
        160226   13  
        160229   17  
        160230   17  
        160271   14  
        160281   19  
        160292   14  
        160304   16  
        160305   16  
        160312   14  
        160317   21  
        160333   16

    Comment


    • #3
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input long UPN int(First_AdministrationDate Last_AdministrationDate)
      160175 20551 20555
      160175 20551 20571
      160175 20556 20564
      160175 20556 20564
      160175 20558 20564
      160175 20558 20559
      160175 20560 20564
      160175 20576 20576
      160192 20562 20591
      160192 20562 20564
      160192 20565 20577
      160192 20566 20568
      160192 20567 20570
      160192 20567 20578
      160192 20570 20583
      160192 20570 20570
      160192 20571 20577
      160192 20579 20584
      160192 20583 20590
      160192 20590 20591
      160202 20569 20575
      160202 20570 20586
      160202 20576 20583
      160202 20576 20581
      160202 20578 20582
      160202 20578 20579
      160202 20584 20585
      160224 20576 20582
      160224 20576 20595
      160224 20582 20590
      160224 20583 20594
      160224 20583 20586
      160224 20585 20605
      160224 20585 20605
      160224 20597 20597
      160225 20580 20593
      160225 20580 20592
      160225 20589 20592
      160225 20599 20599
      160226 20583 20594
      160226 20583 20593
      160226 20583 20594
      160226 20601 20601
      160229 20590 20600
      160229 20590 20606
      160229 20590 20605
      160229 20590 20605
      160229 20600 20604
      160229 20600 20601
      160229 20601 20601
      160229 20601 20606
      160229 20602 20606
      160230 20590 20605
      160230 20590 20605
      160230 20597 20604
      160230 20597 20601
      160230 20608 20608
      160271 20621 20627
      160271 20621 20634
      160271 20627 20632
      160271 20628 20633
      160281 20628 20641
      160281 20628 20646
      160281 20638 20642
      160281 20638 20638
      160281 20638 20641
      160281 20644 20644
      160292 20632 20642
      160292 20632 20645
      160292 20639 20640
      160292 20639 20643
      160292 20641 20644
      160292 20643 20645
      160304 20640 20654
      160304 20640 20645
      160304 20641 20642
      160304 20646 20651
      160304 20646 20652
      160304 20647 20649
      160304 20647 20651
      160304 20661 20661
      160305 20640 20655
      160305 20640 20654
      160312 20643 20655
      160312 20643 20656
      160317 20649 20652
      160317 20649 20669
      160317 20649 20669
      160317 20652 20654
      160317 20653 20661
      160317 20653 20661
      160317 20654 20656
      160317 20656 20658
      160317 20656 20667
      160317 20658 20665
      160317 20665 20668
      160317 20668 20669
      160333 20653 20666
      160333 20653 20668
      160333 20657 20668
      end
      format %tdnn/dd/CCYY First_AdministrationDate
      format %tdnn/dd/CCYY Last_AdministrationDate
      
      //  GO TO LONG LAYOUT
      gen long obs_no = _n
      assert !missing(First_AdministrationDate, Last_AdministrationDate)
      reshape long @_AdministrationDate, i(obs_no) j(first_last) string
      
      //  IDENTIFY NUMBER OF ANTIBIOTICS BEING TAKEN ON ANY DATE
      by UPN (_AdministrationDate first_last), sort: gen n_antibiotics ///
          = sum((first_last == "First") - (first_last == "Last"))
          
      //  MARK CONTINUING SPELLS OF ANTIBIOTIC USE (SPELL ENDS WHEN n_antibiotics == 0)
      by UPN (_AdministrationDate first_last): gen spell_num ///
          = sum((_n == 1) | n_antibiotics[_n-1] == 0)
          
      //  CALCULATE NUMBER OF DAYS IN EACH SPELL (INCLUDING FIRST & LAST)
      by UPN spell_num (_AdministrationDate first_last), sort: ///
          gen spell_days = _AdministrationDate[_N] - _AdministrationDate[1] + 1
          
      //  CALCULATE NUMBER OF DAYS OVER ALL SPELLS
      by UPN (spell_num _AdministrationDate first_last): ///
          egen total_antibiotic_days = total(cond(spell_num != spell_num[_n-1], ///
              spell_days, .))
      Added: Crossed with #2 which offers a rather different approach.
      Last edited by Clyde Schechter; 23 Jan 2022, 15:47.

      Comment

      Working...
      X