Your definition of the logic required still leaves room for interpretation.
Please provide a data "have" table created via fully tested SAS code posted here that covers all the different cases. Then show us the desired result.
If you can't spend the time to provide such representative sample data via a fully working and tested SAS data step here then I can't spend more time trying to provide a solution for you.
It sounds like what you want is the first observation per group where a new group starts when:
It would be easier if you had already assigned the grouping variable, but we can just convert your current FLAG variable into one that only has two possible values and then use that.
data have;
input id date :date. flag;
format date date9.;
cards;
1 1jul2021 .
1 1jul2021 .
1 6jul2021 1
1 10jul2021 4
1 16jul2021 2
1 18jul2021 1
2 1jul2021 .
2 6jul2021 .
;
data want;
set have ;
by id ;
any_flag=not missing(flag);
if first.id or any_flag ne lag(any_flag) or dif(date)=>5 ;
run;
Obs id date flag any_flag 1 1 01JUL2021 . 0 2 1 06JUL2021 1 1 3 1 16JUL2021 2 1 4 2 01JUL2021 . 0 5 2 06JUL2021 . 0
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Select SAS Training centers are offering in-person courses. View upcoming courses for: