I want to mark the latest observation in an unbalanced dataset with panel data

Timevaribel: t (varies som 0 to 17)

person id: id


The program code I use now:

bysort id t: generate last_obs = sum(t==17)==1 & sum(t[n-1]==2)==0


But the problem is that it only marks the persons with a time variable = 17 year and it does not mark the persons with less observations that 17


So how do I generate a variable that marks the latest observation no matter how long the time variabel is?