Effect of duration of treatment on time to event outcome (overall survival) I am trying to understand how to design an analysis for the following situation. We have the following data collected retrospectively on a group of patients:

*

*Age, Sex, Race, and other variables measured at baseline

*TreatmentStartDate - the date treatment was initiated; also the index date

*TreatmentEndDate - the date treatment was ended

*FollowupDate - the death date or end of followup

*Event - 1 if the FollowupDate was the death date, 0 if censored by end of followup

We want to estimate the effect of duration of treatment on overall survival. However, I am a bit confused how to design the analysis to correctly assess this.
A simple approach would be create an additional variable LengthOfTreatment = TreatmentEndDate - TreatmentStartDate and then do a typical time-to-event analysis with Cox models or Kaplan-Meier, possibly using inverse probability weighting or similar to balance potentially confounding baseline variables.
However, I think this is incorrect because LengthOfTreatment is not truly measured at baseline, but rather afterwards during followup, so there can immortal time bias and reverse causality between the outcome and the LengthOfTreatment, i.e., if you live longer then you are also able to have a longer length of treatment and if you die quickly then you will definitely have a shorter length of treatment.
I am not sure what a correct analysis would look like. This seems like it must be a solved problem? Thank you in advance.
 A: You are right that  there is a problem. At the extreme, suppose no-one ever went off treatment. Survival time would be identical to time on treatment, giving an apparently infinite benefit even if the true benefit was zero.
Doing better than this still requires some information about how treatment length ends up  being the way it is. This is partly a solved problem, but partly a definitively unsolved problem. It helps that you seem to have a single treatment period, so  that  everyone being treated at time $t$  has been treated for the same amount of  time.
Let's start simple.  Suppose that some doctors had a policy of mostly treating people for six months and others had a policy of mostly treating people for a year.  Even if you  didn't  know who'd had which policy, you could count time forward from six months (ignoring anyone who died earlier), and see if the binary variable 'treatment after  six months' was associated with survival after six months. With no more than the usual caveats,  that would  tell you whether longer treatment was better.
In the other direction, suppose some types of patient were mostly treated for six months and other types for twelve months. You could do the same analysis,  but now treatment is completely confounded with the type of patient, so you would need separate information on the survival difference by type of patient, which you may well not be able to get.
Those two extremes cover basically everything, it's  just  that  we  have more than two possible time periods for length of treatment.
At  each  point in  time, some patients stop treatment (but are still followed up) and others continue  (and are still followed up). A Cox model with time-dependent treatment variable(s)  is  the limiting case of the split-by-time analysis as you allow more and more splits.  It will say whether lower hazard for treated people compared to untreated people at the same point in time.  You would  also have to decide how to model time-since-stopping, but that's just an ordinary modelling question; you know  treatment history at time  $t$  and you just need  to decide how  to  code it as a  set  of predictor variables.  This model will not tell you directly whether that difference is caused by treatment.
If there is practice variation, so that otherwise similar patients can have different treatment durations, you could model the time to treatment discontinuation and do matching or an inverse-probability weighting thing to try to estimate whether, for the  same patient, there would be a difference in outcome with different treatment duration.
If there is little or no practice variation, so people mostly stay  on treatment until, say, they can't take any more  or until  they feel better or until their insurance stops paying,  you will have complete confounding between treatment and  the reason for treatment. In that case you would need external information on the effect of the reason for treatment on  survival to either estimate or bound the effect of receiving treatment.
And as a final point, you might well want a time-varying coefficient for your treatment variables as well. That is, it might be true that extra treatment matters for the first month but not beyond that. Again, that's an ordinary modelling question -- you can look at  diagnostics for proportional hazards and  see if the hazard ratios seem to vary with time.
