Cox model: advice on constructing time varying exposure to drugs I am performing a survival analysis on medical registry data and would like to model drug exposure to estimate a continuous dose-response curve. I have drug data like this:

where ATC is the unqiue code of the drug, and DDD_pack is the number of days the pack of medication will cover assuming the person is taking the standard dose for that particular drug. Actual dose is unknown.
I'm having problems deciding the best way to construct the exposure variable for the dose response. My initial thoughts are to somehow include DDD pack as a time varying variable in a Cox model. However, I am unsure of how to decide on the time step / interval periods. For example, I could consider interval periods of length 90 days and compute the total DDD out of those 90 days as a proportion. Or I could chose 180 day intervals, or shorter ones. Is there a way to decide? There is also the problem that some people may actually be on a smaller or larger dose than the "standard" and hence the "real" DDD_pack might be somewhat less than or larger than the estimated values in the table. I thought perhaps of looking for patterns in the date dispensed column but it then got confusing since some people might be taking several drugs at once.
I would very much appreciate some advice for my analysis, in particlar how to chose the interval / period length for constructing the time depednant variable (drug exposure) for the Cox model.
 A: For a Cox model, the statistical principle to keep in mind is how the model incorporates covariate values. At each event time, the model examines the covariate values in place at that event time for the individual with the event against the values at that event time for all individuals still at risk. There is no incorporation of prior exposure history into the model unless you construct a predictor that includes a measure of exposure history. The way you are thinking about the association of the drug with outcome thus determines how to present the drug information to the software. You have to make sure that the software knows, for all event times, your intended measure of drug exposure for all those still at risk. That determines how you need to structure your data set.
So the big question is what your intended measure of drug exposure should be. As you can't know whether the participants actually took the drug as prescribed, you will probably have to assume that all the prescribed drug was taken except for time periods that end in death or other terminal event. The case of id 659 that you show suggests that the net dose per day could be less than prescribed, as packs providing 33.3 or 16.7 days of drug lasted much longer than those periods, based on the intervals between refills. Should you divide the DDD_pack value by the number of days until the next refill, at least for time intervals that don't end in death? What's the best way to estimate drug exposure for time periods that end in death? That choice will be vey important and runs a risk of substantial bias if you just divide the last DDD_pack value by the number of days until death. Should there be some smoother average of drug exposure, or some measure of cumulative exposure?
Those considerations aren't strictly statistical. They need to be based on understanding of the subject matter. Discuss these issues with colleagues who are familiar with the details of the data and the underlying biomedical question you are addressing. Once you decide on a measure of estimated drug exposure that makes sense on that basis, then make sure that the values of your measure are available to the software, at each event time in the data set, for all individuals still at risk at that event time. That's typically done by the startTime, stopTime, event structure in the counting-process data format.
A: I agree with @Edm answer, but I want to add something as I worked on a similar dataset some years ago. The first approach is naturally to investigate your outcome (which, if I am not wrong, you don't report in your question) using the full exposure history and investigate complex temporal dependencies trying different exposure definitions, temporal windows and lags.
Nonetheless, there is a different approach that might be useful and complementary to the former. This is by using sequence mining and that can be used to have a look at patterns of prescriptions as a whole. Let's say your exposure to a medication can defined as a categorical time-varying variable (e.g. binary: 1=presence, o=absence) across a daily timescale or whatever timescale you eventually decide to define. Imagine that, for each subject you then define a sequence of days coded as 0 or 1 (in the binary case). This represents the entire exposure history of the subjects. With it you can explore patterns sequences within and between individuals using sequence mining tools that are already developed for other research areas such as genomics. You can compute distances among sequences, explore the most recurring patterns, cluster and so on. It also carries interesting visualizations tools!
I am claiming that this approach does miracles, but it's a nice complementary way to describe and investigate this complex categorical time-series.
I wrote a small paper on the topics called "Use of State Sequence Analysis in Pharmacoepidemiology: A Tutorial", which is a very very simple intro to it (it was my undergrad thesis after all), but includes a lot of references to other works (very recommended). I also found a more detailed introduction in a recent pre-print ("Mining and evaluation of patients' diagnostic therapeutic paths through state sequences analysis") that came out in the end of last year. In any case, the most straightforward implementation of this method uses the R package TraMineR and, once you get the hang of it, it is much simpler than any time-varying survival/case-control analysis you are planning to work on.  Just be mindful the list of publications on the package website it's not very updated.
Regarding your idea of using dlnm I recommend this example of code (on Git Hub 2014_gasparrini_StatMed_Rcodedata) the author of dlnm package ( Gasparrini A.) used to carry out the analyses for the paper "Modeling exposure–lag–response associations with distributed lag non-linear models". If you delve into the functions he created and used to define asbestos exposure, you might find some helpful stuff!
