SAS provides many informats for reading raw data values in various forms. If you completed Chapter 17, "Reading Raw Data in Fixed Fields," on page 525, you worked with informats to read standard and nonstandard data. In this chapter, you learn how to use a special category of SAS informats called date and time informats. These informats enable you to read a variety of common date and time expressions. After you read date and time values, you can also perform calculations with them.
options yearcutoff=1920;
data perm.aprbills;
infile aprdata;
input LastName $8. @10 DateIn mmddyy8. +1 DateOut
mmddyy8. +1 RoomRate 6. @35 EquipCost 6.;
Days=dateout-datein+1;
RoomCharge=days*roomrate;
Total=roomcharge+equipcost;
run;
Before you read date or time values into a SAS data set or use those values in calculations, you should understand how SAS stores date and time values.
When you use a SAS informat to read a date, SAS converts it to a numeric date value. A SAS date value is the number of days from January 1, 1960, to the given date.
Here are some examples of how the appropriate SAS informat can convert different expressions for the date January 2, 2000, to a single SAS date value:
Date Expression | SAS Date Informat | SAS Date Value |
---|---|---|
02Jan00 | DATEw. |
|
01-02-2000 | MMDDYYw |
|
02/01/00 | DDMMYYw. |
|
2000/01/02 | YYMMDDw. |
|
Storing dates and times as numeric values enables you to use dates and times in calculations much as you would use any other number.
SAS stores time values similar to the way it stores date values. A SAS time value is stored as the number of seconds since midnight.
A SAS datetime is a special value that combines both date and time information. A SAS datetime value is stored as the number of seconds between midnight on January 1, 1960, and a given date and time.
As you use SAS date and time values, remember that
SAS date values are based on the Gregorian calendar, and they are valid for dates from A.D. 1582 through A.D. 20,000. Use caution when working with historical dates. Most of Europe started to use the Gregorian calendar in 1582. Great Britain and the American colonies adopted it in 1752. Check the adoption date for other parts of the world before making important calculations.
SAS makes adjustments for leap years but ignores leap seconds.
SAS does not make adjustments for daylight saving time.
You use SAS date and time informats to read date and time expressions and convert them to SAS date and time values. Like other SAS informats, date and time informats are composed of
an informat name
a field width
a period delimiter.
SAS informat names indicate the form of date expression that can be read using that particular informat. Here are some examples of common date and time informats:
DATEw.
DATETIMEw.
MMDDYYw.
TIMEw.
As you know, there are several ways to write a particular date. For example, all the following expressions represent the date October 15, 1999. Each of these common date expressions can be read using the appropriate SAS date informat.
Date Expression | SAS Date Informat |
---|---|
10/15/99 | MMDDYYw. |
15Oct99 | DATEw. |
10-15-99 | MMDDYYw. |
99/10/15 | YYMMDDw. |
Using the INPUT statement with an informat after a variable name is the simplest way to read date and time values into a variable.
For example, the following INPUT statement uses two informats:
input @15 Style $3. @21 Price 5.2;
The $w. character informat ($3.) reads values, starting at column 15 of the raw data, into the variable Style. The w.d numeric informat (5.2) reads values, starting at column 21, into the variable Price.
Now let's look at some specific informats you can use.
You can tell by its name that the informat MMDDYYw. reads date values in the form 10/15/99.
In the MMDDYYw. informat, the month, day, and year fields can be separated by blanks or delimiters such as - or /. If you use delimiters, you must place them between all fields in the values. Remember to specify a field width that includes not only the month, day, and year values, but any delimiters as well. Here are some date expressions that you can read using the MMDDYYw. informat:
Date Expression | SAS Date Informat |
---|---|
101599 |
|
10/15/99 |
|
10 15 99 |
|
10-15-1999 |
|
The DATEw. informat reads date values in the form 30May2000.
You can place blanks or other special characters between the day, month, and year, as long as you increase the width of the informat to include these delimiters. Here are some date expressions that you can read using the DATEw. informat:
Date Expression | SAS Date Informat |
---|---|
30May00 |
|
30May2000 |
|
30-May-2000 |
|
The TIMEw. informat reads values in the form hh:mm:ss.ss.
If you do not enter a value forss.ss, a value of zero is assumed. Here are some examples of time expressions that you can read using the TIMEw. informat:
TimeExpression | SAS Time Informat |
---|---|
17:00:01.34 |
|
17:00 |
|
2:34 |
|
The DATETIMEw. informat reads expressions that are composed of two parts, a date value and a time value, in the form: ddmmmyy hh:mm:ss.ss.
If you do not enter a value for ss.ss, a value of zero
is assumed.
Here are some examples of the DATETIMEw. informat. Note that in the time value, you must use delimiters to separate the values for hour, minutes, and seconds.
Date and Time Expression | SAS Datetime Informat |
---|---|
30May2000:10:03:17.2 |
|
30May00 10:03:17.2 |
|
30May2000/10:03 |
|
Recall from Chapter 2, "Referencing Files and Setting Options," on page 37 that the value of the YEARCUTOFF= system option affects only two-digit year values. A date value that contains a four-digit year value will be interpreted correctly even if it does not fall within the 100-year span set by the YEARCUTOFF= system option.
Date Expression | SAS Date Informat | Interpreted As |
---|---|---|
06Oct59 |
| 06Oct1959 |
17Mar1783 |
| 17Mar1783 |
However, if you specify an inappropriate field width, you will receive incorrect results. Notice that the date expression in the table below contains a four-digit year value. The informat specifies a w value that is too small to read the entire value, so the last two digits of the year are truncated.
Date Expression | SAS Date Informat | Interpreted As |
---|---|---|
17Mar1783 |
| 17Mar2017 |
Another problem arises if you use the wrong informat to read a date or time expression. The SAS log displays an invalid data message, and the variable's values are set to missing.
When you work with date and time values,
check the default value of the YEARCUTOFF= system option, and change it if necessary. The default YEARCUTOFF= value is 1920
.
specify the proper informat for reading a date value.
specify the correct field width so that the entire date value is read.
In this chapter so far, you've learned how date and time informats read common date and time expressions in specific forms. Now you will see how converting date and time expressions to numeric SAS date values can be useful, particularly for determining time intervals or performing calculations.
Suppose you work in the billing department of a small community hospital. It's your job to create a SAS data set from the raw data file that is referenced by the fileref Aprdata. A portion of the raw data file below shows data values that represent each patient's
last name
date checked in
date checked out
daily room rate
equipment cost.
The data set that you create must also include variable values that represent how many days each person stayed in the hospital, the total room charges, and the total of all expenses that each patient incurred. When building the SAS program, you must first name the data set, identify the raw data file Aprdata, and use formatted input to read the data.
options yearcutoff=1920; data perm.aprbills; infile aprdata; input LastName $8.
Notice that the values in the second and third fields are in the form mmddyy.To complete the INPUT statement, add instructions to read the values for RoomRate (third field) and EquipCost (fourth field), and add a semicolon.
options yearcutoff=1920; data perm.aprbills; infile aprdata; input LastName $8. @10 DateIn mmddyy8. +1 DateOut mmddyy8. +1 RoomRate 6. @35 EquipCost 6.;
Now that the INPUT statement is complete, calculate how many days each patient was hospitalized. Because DateIn and DateOut are numeric variables, you can simply subtract to find the difference. But because the dates should be inclusive (patients are charged for both the first and last days), you must add 1 to the difference. Call this new variable Days.
options yearcutoff=1920;
data perm.aprbills;
infile aprdata;
input LastName $8. @10 DateIn mmddyy8. +1 DateOut
mmddyy8. +1 RoomRate 6. @35 EquipCost 6.;
Days=dateout-datein+1;
You can calculate a total room charge by multiplying the variable values for Days and RoomRate.
options yearcutoff=1920;
data perm.aprbills;
infile aprdata;
input LastName $8. @10 DateIn mmddyy8. +1 DateOut
mmddyy8. +1 RoomRate 6. @35 EquipCost 6.;
Days=dateout-datein+1;
RoomCharge=days*roomrate;
Calculating the total cost for each patient is easy. Create a variable named Total whose value is the sum of RoomCharge and EquipCost. Then add a PROC PRINT step and a RUN statement to view the new data.
options yearcutoff=1920;
data perm.aprbills;
infile aprdata;
input LastName $8. @10 DateIn mmddyy8. +1 DateOut
mmddyy8. +1 RoomRate 6. @35 EquipCost 6.;
Days=dateout-datein+1;
RoomCharge=days*roomrate;
Total=roomcharge+equipcost;
run;
proc print data=perm.aprbills;
run;
If the values for DateIn and DateOut look odd to you, remember that these are SAS date values. Applying a format such as MMDDYY, displays them as they appeared in Aprdata. You'll work with some other date and time formats later in this chapter.
Follow the execution of the program that you've written. When the DATA step executes, the values for DateIn and DateOut are converted to SAS date values.
options yearcutoff=1920; data perm.aprbills; infile aprdata; input LastName $8. @10 DateIn mmddyy8. +1 DateOut mmddyy8. +1 RoomRate 6. @35 EquipCost 6.; Days=dateout-datein+1; RoomCharge=days*roomrate; Total=roomcharge+equipcost; run;
After the rest of the INPUT statement executes, the value for Days is created by subtracting the SAS date value for DateIn from the value for DateOut and then adding 1.
options yearcutoff=1920;
data perm.aprbills;
infile aprdata;
input LastName $8. @10 DateIn mmddyy8. +1 DateOut
mmddyy8. +1 RoomRate 6. @35 EquipCost 6.;
Days=dateout-datein+1;
RoomCharge=days*roomrate;
Total=roomcharge+equipcost;
run;
The value for RoomCharge is calculated next. RoomCharge is the product of Days and RoomRate.
options yearcutoff=1920;
data perm.aprbills;
infile aprdata;
input LastName $8. @10 DateIn mmddyy8. +1 DateOut
mmddyy8. +1 RoomRate 6. @35 EquipCost 6.;
Days=dateout-datein+1;
RoomCharge=days*roomrate;
Total=roomcharge+equipcost;
run;
The value for Total is the final calculation. Total is the sum of EquipCost and RoomCharge.
options yearcutoff=1920; data perm.aprbills; infile aprdata; input LastName $8. @10 DateIn mmddyy8. +1 DateOut
mmddyy8. +1 RoomRate 6. @35 EquipCost 6.;
Days=dateout-datein+1;
RoomCharge=days*roomrate;
Total=roomcharge+equipcost;
run;
Remember that when Perm.Aprbills is printed, the values for DateIn and DateOut appear as SAS date values.
options yearcutoff=1920;
data perm.aprbills;
infile aprdata;
input LastName $8. @10 DateIn mmddyy8. +1 DateOut
mmddyy8. +1 RoomRate 6. @35 EquipCost 6.;
Days=dateout-datein+1;
RoomCharge=days*roomrate;
Total=roomcharge+equipcost;
run;
proc print data=perm.aprbills;
run;
SAS provides many specialized date and time formats that enable you to specify how date and time values are displayed and stored. Let's take a closer look at two date formats: WEEKDATEw. and WORDDATEw.
You can use the WEEKDATEw. format to write these values out in a format that displays the day of the week, month, day, and year.
proc print data=perm.aprbills;
format datein dateout weekdate17.;
run;
You can vary the results by changing the w value in the format.
FORMAT Statement | Result |
---|---|
| Mon |
| Monday |
| Monday, Apr 5, 99 |
| Monday, April 5, 1999 |
The WORDDATEw. format is similar to the WEEKDATEw. format, but it does not display the day of the week or the two-digit year values.
proc print data=perm.aprbills;
format datein dateout worddate12.;
run;
You can vary the results by changing the w value in the format.
FORMAT Statement | Result |
---|---|
| Apr |
| April |
| April 15, 1999 |
Remember that you can permanently assign a format to variable values by including a FORMAT statement in the DATA step.
options yearcutoff=1920;
data work.aprbills;
infile aprdata;
input LastName $8. @10 DateIn mmddyy8. +1 DateOut
mmddyy8. +1 RoomRate 6. @35 EquipCost 6.;
Days=dateout-datein+1;
RoomCharge=days*roomrate;
Total=roomcharge+equipcost;
format datein dateout worddate12.;
run;
proc print data=work.aprbills;
run;
SAS stores date values as numeric SAS date values, which are the number of days from January 1, 1960. SAS time values are the number of seconds since midnight.
Use SAS informats to read date and time expressions and convert them to SAS date and time values.
MMDDYYw. reads dates such as 053090, 05/30/90, or 05 30 1990.
DATEw. reads dates such as 30May1990, 30May90, or 30-May-1990.
TIMEw. reads times such as 17:00, 17:00:01.34, or 2:34.
DATETIMEw. reads dates and times such as 30May1990:10:03:17.2, 30May90 10:03:17.2, or 30May1990/10:03.
Two-digit year values require special consideration. When a two-digit year value is read, SAS defaults to a year within a 100-year span that is determined by the YEARCUTOFF= system option. The default value of YEARCUTOFF= is 1920
. You can check or reset the value of this option in your SAS session to use a different 100-year span for date informats.
Date and time values can be used in calculations like other numeric values. In addition to tracking time intervals, SAS date and time values can be used with SAS functions and with complex calculations.
SAS provides many specialized date and time formats that enable you to specify how date and time values are displayed and stored. You can use the WEEKDATEw. format to write date values in the form day-of-week, month-name dd, yy (or yyyy). You can use the WORDDATEw. format to write date values in the form month-name dd, yyyy.
OPTIONS YEARCUTOFF=yyyy;
DATA=SAS-data-set;
INFILE file-specification;
INPUT <pointer-control> variable informat.;
RUN;
PROC PRINT DATA= SAS-data-set;
FORMAT variable format.;
RUN;
options yearcutoff=1920; data perm.aprbills; infile aprdata; input LastName $8. @10 DateIn mmddyy8. +1 DateOut mmddyy8. +1 RoomRate 6. @35 EquipCost 6.; Days=dateout-datein+1; RoomCharge=days*roomrate; Total=roomcharge+equipcost; run; proc print data=perm.aprbills; format datein dateout worddate12.; run;
SAS makes adjustments for leap years, but not for leap seconds or daylight saving time.
The minimum acceptable field width for the TIMEw. informat is 5. If you specify aw value less than 5
, you'll receive an error message in the SAS log.
The default value of the YEARCUTOFF= option is1920
. When you work with two-digit year data, remember to check the default value of the YEARCUTOFF= option, and change it if necessary.
The value of the YEARCUTOFF= system option does not affect four-digit year values. Four-digit values are always read correctly.
Be sure to specify the proper informat for reading a date value, and specify the correct field width so that the entire value is read.
If SAS date values appear in your program output, use a date format to display them in legible form.
Select the best answer for each question. After completing the quiz, you can check your answers using the answer key in the appendix.
SAS date values are the number of days since which date?
January 1, 1900
January 1, 1950
January 1, 1960
January 1, 1970
A great advantage of storing dates and times as SAS numeric date and time values is that
they can easily be edited.
they can easily be read and understood.
they can be used in text strings like other character values.
they can be used in calculations like other numeric values.
SAS does not automatically make adjustments for daylight saving time, but it does make adjustments for:
leap seconds
leap years
Julian dates
time zones
An input data file has date expressions in the form 10222001. Which SAS informat should you use to read these dates?
DATE6.
DATE8.
MMDDYY6.
MMDDYY8.
The minimum width of the TIMEw. informat is:
4
5
6
7
Shown below are date and time expressions and corresponding SAS datetime informats. Which date and time expression cannot be read by the informat that is shown beside it?
30May2000:10:03:17.2 DATETIME20.
30May00 10:03:17.2 DATETIME18.
30May2000/10:03 DATETIME15.
30May2000/1003 DATETIME14.
What is the default value of the YEARCUTOFF= system option?
1920
1910
1900
1930
Suppose your input data file contains the date expression 13APR2009. The YEARCUTOFF= system option is set to 1910. SAS will read the date as:
13APR1909
13APR1920
13APR2009
13APR2020
Suppose the YEARCUTOFF= system option is set to 1920. An input file contains the date expression 12/08/1925, which is being read with the MMDDYY8. informat. Which date will appear in your data?
08DEC1920
08DEC1925
08DEC2019
08DEC2025
Suppose your program creates two variables from an input file. Both variables are stored as SAS date values: FirstDay records the start of a billing cycle, and LastDay records the end of that cycle. The code for calculating the total number of days in the cycle would be:
TotDays=lastday-firstday;
TotDays=lastday-firstday+1;
TotDays=lastday/firstday;
You cannot use date values in calculations.