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 |
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.