I have longitudinal data similar to below:
ID GROUP DATE FLAG
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
I want to flag and and further remove observations for the same ID and GROUP where the dates are within 6 days of each other (plus or minus). How may I do this?
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;
| ID | GROUP | DATE | Flag |
|---|---|---|---|
| 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 |
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.