Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duration between two dates in SAS

Tags:

sas

I will explain through an example-

suppose I have two dates and I want to find the duration between them in years month date format

start date= 19940412
end date= 20120326

duration of this 17 years 11 months 14 days.

So what code do i write to get this result in sas?

like image 838
Rahul Grover Avatar asked Mar 13 '26 08:03

Rahul Grover


1 Answers

Here's the code you need:

data _null_;
   start_date= '19940412';
   end_date= '20120326';
   /* convert to sas dates */
   start_dt=input(start_date,yymmdd8.);
   end_dt=input(end_date,yymmdd8.);
   /* calculate difference in years */
   years=intck('YEAR',start_dt,end_dt,'C');
   /* recalculate start date */
   start_dt=intnx('YEAR',start_dt,years,'S');
   /* calculate remaining months */
   months=intck('MONTH',start_dt,end_dt,'C');
   /* recalculate start date */
   start_dt=intnx('MONTH',start_dt,months,'S');
   /* calculate remaining days */
   days=intck('DAY',start_dt,end_dt,'C');
   /* results */
   put years= months= days=;
run;

Which gives:

years=17 months=11 days=14
like image 84
Allan Bowe Avatar answered Mar 15 '26 15:03

Allan Bowe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!