Sample 24574: Calculate number of years, months, and days between two dates
Determine the exact number of years, months, and days between two SAS date values.
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
data a;
input @1 dob mmddyy10.;
/* Get the current date from operating system */
tod=today();
/* Determine number of days in the month prior to current month */
bdays=day(intnx('month',tod,0)-1);
/* Find difference in days, months, and years between */
/* start and end dates */
dd=day(tod)-day(dob);
mm=month(tod)-month(dob);
yy=year(tod)-year(dob);
/* If the difference in days is a negative value, add the number */
/* of days in the previous month and reduce the number of months */
/* by 1. */
if dd < 0 then do;
dd=bdays+dd;
mm=mm-1;
end;
/* If the difference in months is a negative number add 12 */
/* to the month count and reduce year count by 1. */
if mm < 0 then do;
mm=mm+12;
yy=yy-1;
end;
format dob tod mmddyy10.;
datalines;
01/01/1970
02/28/1992
01/01/2000
02/28/2000
02/29/2000
03/01/2000
05/10/1990
05/11/1990
05/12/1990
;
proc print label;
label dob='Date of Birth'
tod="Today's Date"
dd='Difference in Days'
mm= 'Difference in Months'
yy='Difference in Years';
var dob tod yy mm dd;
run;
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
Date of Today's Difference Difference Difference
Obs Birth Date in Years in Months in Days
1 01/01/1970 09/05/2006 36 8 4
2 02/28/1992 09/05/2006 14 6 8
3 01/01/2000 09/05/2006 6 8 4
4 02/28/2000 09/05/2006 6 6 8
5 02/29/2000 09/05/2006 6 6 7
6 03/01/2000 09/05/2006 6 6 4
7 05/10/1990 09/05/2006 16 3 26
8 05/11/1990 09/05/2006 16 3 25
9 05/12/1990 09/05/2006 16 3 24
Determine the exact number of years, months, and days between two SAS date values.
| Type: | Sample |
| Topic: | SAS Reference ==> Functions ==> Date and Time SAS Reference ==> DATA Step
|
| Date Modified: | 2009-07-29 17:28:41 |
| Date Created: | 2004-09-30 14:08:55 |
Operating System and Release Information
| SAS System | Base SAS | All | n/a | n/a |