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. 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 610 views
  • 1 like
  • 2 in conversation