I provided an example below for an easy explanation for my question.




I need to do survival analysis using longitudinal data, like the example below.

I need to subtract from the date(year) when diabetes occurred first to the baseline date(year) for the survival analysis to get the diabetes incidence time.




If some subjects (Id=3,5) did not have any diabetes during the following up, their incidence time was calculated by subtracting from their last examined date(year) to the baseline.




I tried using 'tsset' function in STATA; however, I could not figure this problem yet.

id time_n diabete date(year)
1 1 0 1
1 2 1 3
1 3 0 5
2 1 0 1
2 2 0 3
2 3 1 5
2 4 1 7
3 1 0 1
3 2 0 6
4 1 0 1
4 2 0 3
4 3 0 5
4 4 0 6
4 5 0 7
4 6 1 8
5 1 0 1
5 2 0 2
5 3 0 3