About SAS Date, Time, and Datetime Values

Definitions

SAS date value
is a value that represents the number of days between January 1, 1960, and a specified date. SAS can perform calculations on dates ranging from A.D. 1582 to A.D. 19,900. Dates before January 1, 1960, are negative numbers; dates after January 1, 1960, are positive numbers.
  • SAS date values account for all leap year days, including the leap year day in the year 2000.
  • SAS date values can reliably tell you what day of the week a particular day fell on as far back as September 1752, when the calendar was adjusted by dropping several days. SAS day-of-the-week and length-of-time calculations are accurate in the future to A.D. 19,900.
  • Various SAS language elements handle SAS date values: functions, formats, and informats.
SAS time value
is a value representing the number of seconds since midnight of the current day. SAS time values are between 0 and 86400.
SAS datetime value
is a value representing the number of seconds between January 1, 1960, and an hour/minute/second within a specified date.
The following figure shows some dates written in calendar form and as SAS date values.
How SAS Converts Calendar Dates to SAS Date Values
Calendar Date versus SAS Date Values

Two-Digit and Four-Digit Years

SAS software can read two-digit or four-digit year values. If SAS encounters a two-digit year, the YEARCUTOFF= option can be used to specify which century within a 100- year span the two-digit year should be attributed to. For example, YEARCUTOFF=1950 means that two-digit years 50 through 99 correspond to 1950 through 1999, while two-digit years 00 through 49 correspond to 2000 through 2049. Note that while the default value of the YEARCUTOFF= option in SAS 9 is 1920, you can adjust the YEARCUTOFF= value in a DATA step to accommodate the range of date values that you are working with at the moment. To correctly handle two-digit years representing dates between 2000 and 2099, you should specify an appropriate YEARCUTOFF= value between 1901 and 2000. For more information, see the YEARCUTOFF= System Option in SAS System Options: Reference.

Five-Digit Years

Although some formats that specify a width large enough to accommodate formatting a five-digit year, such as DATETIME20., the SAS documentation does not display five-digit years.

The Year 2000

Using the YEARCUTOFF= System Option

SAS software treats the year 2000 like any other leap year. If you use two-digit year numbers for dates, you probably need to adjust the default setting for the YEARCUTOFF= option to work with date ranges for your data, or switch to four-digit years. The following program changes the YEARCUTOFF= value to 1950. This change means that all two-digit dates are now assumed to fall in the 100-year span from 1950 to 2049.
options yearcutoff=1950;
data _null_;
   a='26oct02'd;
   put 'SAS date='a;
   put 'formatted date='a date9.;
run;
The PUT statement writes the following lines to the SAS log:
SAS date=15639
formated date=26OCT2002
Note: Whenever possible, specify a year using all four digits. Most SAS date and time language elements support four-digit year values.

Example: How YEARCUTOFF= Affects Two- and Four-Digit Years

The following example shows what happens with data that contains both two and four-digit years. Note how the YEARCUTOFF= option is set to 1920.
data schedule;
   input @1 jobid $ @6 projdate mmddyy10.;
   datalines;
A100 01/15/25
A110 03/15/2025
A200 01/30/96
B100 02/05/00
B200 06/15/2000
;

proc print data=schedule;
   format projdate mmddyy10.;
run;
The resulting output from the PROC PRINT statement looks like this:
Output from the Previous DATA Step Showing Four-Digit Years That Result from Setting YEARCUTOFF= to 1920
                   Days Between Project Start and Project End                  1

                           Obs    jobid      projdate

                            1     A100     01/15/1925
                            2     A110     03/15/2025
                            3     A200     01/30/1996
                            4     B100     02/05/2000
                            5     B200     06/15/2000
Here are some facts to note in this example:
  • In the data lines in the DATA step, the first record contains a two-digit year of 25, and the second record contains a four-digit year of 2025. Because the YEARCUTOFF= system option is set to 1920, the two-digit year defaults to a year in the 1900s in observation number 1. The four-digit year in observation number 2 is unaffected by the YEARCUTOFF= option.
  • The third record is similar to the first and defaults to a year in the 1900s based on the value of YEARCUTOFF=.
  • The output from records 4 and 5 shows results that are similar to records 1 and 2. The fourth record specifies a two-digit year of 00, and the fifth one specifies a four-digit year of 2000. Because of the value of the YEARCUTOFF= option, the years in the two resulting observations are the same.
As you can see, specifying a two-digit year might or might not result in the intended century prefix. The optimal value of the YEARCUTOFF= option depends on the range of the dates that you are processing.
In Releases 6.06 through 6.12 of SAS, the default value for the YEARCUTOFF= system option is 1900; starting with SAS 7, the default value is 1920.
For more information about how SAS handles dates, see the section on dates, times, and datetime values.

Practices That Help Ensure Date Integrity

The following practices help ensure that your date values are correct during all the conversions that occur during processing:
  • Store dates as SAS date values, not as simple numeric or character values.
  • Use the YEARCUTOFF= system option when converting two-digit dates to SAS date values.
  • Examine sets of raw data coming into your SAS process to make sure that any dates containing two-digit years are correctly interpreted by the YEARCUTOFF= system option. Look out for the following situations:
    • two-digit years that are distributed over more than a 100-year period. For dates covering more than a 100-year span, you must either use four-digit years in the data, or use conditional logic in a DATA step to interpret them correctly.
    • two-digit years that need an adjustment to the default YEARCUTOFF= range. For example, if the default value for YEARCUTOFF= in your operating environment is 1920 and you have a two-digit date in your data that represents 1919, you have to adjust your YEARCUTOFF= value downward by a year in the SAS program that processes this value.
  • Make sure that output SAS data sets represent dates as SAS date values.
  • Check your SAS programs to make sure that formats and informats that use two-digit years, such as DATE7., MMDDYY6., or MMDDYY8., are reading and writing data correctly.
Note: The YEARCUTOFF= option has no effect on dates that are already stored as SAS date values.

Working with SAS Dates and Times

Informats and Formats

SAS converts date, time, and datetime values back and forth between calendar dates and clock times with SAS language elements called formats and informats.
  • Formats present a value, recognized by SAS, such as a time or date value, as a calendar date or clock time in a variety of lengths and notations.
  • Informats read notations or a value, such as a clock time or a calendar date, which might be in a variety of lengths, and then convert the data to a SAS date, time, or datetime value.
SAS can read date and time values that are delimited by the following characters:
! # $ % & ( ) * + - . / : ; < = > ? [ \ ] ^ _ { | } ~
The blank character can also be used.
Only one delimiter can be used for a date. Otherwise, an error message is written to the SAS log. For example, 01/Jan/2007 uses a single delimiter, and can be read by SAS. In the case of 01-Jan/2007, two different delimiters separate the parts of the date, which results in an error message.

Date and Time Tools by Task

The following table correlates tasks with various SAS language elements that are available for working with time and date data.
Tasks with Dates and Times, Part 1
Task
Type of Language Element
Language Element
Input
Result
Write SAS date values in recognizable forms
Date formats
DATE.
18703
17MAR11
DATE9.
18703
17MAR2011
DAY.
18703
17
DDMMYY.
18703
17/03/11
DDMMYY10.
18703
17/03/2011
DDMMYYB.
18703
17 03 11
DDMMYYB10.
18703
17 03 2011
DDMMYYC.
18703
17:03:11
DDMMYYC10.
18703
17:03:2011
DDMMYYD.
18703
17-03-11
DDMMYYD10.
18703
17-03-2011
DDMMYYN.
18703
17032011
DDMMYYN6.
18703
170311
DDMMYYP.
18703
17.03.11
DDMMYYP10.
18703
17.03.2011
DDMMYYS.
18703
17/03/11
DDMMYYS10.
18703
17/03/2011
DOWNAME.
18703
Friday
JULDAY. 1
18703
77
JULIAN. 1
18703
00077
MMDDYY.
18703
03/17/00
MMDDYY10.
18703
03/17/2011
MMDDYYB.
18703
03 17 00
MMDDYYB10.
18703
03 17 2011
MMDDYYC.
18703
03:17:00
MMDDYYC10.
18703
03:17:2011
MMDDYYD.
18703
03-17-00
MMDDYYD10.
18703
03-17-2011
MMDDYYN.
18703
031700
MMDDYYN8.
18703
03172011
MMDDYYP.
18703
03.17.00
MMDDYYP10.
18703
03.17.2011
MMDDYYS.
18703
03/17/00
MMDDYYS10.
18703
03/17/2011
MMYY.
18703
03M2011
MMYYC.
18703
03:2011
MMYYD.
18703
03-2011
MMYYN.
18703
032011
MMYYP.
18703
03.2011
MMYYS.
18703
03/2011
MONNAME.
18703
March
MONTH.
18703
3
MONYY.
18703
MAR11
PDJULG. 1
18703
2011077F
PDJULI. 1
18703
0100077F
WEEKDATE.
18703
Thursday, March 17, 2011
WEEKDAY.
18703
5
WORDDATE.
18703
March 17, 2011
WORDDATX.
18703
17 March 2011
Quarter formats
QTR.
18703
1
QTRR.
18703
I
Time formats
TIME.
18703
5:11:43
TIMEAMPM.
18703
5:11:43 AM
TOD.
18703
05:11:43
Year formats
YEAR.
18703
2011
YYMM.
18703
2011M03
YYMMC.
18703
2011:03
YYMMD.
18703
2011-03
YYMMP.
18703
2011.03
YYMMS.
18703
2011/03
YYMMN.
18703
201103
YYMMDD.
18703
11-03-17
YYMON.
18703
2011MAR
Year/Quarter formats
YYQ.
18703
2011Q1
YYQC.
18703
2011:1
YYQD.
18703
2011-1
YYQP.
18703
2011.1
YYQS.
18703
2011/1
YYQN.
18703
20111
YYQR.
18703
2011QI
YYQRC.
18703
2011:I
YYQRD.
18703
2011-I
YYQRP.
18703
2011.I
YYQRS.
18703
2011/I
YYQRN.
18703
2011I
1In SAS, a Julian date is a date in the form YYNNN or YYYYNNN, where YY is a two-digit year, YYYY is a four-digit year, and NNN is the ordinal offset from January 1 of the year YY or YYYY. SAS processes Julian dates only for valid SAS dates.
Tasks with Dates and Times, Part 2
Task
Type of Language Element
Language Element
Input
Result
Date Tasks
Read calendar dates as SAS date
Note: YEARCUTOFF=1920
Date informats
DATE.
17MAR11
18703
DATE9.
17MAR2011
18703
DDMMYY.
170300
18703
DDMMYY8.
17032011
18703
JULIAN. 1
11076
18703
JULIAN7. 1
2011077
18703
MMDDYY.
031700
18703
MMDDYY8.
03172011
18703
MONYY.
MAR00
18687
YYMMDD.
000317
18703
YYMMDD8.
20110317
18703
YYQ.
11q1
18628
DATETIME
17MAR2011 00:00:00
1615939200
TIME
14:45:32
53132
Return today's date as a SAS date value
Date functions
DATE() or TODAY() (equivalent)
( )
The SAS date value for today.
Extract calendar dates from SAS
Date functions
DAY
18703
17
HOUR
18703
5
JULDATE 1
18703
11076
JULDATE7 1
18703
2011076
MINUTE
18703
11
MONTH
18703
3
QTR
18703
1
SECOND
18703
43
WEEKDAY
18703
5
YEAR
18703
2011
Write a date as a constant in an expression
SAS date constant
'ddmmmyy'd
or
'ddmmmyyyy'
'17mar00'd
'17mar2011'd
18703
Write today's date as a string
SYSDATE automatic macro variable
SYSDATE
&SYSDATE
The date at the time of SAS initialization in the form DDMMMYY.
SYSDATE9
SYSDATE9
&SYSDATE9
The date at time of SAS initialization, in the form DDMMMYYYY.
Time Tasks
Write SAS time values as time values
time formats
HHMM.
18703
14:46
HOUR.
18703
5
MMSS.
18703
311
TIME.
18703
5:11:43
TIMEAMPM.
18703
5:11:43 AM
TOD.
53132
05:11:43
Read time values as SAS time values
Time informats
TIME.
05:11:43
18703
Write the current time as a string
SYSTIME automatic macro variable
SYSTIME
&SYSTIME
The time at the moment of execution, in the form HH:MM
Return the current time of day as a SAS time value
Time functions
TIME( )
( )
The SAS time value at moment of execution, in the form NNNNN.NNN.
Return the time part of a SAS datetime value
Time functions
TIMEPART
17mar2011 05:11:43
5:11:43
Datetime Tasks
Write SAS datetime values as datetime values
Datetime formats
DATEAMPM
1615957903
26JUL11:05:11:43 AM
DATETIME
1615957903
17MAR11:05:11:43
Read datetime values as SAS datetime values
Datetime informats
DATETIME
17MAR11:05:11:43
1615957903
Return the current date and time of day as a SAS datetime value
Datetime functions
DATETIME()
()
The SAS datetime value at the moment of execution, in the form NNNNNNNNNN.N.
Interval Tasks
Return the number of specified time intervals that lie between the two date or datetime values
Interval functions
INTCK
week2
01aug60
01jan11
1331
Advances a date, time, or datetime value by a given interval, and returns a date, time, or datetime value
Interval functions
INTNX
day
18703
365
19068
1In SAS, a Julian date is a date in the form YYNNN or YYYYNNN, where YY is a two-digit year, YYYY is a four-digit year, and NNN is the ordinal offset from January 1 of the year YY or YYYY. SAS processes Julian dates only for valid SAS dates.
SAS also supports international formats and informats that are equivalent to some of the most commonly used English-language date formats and informats. For details, see the SAS formats and informats in SAS Formats and Informats: Reference.

Examples

Example 1: Displaying Date, Time, and Datetime Values as Recognizable Dates and Times

The following example demonstrates how a value might be displayed as a date, a time, or a datetime. Remember to select the SAS language element that converts a SAS date, time, or datetime value to the intended date, time, or datetime format. See the previous tables for examples.
Note:
  • Time formats count the number of seconds within a day, so the values are between 0 and 86400.
  • DATETIME formats count the number of seconds since January 1, 1960. For datetimes that are greater than 02JAN1960:00:00:01 (integer of 86401), the datetime value is always greater than the time value.
  • When in doubt, look at the contents of your data set for clues as to which type of value you are dealing with.
This program uses the DATETIME, DATE, and TIMEAMPM formats to display the value 86399 to a date and time, a calendar date, and a time.
options nodate pageno=1 linesize=80 pagesize=18;
data test;
   Time1=86399;
   format Time1 datetime.;
   Date1=86399;
   format Date1 date9.;
   Time2=86399;
   format Time2 timeampm.;
run;
proc print data=test;
   title  'Same Number, Different SAS Values';
   footnote1 'Time1 is a SAS DATETIME value';
   footnote2 'Date1 is a SAS DATE value';
   footnote3 'Time2 is a SAS TIME value';
run;
footnote;
Datetime, Date, and Time Values for 86399
                       Same Number, Different SAS Values                       1

              Obs         Time1              Date1       Time2

               1     01JAN60:23:59:59    20JUL2196    11:59:59 PM
 
 
 
 
 
 
 
 
 
 
                         Time1 is a SAS DATETIME value
                           Date1 is a SAS DATE value
                           Time2 is a SAS TIME value

Example 2: Reading, Writing, and Calculating Date Values

This program reads four regional meeting dates and calculates the dates on which announcements should be mailed.
data meeting;
   
   input region $ mtg : mmddyy8.;
   sendmail=mtg-45;
   datalines;
N  11-24-12
S  12-28-12
E  12-03-12
W  10-04-12
;

proc print data=meeting;
   format mtg sendmail date9.;
   title 'When To Send Announcements';
run;
Calculated Date Values: When to Send Mail
                           When To Send Announcements                          1

                    Obs    region          mtg     sendmail

                     1       N       24NOV2012    10OCT2012
                     2       S       28DEC2012    13NOV2012
                     3       E       03DEC2012    19OCT2012
                     4       W       04OCT2012    20AUG2012