Announcement

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

  • Follow changes of multiple variables across time

    Dear Statalist,

    I am currently using STATA version 16 with a dataset containing 11,828 observations. For each subject (identified by maskid), I have a list of medications (MEDNAME1 to MEDNAME4) with corresponding start and stop dates. I follow each subject longitudinally through multiple visits. I would appreciate your help in the data analysis.

    1) Is there a way to track changes in the medication list across visits? For example, if a patient is on “diltiazem” during the first visit but not during the second visit, how can I identify this change? Conversely, if a patient is on “diltiazem” during the first visit, how can I determine if they are still on it during the third visit?

    2) Is there a way to determine which medication is replaced during a visit? For example, if "diltiazem" is replaced by "spironolactone" during the second visit, how can I identify this change? From question 1, I will know that "spironolactone" has been started, but I also need to know if "spironolactone" is replacing another medication.

    3) Additionally, each visit includes a blood pressure check. What is the best approach to analyze the effect of medication changes on blood pressure over time? Specifically, if a patient stays on “diltiazem” for 100 days versus 10 days, how can I account for the duration of treatment when assessing its impact on blood pressure changes?

    Thank you very much for your help!

    Code:
    clear
    input str6 maskid int formdays str57 medname1 int(daysstart1 daysstop1) str78 medname2 int(daysstart2 daysstop2) str64 medname3 int(daysstart3 daysstop3)
    "S00087"  155 "diltiazem cd"    64    . "losartan"        85    . "doxazosin"       85    .
    "S00087"  206 "diltiazem cd"    64    . "losartan"        85    . "doxazosin"       85  206
    "S00087"  218 "diltiazem cd"    64    . "losartan"        85    . "spironolactone" 119    .
    "S00087"  249 "diltiazem cd"    64    . "losartan"        85  249 "spironolactone" 119    .
    "S00087"  353 "diltiazem cd"    64    . "spironolactone" 119    . "metoprolol"     249    .
    "S00087"  406 "diltiazem cd"    64    . "spironolactone" 119    . "metoprolol"     249    .
    "S00087"  501 "diltiazem cd"    64    . "spironolactone" 119    . "losartan"       368    .
    "S00087"  826 "spironolactone" 119    . "amlodipine"     435    . "losartan"       732    .
    "S00087"  119 "furosemide"     -18  119 "diltiazem cd"    64    . "losartan"        85    .
    "S00087"  189 "diltiazem cd"    64    . "losartan"        85    . "doxazosin"       85    .
    "S00087" 1458 "spironolactone" 119    . "amlodipine"     435    . "diltiazem"      732    .
    "S00087" 1096 "spironolactone" 119    . "amlodipine"     435    . "losartan"       732    .
    "S00087" 1380 "spironolactone" 119    . "amlodipine"     435    . "diltiazem"      732    .
    "S00087" 1011 "spironolactone" 119    . "amlodipine"     435    . "losartan"       732    .
    "S00087"  637 "diltiazem cd"    64    . "spironolactone" 119    . "losartan"       368    .
    "S00087" 1317 "spironolactone" 119    . "amlodipine"     435    . "diltiazem"      732    .
    "S00087"  322 "diltiazem cd"    64    . "spironolactone" 119    . "losartan"       249  322
    "S00087"  732 "diltiazem cd"    64  732 "spironolactone" 119    . "losartan"       368  732
    "S00087"  917 "spironolactone" 119    . "amlodipine"     435    . "losartan"       732    .
    "S00087"  368 "diltiazem cd"    64    . "spironolactone" 119    . "metoprolol"     249    .
    "S00087"  549 "diltiazem cd"    64    . "spironolactone" 119    . "losartan"       368    .
    "S00087"  435 "diltiazem cd"    64    . "spironolactone" 119    . "metoprolol"     249  435
    "S00087" 1286 "spironolactone" 119    . "amlodipine"     435    . "losartan"       732 1286
    "S00087"  281 "diltiazem cd"    64    . "spironolactone" 119    . "losartan"       249    .
    "S00087" 1193 "spironolactone" 119    . "amlodipine"     435    . "losartan"       732    .
    "S00193"  455 "diovan"          33    . "spironolactone"  33    . "amlodipine"      55    .
    "S00193"  734 "diovan"          33    . "spironolactone"  33    . "amlodipine"      55    .
    "S00193" 1019 "diovan"          33    . "spironolactone"  33    . "amlodipine"      55    .
    "S00193"  823 "diovan"          33    . "spironolactone"  33    . "amlodipine"      55    .
    "S00193"  371 "diovan"          33    . "spironolactone"  33    . "amlodipine"      55    .
    "S00193"   91 "diovan"          33    . "spironolactone"  33    . "amlodipine"      55    .
    "S00193"  180 "diovan"          33    . "spironolactone"  33    . "amlodipine"      55    .
    "S00193"   69 "diovan"          33    . "spironolactone"  33    . "amlodipine"      55    .
    "S00193"  133 "diovan"          33    . "spironolactone"  33    . "amlodipine"      55    .
    "S00193"  550 "diovan"          33    . "spironolactone"  33    . "amlodipine"      55    .
    "S00193"   32 "amlodipine"      -7    . "diovan"           1   32 "diovan"          33    .
    "S00193"  273 "diovan"          33    . "spironolactone"  33    . "amlodipine"      55    .
    "S00193" 1104 "diovan"          33 1104 "spironolactone"  33 1104 "amlodipine"      55    .
    "S00193"  636 "diovan"          33    . "spironolactone"  33    . "amlodipine"      55    .
    "S00193"  952 "diovan"          33    . "spironolactone"  33    . "amlodipine"      55    .
    "S00193"   54 "amlodipine"      -7   54 "diovan"          33    . "spironolactone"  33    .
    "S001E5" 1044 "doxazosin"      590    . "chlorthalidone" 590    . "lisinopril"     617    .
    "S001E5" 1079 "doxazosin"      590    . "chlorthalidone" 590    . "lisinopril"     617    .
    "S001E5" 1129 "doxazosin"      590    . "chlorthalidone" 590 1099 "lisinopril"     617    .
    "S00248"  724 "lisinopril"       0    . "amlodipine"      31    . "doxazosin"       69    .
    "S00248"  908 "lisinopril"       0    . "amlodipine"      31    . "doxazosin"       69    .
    "S00248"  185 "chlorthalidone"   0    . "lisinopril"       0    . "amlodipine"      31    .
    "S00248" 1092 "lisinopril"       0 1092 "amlodipine"      31    . "doxazosin"       69    .
    "S00248" 1180 "amlodipine"      31    . "doxazosin"       69    . "metoprolol"     384    .
    "S00248"  384 "lisinopril"       0    . "amlodipine"      31    . "doxazosin"       69    .
    "S00248"  356 "lisinopril"       0    . "amlodipine"      31    . "doxazosin"       69    .
    "S00248"  605 "lisinopril"       0    . "amlodipine"      31    . "doxazosin"       69    .
    "S00248"  418 "lisinopril"       0    . "amlodipine"      31    . "doxazosin"       69    .
    "S00248"  824 "lisinopril"       0    . "amlodipine"      31    . "doxazosin"       69    .
    "S00248"  237 "chlorthalidone"   0    . "lisinopril"       0    . "amlodipine"      31    .
    "S00248"  501 "lisinopril"       0    . "amlodipine"      31    . "doxazosin"       69    .
    "S00248"  284 "chlorthalidone"   0    . "lisinopril"       0    . "amlodipine"      31    .
    "S00248"  997 "lisinopril"       0    . "amlodipine"      31    . "doxazosin"       69    .
    "S00248"  209 "chlorthalidone"   0    . "lisinopril"       0    . "amlodipine"      31    .
    "S00248"  467 "lisinopril"       0    . "amlodipine"      31    . "doxazosin"       69    .
    "S00248"  312 "chlorthalidone"   0  312 "lisinopril"       0    . "amlodipine"      31    .
    "S00248"  537 "lisinopril"       0    . "amlodipine"      31    . "doxazosin"       69    .
    "S00248"  265 "chlorthalidone"   0    . "lisinopril"       0    . "amlodipine"      31    .
    "S00304"  532 "metoprolol"     193  512 "valsartan"      503    . "carvedilol"     512    .
    "S00304"  634 "valsartan"      503  546 "carvedilol"     512  546 "furosemide"     512  550
    "S00341"  274 "spironalactone" -27    . "doxazosin"       28    . "chlorthalidone"  29    .
    "S00341"  554 "spironalactone" -27    . "doxazosin"       28    . "chlorthalidone"  29    .
    "S00341"   64 "spironalactone" -27    . "amlodipine"     -27    . "bystolic"       -27   64
    "S00341" 1118 "spironalactone" -27    . "metoprolol"      90    . "amlodipine"     240    .
    "S00341"   28 "spironalactone" -27    . "amlodipine"     -27    . "bystolic"       -27    .
    "S00341" 1016 "spironalactone" -27    . "doxazosin"       28 1016 "chlorthalidone"  29    .
    "S00341"  645 "spironalactone" -27    . "doxazosin"       28    . "chlorthalidone"  29    .
    "S00341"  358 "spironalactone" -27    . "doxazosin"       28    . "chlorthalidone"  29    .
    "S00341"  239 "spironalactone" -27    . "amlodipine"     -27  239 "doxazosin"       28    .
    "S00341"  832 "spironalactone" -27    . "doxazosin"       28    . "chlorthalidone"  29    .
    "S00341"  743 "spironalactone" -27    . "doxazosin"       28    . "chlorthalidone"  29    .
    "S00341"  147 "spironalactone" -27    . "amlodipine"     -27    . "doxazosin"       28    .
    "S00341"  820 "spironalactone" -27    . "doxazosin"       28    . "chlorthalidone"  29    .
    "S00341" 1048 "spironalactone" -27    . "chlorthalidone"  29 1048 "metoprolol"      90    .
    "S00341"   90 "spironalactone" -27    . "amlodipine"     -27    . "doxazosin"       28    .
    "S00341"  120 "spironalactone" -27    . "amlodipine"     -27    . "doxazosin"       28    .
    "S00341"  923 "spironalactone" -27    . "doxazosin"       28    . "chlorthalidone"  29    .
    "S00341" 1251 "spironalactone" -27    . "metoprolol"      90    . "amlodipine"     240    .
    "S00341"  482 "spironalactone" -27    . "doxazosin"       28    . "chlorthalidone"  29    .
    "S00343" 1496 "losartan"       171    . "amlodipine"     393    . "hydralazine"    844    .
    "S00343" 1682 "losartan"       171    . "amlodipine"     393    . "hydralazine"    844    .
    "S00397"  744 "metoprolol"       1    . "spironalactone"  63    . "lisinopril"     374    .
    "S00397"  555 "amlodipine"     -18    . "metoprolol"       1    . "spironalactone"  63    .
    "S00397"  646 "amlodipine"     -18    . "metoprolol"       1    . "spironalactone"  63    .
    "S00397"  692 "metoprolol"       1    . "spironalactone"  63    . "lisinopril"     374    .
    "S00397"  464 "amlodipine"     -18    . "metoprolol"       1    . "spironalactone"  63    .
    "S00397" 1346 "metoprolol"       1    . "spironalactone"  63    . "lisinopril"     374    .
    "S00397"  923 "metoprolol"       1    . "spironalactone"  63    . "lisinopril"     374    .
    "S00397" 1245 "metoprolol"       1    . "spironalactone"  63    . "lisinopril"     374    .
    "S00397" 1042 "metoprolol"       1    . "spironalactone"  63    . "lisinopril"     374    .
    "S00397"  885 "metoprolol"       1    . "spironalactone"  63    . "lisinopril"     374    .
    "S00397" 1565 "metoprolol"       1    . "spironalactone"  63    . "lisinopril"     374 1516
    "S00397"   90 "amlodipine"     -18    . "lisinopril"     -18    . "metoprolol"       1    .
    "S00397"  121 "amlodipine"     -18    . "lisinopril"     -18    . "metoprolol"       1    .
    "S00397"  373 "amlodipine"     -18    . "lisinopril"     -18  373 "metoprolol"       1    .
    end

  • #2
    Thanks for offering a -dataex- example on your first posting. I still see some questions about your data:

    What variable are you using to indicate the sequence of the visit number? That's not obvious to me. What does it mean when "daysstart" is a negative number? Is a medication defined as "replaced" if and only if, for a particular patient, a particular medname* changes from one visit to the next? Within a patient, can a medicine shift from one medname* variable to another between visits for a particular patient? For example, could "metoprolol" be present as medname1 on the first visit, but become "medname3" on the second visit?

    These questions aside, Stata's -by- command and the use of the system variable _n are likely to be necessary here, so you might start by "digesting" -help by- and -help system variables-. The examples within the help are often the most helpful thing when a command is new to you.

    Comment


    • #3
      Sorry, but your data confuses me. Does each observation represent a separate visit? If so, are they in chronological order in the data set as shown? (Related question: what is the variable formdays. At first I thought it might be somehow related to visit date, but it is not always in increasing order within maskid.) If there is not a one-to-one correspondence between observations and visits, how can we tell where the data on one visit ends and another one begins. And how are the variables daysstar* and daysstop* to be understood? They are not date variables, and sometimes they are negative. Are they offsets from some reference date? If so, what is the reference date--and does the reference date change from one observation to the next or is it constant for all observations of the same maskid.

      Concerning question 2, it does not seem possible to answer this question. It is possible, once we figure out the dates in this data, to determine when one medication is discontinued and another one started on the same date. But whether to call that a replacement or simply the co-occurrence of two medication changes requires reading the mind of the prescriber. So unless you have additional data in which the prescriber recorded his or her intentions, this just can't be done.

      Added: Crossed with #2.

      Comment


      • #4
        Thanks, Mike!

        The "formdays" variable represents the day when the patient establishes care with us. For example, a "formdays" value of 14 means the patient had a follow-up visit 14 days after the initial encounter (where "formdays" = 0).
        Similarly, "formdays" = 40 indicates a follow-up 40 days after the first encounter, making it a more recent visit compared to "formdays" = 14.

        The "daystart" variable might be negative if the patient was already on a medication prior to the first encounter.

        The challenge is that the "medname*" shifts over time. For instance, "spironolactone" might be recorded as "medname1" during one visit and as "medname2" in the next visit.
        So, it is correct that "metoprolol" might be listed as "medname1" during the first visit but as "medname3" in the second visit.

        If "metoprolol" is present during the first visit but not during the second visit, and if "spironolactone" is present in the second visit but not in the first visit, then it indicates that "metoprolol" has been replaced by "spironolactone."

        I have tried to track the drug names using the following code:

        ```stata
        # Generate variable "spiro"
        generate byte onspiro = (strpos(strlower(medname1), "spiro") > 0 |
        strpos(strlower(medname2), "spiro") > 0 |
        strpos(strlower(medname3), "spiro") > 0 |
        strpos(strlower(medname4), "spiro") > 0 |
        strpos(strlower(medname5), "spiro") > 0 |
        strpos(strlower(medname6), "spiro") > 0 |
        strpos(strlower(medname7), "spiro") > 0 |
        strpos(strlower(medname8), "spiro") > 0 |
        strpos(strlower(medname9), "spiro") > 0 |
        strpos(strlower(medname10), "spiro") > 0)
        tab onspiro

        # Generate a visit order variable
        sort maskid formdays
        quietly by maskid : gen dup = cond(_N == 1, 0, _n)

        # Apply filters
        bysort maskid (dup) : gen group = ( )
        ```

        However, keeping track of medication changes, especially over time, has become too complicated for me to understand and execute.



        Thanks, Clyde!

        Yes, each observation represents a separate visit and is in chronological order as indicated. "formdays" is a surrogate for the visit day, with "formdays" = 0 being the first encounter. Subsequent "formdays" values, such as 14, indicate the visit was 14 days after the first encounter.

        I apologize for the oversight; it should indeed be sorted in increasing order within each `maskid`, with the highest "formdays" value indicating the latest visit for that patient.

        "daystart*" represents the day a medication is started, and "daystop*" represents the day it is stopped. Negative values for "daystart*" and "daystop*" mean that the patient was on these medications prior to the first encounter.

        If I understand your question correctly, the reference date would be day 0, when the patient first sees us in the clinic.

        I really appreciate your time and prompt responses on this. Please let me know if you have any ideas on how to resolve this.


        Comment


        • #5
          Thanks for the clarification. Working with this data, it seems to have a number of internal inconsistencies. For example, look at S00087's use of losartan. It begins on day 85, and then stops on day 249. So far, so good. But then looking at formdays 322 for this patient, losartan appears again, this time as medname3, and is shown as starting on day 249. So it was both started and stopped on day 249? How does that make sense? Subsequently this patient restarts losartan on day 368--but this is according to a record with markdays 549. In particular, there is no mention of losartan at the visit on day 368. Still, when we get to markday 732, losartan is stopped again. But according to the visit on markday 917, it was actually started on day 732!

          I am trying to organize the data into a timeline for each drug with start and stop events, but things like those mentioned in the preceding paragraph defy this because the timeline would have to be self contradictory. This is only one of several situation like this in the example data.

          So either the meanings of these variables are not quite as you describe them, or the data contains numerous errors that will need to be fixed.

          To chase these situations down yourself, you can run the code below:
          Code:
          reshape long medname daysstart daysstop, i(maskid formdays) j(seq)
          
          frame put _all , into(working)
          frame change working
          duplicates drop
          reshape long days, i(maskid formdays medname seq) j(event) string
          keep maskid medname days event
          drop if missing(days)
          duplicates drop
          
          sort maskid medname days event
          
          frame default: sort maskid formdays seq
          When you are done with that, frame working will contain an attempt at providing each drug with a timeline. It is easy here to spot situations where a drug is both started and stopped on the same date. Frame default contains the original data, but in long layout, so that there are three observations for each formday for each patient (one corresponding to each drug mentioned). In this organization you can see things from the perspective of the sequence of visits.

          Comment

          Working...
          X