Statalist The Stata Listserver


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

st: looping over bys groups?


From   Jeph Herrin <[email protected]>
To   statalist <[email protected]>
Subject   st: looping over bys groups?
Date   Fri, 03 Nov 2006 13:16:24 -0500

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.


thanks in advance,
Jeph
--
Jeph Herrin
[email protected]











*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index