Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
st: Re: Creating drugs combination
From
"Joseph Coveney" <[email protected]>
To
<[email protected]>
Subject
st: Re: Creating drugs combination
Date
Sat, 15 Sep 2012 12:03:09 +0900
Michele Santacatterina wrote:
I have an issue. I am trying to create, from a HIV database, a variable
containing the value of different drug combinations (name regimen) and the
number of drugs (number of drugs)$
ID treatstart treatstop drug name_regimen
number_of_drugs
2 24-Aug-95 14-Nov-97 ZDV ZDV+3TC
2
2 24-Aug-95 13-Feb-03 3TC
2 7-Aug-96 14-Nov-97 LOV ZDV+3TC+LOV
3
2 14-Nov-97 15-Jun-00 IDV 3TC+IDV+d4T
3
2 14-Nov-97 13-Feb-03 d4T
2 15-Jun-00 13-Feb-03 IDV 3TC+IDV+RTV
3
2 15-Jun-00 13-Feb-03 RTV
Patient 2 started ART at 24-Aug-95 with ZDV+3TC and at 7-Aug-96, LOV was added
to the treatment becoming ZDV+3TC+LOV. At 14-nov-97 he finished with ZDV and LOV
and he started with $
If I run the command
by infcare_ID treatstart, sort: gen n_trt = _n
for the number of drugs, i obtain at 7-Aug-96 just one drug (LOV) and not 3 as I
should have (ZDV+3TC+LOV).
Hoping I have been clear enough.
Someone knows how to do it?
--------------------------------------------------------------------------------
It might be helpful to think of the problem as a timeline punctuated by events
(time points at which some drugs are added to the regimen and others
discontinued from it).
It seems that this patient has five distinct calendar dates or events when the
drug regimen was changed (new drugs added, old drugs removed). So, set up a
vertical timeline with an indicator variable for each drug as to whether it was
added to, or removed from, the ongoing regimen. Then, make the indicated
changes to the drug regimen. At the end of the day (event), count the number of
drugs currently in the patient's regimen.
Joseph Coveney
(The machine I'm sending this from has a previous Stata release installed, but
the do-file should work with the current release, too.)
. version 11.2
.
. clear *
. set more off
. input byte ID str9 treatstart str9 treatstop str3 drug
ID treatst~t treatstop drug
1. 2 "24-Aug-95" "14-Nov-97" "ZDV"
2. 2 "24-Aug-95" "13-Feb-03" "3TC"
3. 2 "7-Aug-96" "14-Nov-97" "LOV"
4. 2 "14-Nov-97" "15-Jun-00" "IDV"
5. 2 "14-Nov-97" "13-Feb-03" "d4T"
6. 2 "15-Jun-00" "13-Feb-03" "IDV"
7. 2 "15-Jun-00" "13-Feb-03" "RTV"
8. end
.
. // Convert string dates to date values
. generate int calendar_dt1 = date(treatstart, "DMY", 2020)
. generate int calendar_dt0 = date(treatstop, "DMY", 2020)
.
. /* Convert wide dataset to vertical timeline of changes
> to drug regimen ("event") and type of change within
> event ("action") */
. generate long row_nr = _n
. quietly reshape long calendar_dt, i(row_nr) j(action)
. label define Actions 1 Add 0 Remove
. label values action Actions
. drop treats* row_nr
. format calendar_dt %tdCCYY-NN-DD
. egen long event_nr = group(calendar_dt)
. gsort +event_nr -action
. order ID event_nr action drug calendar_dt
. list, noobs sepby(event_nr) abbreviate(20)
+---------------------------------------------+
| ID event_nr action drug calendar_dt |
|---------------------------------------------|
| 2 1 Add 3TC 1995-08-24 |
| 2 1 Add ZDV 1995-08-24 |
|---------------------------------------------|
| 2 2 Add LOV 1996-08-07 |
|---------------------------------------------|
| 2 3 Add IDV 1997-11-14 |
| 2 3 Add d4T 1997-11-14 |
| 2 3 Remove LOV 1997-11-14 |
| 2 3 Remove ZDV 1997-11-14 |
|---------------------------------------------|
| 2 4 Add IDV 2000-06-15 |
| 2 4 Add RTV 2000-06-15 |
| 2 4 Remove IDV 2000-06-15 |
|---------------------------------------------|
| 2 5 Remove IDV 2003-02-13 |
| 2 5 Remove d4T 2003-02-13 |
| 2 5 Remove 3TC 2003-02-13 |
| 2 5 Remove RTV 2003-02-13 |
+---------------------------------------------+
.
. // Initialize a running tally of drugs ("drug_regimen")
. generate str drug_regimen = drug if action == "Add":Actions
(7 missing values generated)
. /* At each successive "event", you either concatenate (add)
> "drug" to the existing "drug_regimen" (if action is Add)
> or remove "drug" from it (if action is Remove, i.e., not Add) */
. replace drug_regimen = ///
> trim(itrim( ///
> cond(action == "Add":Actions, ///
> drug_regimen + " " + drug_regimen[_n-1], ///
> subinword(drug_regimen[_n-1], drug, "", 1) ///
> ) ///
> )) if _n > 1
drug_regimen was str3 now str19
(12 real changes made)
. /* The value of "drug_regimen" at the end (last row) of the "event"
> is the current list of drugs as of the end of that event day */
. list, noobs sepby(event_nr) abbreviate(20)
+-------------------------------------------------------------------+
| ID event_nr action drug calendar_dt drug_regimen |
|-------------------------------------------------------------------|
| 2 1 Add 3TC 1995-08-24 3TC |
| 2 1 Add ZDV 1995-08-24 ZDV 3TC |
|-------------------------------------------------------------------|
| 2 2 Add LOV 1996-08-07 LOV ZDV 3TC |
|-------------------------------------------------------------------|
| 2 3 Add IDV 1997-11-14 IDV LOV ZDV 3TC |
| 2 3 Add d4T 1997-11-14 d4T IDV LOV ZDV 3TC |
| 2 3 Remove LOV 1997-11-14 d4T IDV ZDV 3TC |
| 2 3 Remove ZDV 1997-11-14 d4T IDV 3TC |
|-------------------------------------------------------------------|
| 2 4 Add IDV 2000-06-15 IDV d4T IDV 3TC |
| 2 4 Add RTV 2000-06-15 RTV IDV d4T IDV 3TC |
| 2 4 Remove IDV 2000-06-15 RTV d4T IDV 3TC |
|-------------------------------------------------------------------|
| 2 5 Remove IDV 2003-02-13 RTV d4T 3TC |
| 2 5 Remove d4T 2003-02-13 RTV 3TC |
| 2 5 Remove 3TC 2003-02-13 RTV |
| 2 5 Remove RTV 2003-02-13 |
+-------------------------------------------------------------------+
. by event_nr: keep if _n == _N
(9 observations deleted)
.
. // Finish with the tally of drugs in the regimen
. generate byte drug_tally = wordcount(drug_regimen)
. list ID event_nr calendar_dt drug_regimen drug_tally, noobs abbreviate(20)
+------------------------------------------------------------+
| ID event_nr calendar_dt drug_regimen drug_tally |
|------------------------------------------------------------|
| 2 1 1995-08-24 ZDV 3TC 2 |
| 2 2 1996-08-07 LOV ZDV 3TC 3 |
| 2 3 1997-11-14 d4T IDV 3TC 3 |
| 2 4 2000-06-15 RTV d4T IDV 3TC 4 |
| 2 5 2003-02-13 0 |
+------------------------------------------------------------+
.
. exit
end of do-file
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/