Hi SAS Community,
I need in your help in the below regard.
I have a data set like this:-
ID | StartDate |
1 | 9-May-12 |
1 | 10-May-12 |
1 | 10-May-12 |
1 | 30-May-12 |
1 | 30-Jun-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 |
And want an output like this:-
ID | StartDate | Line |
1 | 9-May-12 | 1 |
1 | 10-May-12 | 1 |
1 | 10-May-12 | 1 |
1 | 30-May-12 | 2 |
2 | 17-May-12 | 1 |
2 | 30-May-12 | 1 |
2 | 31-May-12 | 1 |
2 | 26-Jun-12 | 2 |
3 | 26-Jun-12 | 1 |
3 | 29-Jun-12 | 1 |
3 | 27-Jul-12 | 2 |
3 | 25-Aug-12 | 3 |
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.
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;
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;
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: