Sample 24574: Calculate the number of years, months, and days between two dates
The sample code on the Full Code tab illustrates how to 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.
The date of birth (DOB) is read from DATALINES. Today's date is computed with the TODAY function. The number of years, months, and days between the two dates are computed using the INTCK and INTNX functions. The value 'S' used for the optional alignment parameter in the INTNX function specifies that the date that is returned has the same alignment as the input date. The optional fourth argument in the INTCK function, 'C', specifies that continuous time is measured. The interval is shifted based on the starting date. See the documentation for these in the SAS
® Functions and Call Routines Reference.
data a;
input @1 dob : mmddyy10.;
/* Get the current date from operating system */
tod=today();
/* Find difference in days, months, and years between start and end dates. */
years = intck('year', dob, tod, 'c');
months = intck('month', intnx('year', dob, years, 's'), tod, 'c');
days = tod - intnx('month', dob, months + years*12, 's');
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
;
run;
proc print;
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.
Obs dob tod years months days
1 01/01/1970 08/17/2016 46 7 16
2 02/28/1992 08/17/2016 24 5 20
3 01/01/2000 08/17/2016 16 7 16
4 02/28/2000 08/17/2016 16 5 20
5 02/29/2000 08/17/2016 16 5 19
6 03/01/2000 08/17/2016 16 5 16
7 05/10/1990 08/17/2016 26 3 7
8 05/11/1990 08/17/2016 26 3 6
9 05/12/1990 08/17/2016 26 3 5
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 |