@ShanshanCheng Make it a habit to provide sample data as text - ideally shared via a data step that creates it. Screenshots are not very useful if you're after tested code as solution.
Does below return what you're after?
data have;
infile datalines truncover dlm=',' dsd;
input subject_id:$5. avalcn:best32. adtm:e8601dt.;
format adtm datetime20.;
datalines;
A,1,2024-04-14T01:36
A,1,2024-04-14T08:36
A,0,2024-04-14T20:05
A,1,2024-04-15T08:38
A,1,2024-04-15T20:03
A,1,2024-04-16T08:50
B,1,2024-05-14T01:36
B,1,2024-05-14T08:36
B,1,2024-05-15T08:38
B,1,2024-05-15T20:03
B,1,2024-05-16T08:50
;
data want(drop=_:);
set have nobs=_nobs;
by subject_id adtm;
if avalcn=0 then return;
retain _start_obs;
_start_obs=max(_start_obs,_n_+1);
do i=_start_obs to _nobs;
set have(keep=subject_id avalcn adtm
rename=(subject_id=_subject_id avalcn=_avalcn adtm=_adtm))
point=i;
if _avalcn=0 or _subject_id ne subject_id then leave;
if _adtm-adtm>=24*3600 then
do;
select_flag=1;
_start_obs=i;
leave;
end;
end;
run;
proc print data=want;
run;
... View more