BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
lillymaginta
Lapis Lazuli | Level 10
data test;
input id$2. start  end ;
attrib start format =date9. informat=date9.;
attrib end format =date9. informat=date9.;
datalines;
1 01JAN2015 14FEB2015
1 18FEB2015 30APR2015

2 01jan2015 28JAN2015
2 01apr2015 30apr2015
3 01JAN2015 14FEB2015 3 15FEB2015 15MAR2015 3 20MAR2015 30APR2015 ; run;

I have the above data, there are multiple records per id I want to apply the following criteria:

-keep all records with more than 60 days gap between end of one period and the start of the other

-keep only the first record if if the end dat of one period and the start of ther others are less than 60 days

2 01apr2015 30apr2015

 

 

Output 

1 01JAN2015 14FEB2015

2 01jan2015 28JAN2015

2 01apr2015 30apr2015

3 01JAN2015 14FEB2015

 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
data test;
input id$2. start  end ;
attrib start format =date9. informat=date9.;
attrib end format =date9. informat=date9.;
datalines;
1 01JAN2015 14FEB2015
1 18FEB2015 30APR2015
2 01jan2015 28JAN2015
2 01apr2015 30apr2015
3 01JAN2015 14FEB2015
3 15FEB2015 15MAR2015
3 20MAR2015 30APR2015
;
run;

data want;
set test;
by id;
l=lag(end);
if first.id then f=1;
else do;
if  start-l>60 then f=1;
else f=0;
end;
if f;
drop l f;
run;

View solution in original post

3 REPLIES 3
novinosrin
Tourmaline | Level 20
data test;
input id$2. start  end ;
attrib start format =date9. informat=date9.;
attrib end format =date9. informat=date9.;
datalines;
1 01JAN2015 14FEB2015
1 18FEB2015 30APR2015
2 01jan2015 28JAN2015
2 01apr2015 30apr2015
3 01JAN2015 14FEB2015
3 15FEB2015 15MAR2015
3 20MAR2015 30APR2015
;
run;

data want;
set test;
by id;
l=lag(end);
if first.id then f=1;
else do;
if  start-l>60 then f=1;
else f=0;
end;
if f;
drop l f;
run;
lillymaginta
Lapis Lazuli | Level 10

Thank you! 

novinosrin
Tourmaline | Level 20

Hi @lillymaginta  You are welcome. Plus, Thank you for posting the sample HAVE and WANT clearly. That makes it so much convenient. 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 3 replies
  • 1271 views
  • 1 like
  • 2 in conversation