I have data with above columns where per reference I have multiple extension start dates and extension end dates
in most case new extension start date doesn't start before pervious extension end date but in some instance new extension start date start before the previous extension end date
I am trying to find total numbers of days per extension but I need to ensure if new extension starts before previous extension end date , then it should take difference of last extension end date and new extension end date
I am not sure how to do this in SAS ,is any one able to help ?
I don't think your last line is correct? The dates do not overlap?
234567891 1-Apr-17 30-Jun-17 90 90
234567891 2-Feb-18 30-May-18 117 90
Assuming I'm correct, this works for me:
data have;
informat ID 8. date_start date_end date11.;
format date_: date9.;
input ID date_start date_end days right_days;
cards;
123456789 7-Aug-12 20-Sep-12 44 44
123456789 5-Mar-13 6-Jun-13 93 93
123456789 10-May-13 20-Jun-13 41 14
123456789 25-Jul-14 30-Sep-14 67 67
123456789 12-Sep-14 30-Oct-14 48 30
123456789 14-Dec-15 20-Jan-16 37 37
123456789 1-Apr-16 30-Jun-16 90 90
123456789 2-Feb-17 30-May-17 117 117
234567891 7-Aug-13 20-Sep-13 44 44
234567891 5-Mar-14 6-Jun-14 93 93
234567891 10-May-14 20-Jun-14 41 14
234567891 29-Jul-15 30-Sep-15 63 63
234567891 1-Sep-15 30-Oct-15 59 30
234567891 14-Dec-16 20-Jan-17 37 37
234567891 1-Apr-17 30-Jun-17 90 90
234567891 2-Feb-18 30-May-18 117 90
;;;;
run;
proc sort data=have;
by id date_start;
run;
data want;
set have;
by id;
prev_date = lag(date_end);
if first.id then prev_date = .;
if prev_date>date_start then date_calc = date_end - prev_date;
else date_calc = date_end - date_start;
check = right_days - date_calc;
run;
@H1M wrote:
Reference Extetion_start_Date Extention_end_date Total_Extension_days Should_be_ext_days
123456789 7-Aug-12 20-Sep-12 44 44
123456789 5-Mar-13 6-Jun-13 93 93
123456789 10-May-13 20-Jun-13 41 14
123456789 25-Jul-14 30-Sep-14 67 67
123456789 12-Sep-14 30-Oct-14 48 30
123456789 14-Dec-15 20-Jan-16 37 37
123456789 1-Apr-16 30-Jun-16 90 90
123456789 2-Feb-17 30-May-17 117 117
234567891 7-Aug-13 20-Sep-13 44 44
234567891 5-Mar-14 6-Jun-14 93 93
234567891 10-May-14 20-Jun-14 41 14
234567891 29-Jul-15 30-Sep-15 63 63
234567891 1-Sep-15 30-Oct-15 59 30
234567891 14-Dec-16 20-Jan-17 37 37
234567891 1-Apr-17 30-Jun-17 90 90
234567891 2-Feb-18 30-May-18 117 90
Sure! And this is also a common asked question so if you search for "overlapping dates" you'll find some examples.
To be able to help you it would be best if you could provide data as text as minimum, definitely not an image. We'd have to type your data out to work with it and it's much easier and faster if you post it as text.
@H1M wrote:
I have data with above columns where per reference I have multiple extension start dates and extension end dates
in most case new extension start date doesn't start before pervious extension end date but in some instance new extension start date start before the previous extension end date
I am trying to find total numbers of days per extension but I need to ensure if new extension starts before previous extension end date , then it should take difference of last extension end date and new extension end date
I am not sure how to do this in SAS ,is any one able to help ?
Please paste it as text directly into the forums, not as an attachment.
I don't think your last line is correct? The dates do not overlap?
234567891 1-Apr-17 30-Jun-17 90 90
234567891 2-Feb-18 30-May-18 117 90
Assuming I'm correct, this works for me:
data have;
informat ID 8. date_start date_end date11.;
format date_: date9.;
input ID date_start date_end days right_days;
cards;
123456789 7-Aug-12 20-Sep-12 44 44
123456789 5-Mar-13 6-Jun-13 93 93
123456789 10-May-13 20-Jun-13 41 14
123456789 25-Jul-14 30-Sep-14 67 67
123456789 12-Sep-14 30-Oct-14 48 30
123456789 14-Dec-15 20-Jan-16 37 37
123456789 1-Apr-16 30-Jun-16 90 90
123456789 2-Feb-17 30-May-17 117 117
234567891 7-Aug-13 20-Sep-13 44 44
234567891 5-Mar-14 6-Jun-14 93 93
234567891 10-May-14 20-Jun-14 41 14
234567891 29-Jul-15 30-Sep-15 63 63
234567891 1-Sep-15 30-Oct-15 59 30
234567891 14-Dec-16 20-Jan-17 37 37
234567891 1-Apr-17 30-Jun-17 90 90
234567891 2-Feb-18 30-May-18 117 90
;;;;
run;
proc sort data=have;
by id date_start;
run;
data want;
set have;
by id;
prev_date = lag(date_end);
if first.id then prev_date = .;
if prev_date>date_start then date_calc = date_end - prev_date;
else date_calc = date_end - date_start;
check = right_days - date_calc;
run;
@H1M wrote:
Reference Extetion_start_Date Extention_end_date Total_Extension_days Should_be_ext_days
123456789 7-Aug-12 20-Sep-12 44 44
123456789 5-Mar-13 6-Jun-13 93 93
123456789 10-May-13 20-Jun-13 41 14
123456789 25-Jul-14 30-Sep-14 67 67
123456789 12-Sep-14 30-Oct-14 48 30
123456789 14-Dec-15 20-Jan-16 37 37
123456789 1-Apr-16 30-Jun-16 90 90
123456789 2-Feb-17 30-May-17 117 117
234567891 7-Aug-13 20-Sep-13 44 44
234567891 5-Mar-14 6-Jun-14 93 93
234567891 10-May-14 20-Jun-14 41 14
234567891 29-Jul-15 30-Sep-15 63 63
234567891 1-Sep-15 30-Oct-15 59 30
234567891 14-Dec-16 20-Jan-17 37 37
234567891 1-Apr-17 30-Jun-17 90 90
234567891 2-Feb-18 30-May-18 117 90
Thanks Reeza yes not all dates are overlapping only some where i need to ensure its not double counting No. of days
thank you for the quick response and it is working fine
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.