Dates, Times, and Intervals |
Definitions |
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 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.
is a value representing the number of seconds since midnight of the current day. SAS time values are between 0 and 86400.
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
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 you are working with at the moment. To correctly handle 2-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 Language Reference: Dictionary.
The Year 2000 |
SAS software treats the year 2000 like any other leap year. If you use two-digit year numbers for dates, you'll 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.
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.
options yearcutoff=1920 nodate pageno=1 linesize=80 pagesize=60; 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:
The SAS System 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 on how SAS handles dates, see the section on dates, times, and datetime values.
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 will be correctly interpreted by the YEARCUTOFF= system option. Look out for
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 will 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 |
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.
The following table correlates tasks with various SAS language elements that are available for working with time and date data.
Task | Type of Language Element | Language Element | Input | Result |
---|---|---|---|---|
Write SAS date values in recognizable forms | Date formats | DATE. | 14686 | 17MAR00 |
DATE9. | 14686 | 17MAR2000 | ||
DAY. | 14686 | 17 | ||
DDMMYY. | 14686 | 17/03/00 | ||
DDMMYY10. | 14686 | 17/03/2000 | ||
DDMMYYB. | 14686 | 17 03 00 | ||
DDMMYYB10. | 14686 | 17 03 2000 | ||
DDMMYYC. | 14686 | 17:03:20 | ||
DDMMYYC10. | 14686 | 17:03:2000 | ||
DDMMYYD. | 14686 | 17-03-00 | ||
DDMMYYD10. | 14686 | 17-03-2000 | ||
DDMMYYN. | 14686 | 17032000 | ||
DDMMYYN6. | 14686 | 170300 | ||
DDMMYYP. | 14686 | 17.03.00 | ||
DDMMYYP10. | 14686 | 17.03.2000 | ||
DDMMYYS. | 14686 | 17/03/00 | ||
DDMMYYS10. | 14686 | 17/03/2000 | ||
DOWNAME. | 14686 | Friday | ||
JULDAY. | 14686 | 77 | ||
JULIAN. | 14686 | 00077 | ||
MMDDYY. | 14686 | 03/17/00 | ||
MMDDYY10. | 14686 | 03/17/2000 | ||
MMDDYYB. | 14686 | 03 17 00 | ||
MMDDYYB10. | 14686 | 03 17 2000 | ||
MMDDYYC. | 14686 | 03:17:00 | ||
MMDDYYC10. | 14686 | 03:17:2000 | ||
MMDDYYD. | 14686 | 03-17-00 | ||
MMDDYYD10. | 14686 | 03-17-2000 | ||
MMDDYYN. | 14686 | 031700 | ||
MMDDYYN8. | 14686 | 03172000 | ||
MMDDYYP. | 14686 | 03.17.00 | ||
MMDDYYP10. | 14686 | 03.17.2000 | ||
MMDDYYS. | 14686 | 03/17/00 | ||
MMDDYYS10. | 14686 | 03/17/2000 | ||
MMYY. | 14686 | 03M2000 | ||
MMYYC. | 14686 | 03:2000 | ||
MMYYD. | 14686 | 03-2000 | ||
MMYYN. | 14686 | 032000 | ||
MMYYP. | 14686 | 03.2000 | ||
MMYYS. | 14686 | 03/2000 | ||
MONNAME. | 14686 | March | ||
MONTH. | 14686 | 3 | ||
MONYY. | 14686 | MAR2000 | ||
PDJULG. | 14686 | 2000077F | ||
PDJULI. | 14686 | 0100077F | ||
WEEKDATE. | 14686 | Friday, March 17, 2000 | ||
WEEKDAY. | 14686 | 6 | ||
WORDDATE. | 14686 | March 17, 2000 | ||
WORDDATX. | 14686 | 17 MARCH 2000 | ||
Quarter formats | QTR. | 14686 | 1 | |
QTRR. | 14686 | I | ||
Time formats | TIME. | 14686 | 4:04:46 | |
TIMEAMPM. | 14686 | 4:04:46 AM | ||
TOD. | 14686 | 04:04:46 | ||
Year formats | YEAR. | 14686 | 2000 | |
YYMM. | 14686 | 2000M03 | ||
YYMMC. | 14686 | 2000:03 | ||
YYMMD. | 14686 | 2000-03 | ||
YYMMP. | 14686 | 2000.03 | ||
YYMMS. | 14686 | 2000/03 | ||
YYMMN. | 14686 | 200003 | ||
YYMMDD. | 14686 | 00-03-17 | ||
YYMON. | 14686 | 2000MAR | ||
Year/Quarter formats | YYQ. | 14686 | 2000Q1 | |
YYQC. | 14686 | 2000:1 | ||
YYQD. | 14686 | 2000-1 | ||
YYQP. | 14686 | 2000.1 | ||
YYQS. | 14686 | 2000/1 | ||
YYQN. | 14686 | 20001 | ||
YYQR. | 14686 | 2000QI | ||
YYQRC. | 14686 | 2000:I | ||
YYQRD. | 14686 | 2000-I | ||
YYQRP. | 14686 | 2000.I | ||
YYQRS. | 14686 | 2000/I | ||
YYQRN. | 14686 | III |
Task | Type of Language Element | Language Element | Input | Result |
---|---|---|---|---|
Date Tasks |
|
|
||
Read calendar dates as SAS date | Date informats | DATE. | 17MAR2000 | 14686 |
DATE9. | 17MAR2000 | 14686 | ||
DDMMYY. | 170300 | 14686 | ||
DDMMYY8. | 17032000 | 14686 | ||
JULIAN. | 0077 | 14686 | ||
JULIAN7. | 2000077 | 14686 | ||
MMDDYY. | 031700 | 14686 | ||
MMDDYY10. | 03172000 | 14686 | ||
MONYY. | MAR00 | 14670 | ||
YYMMDD. | 000317 | 14686 | ||
YYMMDD10. | 20000317 | 14686 | ||
YYQ. | 00Q1 | 14610 | ||
Create date values from pieces | Date functions | DATEJUL | 2000077 | 14686 |
|
|
DATETIME | '17MAR2000'D, | 1268870400 |
|
|
TIME | 14,45,32 | 53132 |
|
|
MDY | 03,17,00 | 14686 |
|
|
MDY | 03,17,2000 | 14686 |
|
|
YYQ | 00,1 | 14610 |
Extract a date from a datetime value | Date functions | DATEPART | '17MAR00:00:00 | 14686 |
Return today's date as a SAS date | Date functions | DATE() or TODAY() (equivalent) | ( ) | SAS date for today |
Extract calendar dates from SAS | Date functions | DAY | 14686 | 17 |
|
|
HOUR | 14686 | 4 |
|
|
JULDATE | 14686 | 77 |
|
|
JULDATE7 | 14686 | 2000077 |
|
|
MINUTE | 14686 | 4 |
|
|
MONTH | 14686 | 3 |
|
|
QTR | 14686 | 3 |
|
|
SECOND | 14686 | 46 |
|
|
WEEKDAY | 14686 | 6 |
|
|
YEAR | 14686 | 2000 |
Write a date as a constant in an expression | SAS date constant | 'ddmmmyy'd |
'17mar00'd
'17mar2000'd |
|
Write today's date as a string | SYSDATE automatic macro variable | SYSDATE | &SYSDATE | Date at time of SAS initialization in DDMMMYY |
SYSDATE9 | SYSDATE9 | &SYSDATE9 | Date at time of SAS initialization in DDMMMYYYY | |
Time Tasks |
|
|
||
Write SAS time values as time values | time formats | HHMM. | 53132 | 14:46 |
|
|
HOUR. | 53132 | 15 |
|
|
MMSS. | 53132 | 885 |
|
|
TIME. | 53132 | 14:45:32 |
|
|
TIMEAMPM. | 53132 | 2:45:32 PM |
|
|
TOD. | 53132 | 14:45:32 |
Read time values as SAS time values | Time informats | TIME. | 14:45:32 | 53132 |
Write the current time as a string | SYSTIME automatic macro variable | SYSTIME | &SYSTIME | Time at moment of execution in HH:MM |
Return the current time of day as a SAS time value | Time functions | TIME( ) | ( ) | SAS time value at moment of execution in NNNNN.NN |
Return the time part of a SAS datetime value | Time functions | TIMEPART | SAS datetime value in NNNNNNNNNN.N | SAS time value part of date value in NNNNN.NN |
Datetime Tasks |
|
|
||
Write SAS datetime values as datetime values | Datetime formats | DATEAMPM | 1217083532 | 26JUL98:02:45 PM |
DATETIME | 1268870400 | 17MAR00:00:00 | ||
Read datetime values as SAS datetime values | Datetime informats | DATETIME | 17MAR00:00:00:00 | 1268870400 |
Return the current date and time of day as a SAS datetime value | Datetime functions | DATETIME() | () | SAS datetime value at moment of execution in NNNNNNNNNN.N |
Interval Tasks |
|
|
||
Return the number of specified time intervals that lie between the two date or datetime values | Interval functions | INTCK | week 2 | 1055 |
Advances a date, time, or datetime value by a given interval, and returns a date, time, or datetime value | Interval functions | INTNX | day | 14086 |
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 Language Reference: Dictionary.
Examples |
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.
Time formats count the number of seconds within a day, so the values will be between 0 and 86400.
DATETIME formats count the number of seconds since January 1, 1960, so for datetimes that are greater than 02JAN1960:00:00:01 (integer of 86401), the datetime value will always be 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.
data test; Time1=86399; format Time1 datetime.; Date1=86399; format Date1 date.; 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 20JUL96 11:59:59 PM Time1 is a SAS DATETIME value Date1 is a SAS DATE value Time2 is a SAS TIME value.
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-99 S 12-28-99 E 12-03-99 W 10-04-99 ; 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 Obs region mtg sendmail 1 N 24NOV1999 10OCT1999 2 S 28DEC1999 13NOV1999 3 E 03DEC1999 19OCT1999 4 W 04OCT1999 20AUG1999
Copyright © 2010 by SAS Institute Inc., Cary, NC, USA. All rights reserved.