HI @Melk Do you mean the following by any chance?
data have;
input ID GROUP $ DATE :mmddyy10.;* FLAG;
format date mmddyy10.;
cards;
1 A 1/1/2020 1
1 A 1/2/2020 1
1 B 1/3/2020
2 A 1/1/2020
2 A 1/8/2020
2 B 1/4/2020 1
2 B 1/1/2020 1
;
data want;
merge have have(firstobs=2 rename=(date=_date group=_group));
if lag(id)=id and lag(group)=group and abs(dif(date))<=6 then Flag=1;
else if group=_group and 0<abs(_date-date)<=6 then Flag=1;
drop _:;
run;
proc print noobs;run;
| 1 |
A |
01/01/2020 |
1 |
| 1 |
A |
01/02/2020 |
1 |
| 2 |
B |
01/03/2020 |
. |
| 2 |
A |
01/01/2020 |
. |
| 2 |
A |
01/08/2020 |
. |
| 2 |
B |
01/04/2020 |
1 |
| 2 |
B |
01/01/2020 |
1 |