Sample 24585: Compute the difference between two SAS® date values or two SAS® datetime values
The sample code on the Full Code tab illustrates how to calculate the difference in days between two SAS date values and calculate the difference in seconds between two SAS datetime values. Use mathematical operations to convert the difference in seconds of the two SAS datetime values to be represented as the difference in hours or as the difference in days.
You can see the full list of informats in the Dictionary of Informats so that you can convert your values to SAS date and datetime 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.
/* Sample 1 - Calculate the difference between two SAS dates */
/* */
/* A SAS date value represents the number of days between January 1, */
/* 1960 and the specific date. To produce the difference in days */
/* between two SAS date values, take the more recent date and subtract */
/* the other date. */
data dates;
/* Date constant syntax -- 'ddmmmyy'd */
date1='19Jul2000'd;
date2='19Jan2000'd;
days=date1-date2;
put "Difference in " days=;
run;
/* Sample 2 -- Calculating the difference between two SAS datetimes */
/* */
/* A SAS datetime value represents the number of seconds since */
/* midnight of January 1, 1960 and the specified datetime. To */
/* produce the difference in seconds between two SAS datetime values, */
/* take the greater of the two datetime values and subtract the other */
/* datetime. To convert the difference in seconds to represent the */
/* difference in hours, divide the difference in seconds by 3600 */
/* (60 seconds x 60 minutes) and use the ROUND function to round the */
/* value up to the nearest hour. To convert the difference in seconds */
/* to represent the difference in days, divide the difference in */
/* seconds by 86400 (60 seconds x 60 minutes x 24 hours) and use the */
/* ROUND function to round the value up to the nearest day. */
data datetime;
/* Datetime constant syntax -- 'ddmmmyy:hh:mm:ss<.s>'dt */
dtime1='01Jun2000:08:00:00'dt;
dtime2='31May2000:16:00:00'dt;
seconds=dtime1-dtime2;
hours=round((dtime1-dtime2)/3600);
days=round((dtime1-dtime2)/86400);
put "Difference in " seconds= ;
put "Difference in " hours= ;
put "Difference in " days= ;
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.
OUTPUT from Sample 1's PUT statement
Difference in DAYS=182
OUTPUT from Sample 2's PUT statements
Difference in SECONDS=57600
Difference in HOURS=16
Difference in DAYS=1
Type: | Sample |
Topic: | SAS Reference ==> DATA Step
|
Date Modified: | 2005-12-08 11:34:07 |
Date Created: | 2004-09-30 14:08:56 |
Operating System and Release Information
SAS System | Base SAS | All | n/a | n/a |