Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: looping over bys groups?


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: looping over bys groups?
Date   Fri, 3 Nov 2006 19:43:50 -0000

I only have understood part of this. I don't understand any of 
the stuff about number of days at all. Perhaps there are some U.S. 
specific details here which you are assuming understood. 

The categories, with an arbitrary code for discussion, 
seem to include first (1) add (2) switch (3). 

Start at the "first"s. 

bysort scrssn (date) : gen byte code = 1 if _n == 1 

At this point everything else in -code- is missing. 

That leaves the "add"s, which are later but the same
as the first. 

by scrssn : replace code = 2 if missing(code) and code==code[1] 

Then everything else is "switch": 

replace code = 3 if missing(code) 

I hope this helps a bit. 

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

Jeph Herrin
 
> This has me tearing my (sparse enough) hair out, thought
> I'd share that experience with others...
> 
> I have prescription data on patients (-id-); once they start on a
> diabetic medication (-medcat-), they can either stay on it, switch, or
> add a new one. For each prescription, there is the number of -days-
> the script is good for. Here is one patient, where I have 
> also calculated
> the -gap- in days between each fill date:
> 
>    +--------------------------------------------+
>    |   scrssn        date   medcat   days   gap |
>    |--------------------------------------------|
>    |     1081   16 Feb 00        1     90     . |
>    |     1081   05 May 00        6     90    79 |
>    |     1081   06 May 00        1     90     1 |
>    |     1081   25 Jul 00        2     90    80 |
>    |     1081   01 Aug 00        5     90     7 |
>    |     1081   26 Sep 00        1     90    56 |
>    |     1081   22 Oct 00        5     90    26 |
>    |     1081   01 Nov 00        2     90    10 |
>    |     1081   23 Jan 01        1     90    83 |
>    |     1081   01 Feb 01        2     90     9 |
> 
> 
> So this patient started on medcat=1, for 90 days, but after 79 days
> got a different med, medcat=2, but refilled the original medcat=1
> on day 80. This means that the second record indicates an "add"
> instead of a "switch". However, the next time they got their start
> med was on 26 Sep 2000, 80+7+56 = 143 days after 6 May, so the
> med on 25 Jul 2000 represents "switch" to medcat=2, and the med
> on 26 Sep 2000 represents a switch back to the original med.
> 
> What I need to do is operationalize this logic, and mark every
> record that is not the index med (first medication) as either
> a switch, an add, or a continuation. I have written piles of
> useless code for this today, and imagine I am missing some
> very useful insight/adofile/nerve that would do the trick. Rather
> than paste in the broken bits here, I thought I'd leave it open
> for free-thinking listers... Any pointers?
> 
> Other info:
>   it is just chance that all of the -days- here are 90; many patients
>       also have 14 & 30 day scripts)
>   there are only 6 distinct -medcat- medication categories, so they
>       could be handled one at a time
>   as long as the last record is within the -days- covered by the last
>       switched or continued medcat, it is considered an -add-
> 
> 
> I keep thinking the solution is of the form
> 
>    bys scrssn (date) : <loop over 1/_N for each record>
> 
> but can't see a way to do it.

*
*   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/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index