How to conditionally recode variables based on Gap between dates

Accepted Solution Solved
Reply
Highlighted
Contributor
Posts: 48
Accepted Solution

How to conditionally recode variables based on Gap between dates

[ Edited ]

Hi SAS Community,

 

 

I need in your help in the below regard.

 

I have a data set like this:-

IDStartDate
19-May-12
110-May-12
110-May-12
130-May-12
130-Jun-12
  
217-May-12
230-May-12
231-May-12
226-Jun-12
  
326-Jun-12
329-Jun-12
327-Jul-12
325-Aug-12

 

 


And want an output like this:- 

IDStartDateLine
19-May-121
110-May-121
110-May-121
130-May-122
   
217-May-121
230-May-121
231-May-121
226-Jun-122
   
326-Jun-121
329-Jun-121
327-Jul-122
325-Aug-123

 

Basically, The logic is that if there is a gap of more than 20 between the dates by ID then the Line variable should increase by 1.

 

Thank you for your time and I appreciate your help.

 

Regards,

SC.

 


Accepted Solutions
Solution
3 weeks ago
PROC Star
Posts: 603

Re: How to conditionally recode variables based on Gap between dates

data have;
input ID StartDate :date7.;
format startdate date7.;
datalines;
1 9-May-12
1 10-May-12
1 10-May-12
1 30-May-12
2 17-May-12
2 30-May-12
2 31-May-12
2 26-Jun-12
3 26-Jun-12
3 29-Jun-12
3 27-Jul-12
3 25-Aug-12
;

data want;
set have;
by id;
k=lag(startdate);
if first.id then line=1;
else if intck('day',k,startdate)>=20 then line+1;
drop k;
run;

View solution in original post


All Replies
Solution
3 weeks ago
PROC Star
Posts: 603

Re: How to conditionally recode variables based on Gap between dates

data have;
input ID StartDate :date7.;
format startdate date7.;
datalines;
1 9-May-12
1 10-May-12
1 10-May-12
1 30-May-12
2 17-May-12
2 30-May-12
2 31-May-12
2 26-Jun-12
3 26-Jun-12
3 29-Jun-12
3 27-Jul-12
3 25-Aug-12
;

data want;
set have;
by id;
k=lag(startdate);
if first.id then line=1;
else if intck('day',k,startdate)>=20 then line+1;
drop k;
run;

Contributor
Posts: 48

Re: How to conditionally recode variables based on Gap between dates

Posted in reply to novinosrin
You are of great help. Thank you very much.
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 144 views
  • 0 likes
  • 2 in conversation