BookmarkSubscribeRSS Feed
Melk
Lapis Lazuli | Level 10

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?

1 REPLY 1
novinosrin
Tourmaline | Level 20

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

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 670 views
  • 0 likes
  • 2 in conversation