# Re: st: Simple longitudinal data question – drug cont

 From n j cox To statalist@hsphsun2.harvard.edu Subject Re: st: Simple longitudinal data question – drug cont Date Fri, 05 Oct 2007 11:22:00 +0100

```-egen- is your friend.

You can count prescriptions in the first half of 2005 by

egen in2005 = total(drug == "microg" &
inrange(datevisit, mdy(1,1,2005), mdy(6,30,2005))), by(patient_id)

and in 2006 similarly by

egen in2006 = total(drug == "microg" &
inrange(datevisit, mdy(1,1,2006), mdy(6,30,2006))), by(patient_id)

You want patients with positive values of both variables.

gen both = (in2005 > 0) & (in2006 > 0)

or equivalently

gen both = in2005 & in2006

-count if both- will count all visits by those patients, regardless
of prescription.

That is not what you need. However,

egen tag = tag(patient_id)

will tag just one observation for each patient. So

count if tag & both

will count patients with prescriptions in both years.

How do I create a variable recording whether any members of a
group (or all members of a group) possess some characteristic?
http://www.stata.com/support/faqs/data/anyall.html

What is true and false in Stata?
http://www.stata.com/support/faqs/data/trueorfalse.html

More generally, I can imagine a fourfold table:

no & no     no & yes
yes & no    yes & yes

and that's programmable, say on the following lines
(rough sketch only, no promises it works)

program twoperiod
version 8
syntax [if] [in] , first(str asis) second(str asis) by(varname)

quietly  {
marksample touse, novarlist
count if `touse'
if r(N) == 0 error 2000

tempvar tag t1 t2 which
egen `tag' = tag(`by') if `touse'
egen `t1' = max((`first') & `touse'), by(`by')
egen `t2' = max((`second') & `touse'), by(`by')
}

tempname lbl
label var `t1' "in first period"
label var `t2' "in second period"
label def `lbl' 0 "no" 1 "yes"
label val `t1' `lbl'
label val `t2' `lbl'

tab `t1' `t2' if `tag'
end

with call like

twoperiod, first(drug == "microg" &
inrange(datevisit, mdy(1,1,2005), mdy(6,30,2005)))
second(drug == "microg" &
inrange(datevisit, mdy(1,1,2006), mdy(6,30,2006)))
by(patient_id)

Nick
n.j.cox@durham.ac.uk

Paul O'Brien

I have a database of clinic visits with 3 variables, patient_id,
datevisit, and drug.

I want to know the number of patients who were prescribed drug
‘microg’ in the first half of 2005 were also prescribed the same drug
during a visit in the first half of 2006.

Or is there a better way to calculate the one-year continuation rate?

Patients can make any number of visits.

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```