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
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.