Announcement

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

  • Counting number of days between dates among different observations 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
    if the dataset is not too large,
    Code:
    bys UPN First_AdministrationDate (DaysAntimicrobial): keep if _n==_N
    
    su First_AdministrationDate
    local min = r(min)
    
    su Last_AdministrationDate
    local max = r(max)
    
    forval i = `min' / `max' {
    g days`i'=`i'
    }
    
    reshape long days ,i(UPN First_AdministrationDate Last_AdministrationDate DaysAntimicrobial)
    
    keep if inrange(days,First_AdministrationDate,Last_AdministrationDate)
    bys UPN days: keep if _n==1
    
    collapse (count) days, by(UPN)

    Comment

    Working...
    X