Chapter 7
Dates, Times, and Intervals
About SAS Date, Time, and Datetime Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Two-Digit and Four-Digit Years . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Five-Digit Years . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
The Year 2000 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Working with SAS Dates and Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
About Date and Time Intervals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Intervals by Category . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Example: Calculating a Duration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Boundaries of Intervals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Single-Unit Intervals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Multi-Unit Intervals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Shifted Intervals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Custom Intervals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Retail Calendar Intervals: ISO 8601 Compliant . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
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. November
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. That was 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.
111
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.
Figure 7.1 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. Two-digit
years 00 through 49 correspond to 2000 through 2049. Note that while the default value
of the YEARCUTOFF= option in SAS is 1926, 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.
112 Chapter 7 Dates, Times, and Intervals
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. By default, the YEARCUTOFF= option is set to 1926.
options nodate;
data schedule;
input @1 jobid $ @6 projdate mmddyy10.;
datalines;
A100 01/15/25
A110 03/15/2025
A200 01/30/96
B100 02/05/12
B200 06/15/2012
;
proc print data=schedule;
format projdate mmddyy10.;
run;
The resulting output from the PROC PRINT statement looks like this:
Output 7.1 Output Showing Four-Digit Years That Result from Setting YEARCUTOFF= to
1926
Here are some facts to note in this example:
About SAS Date, Time, and Datetime Values 113
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. The century for the first
record defaults to the 2000s because 2025 is in the range of 1926–2025. The four-
digit year in the second record is unaffected by the YEARCUTOFF= option.
In the third record, the century defaults to the 1900s because the year 1996 is in the
range of 1926–2025.
The output from the fourth and fifth records show results that are similar to the first
and second records. The fourth record specifies a two-digit year of 12, and the fifth
one specifies a four-digit year of 2012. The century in the fourth record defaults to
the 2000s because 2012 is in the range of 1926–2025. The four-digit year in the fifth
record is unaffected by the YEARCUTOFF= option.
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 SAS 6.06 through SAS 6.12, the default value for the YEARCUTOFF=
system option is 1900. Starting with SAS 7, the default value is 1920; starting with SAS
9.4, the default value is 1926.
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 1926 and you have a two-digit date in your data that represents
1925, 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.
114 Chapter 7 Dates, Times, and Intervals
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.
About SAS Date, Time, and Datetime Values 115
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.81.201