Correct way to calculate time to an event variable Is this the correct way to calculate time to an event(death) for survival analysis?
t2death column calculated using SQL as:
case when death_dt is not null 
then death_dt - admission_dt else study_end_dt - admission_dt end as t2death

The final data looks like:
id  time0       study_end_dt    admission_dt    death_dt        t2death
1   2021-05-24  2022-08-31      2021-05-24      NA              464
2   2019-12-10  2022-08-31      2020-01-19      2020-01-23      4
3   2020-10-14  2022-08-31      2020-11-22      2020-12-03      11
4   2021-11-23  2022-08-31      2021-11-26      NA              278

My concern is that there is t2death value for id: 1, 4, but they are still alive.

*

*How does a survival analysis algorithm know that they have been
censored?

*Any reading resources or explanation would be greatly appreciated.


For question 1: Software expects an indicator variable to distinguish time values that represent events from those that represent censoring. The coding depends on the software. In R for this type of data, 0 is typically used for censored and 1 for an event. In contrast, at one time at least, Matlab used opposite coding; that led to some interesting conversations with a colleague until we figured out that difference. That coding can be extended to allow for multiple types of state transitions.
For question 2: The vignettes of the R survival package can be very helpful. The main vignette, "The survival package," is a good overview. Other vignettes go into detail about specific aspects of survival analysis. This Cross Validated site has some
Book suggestions for survival analysis and about 2000 pages to date on the topic.
