Chapter 19. Reading Date and Time Values

Overview

Introduction

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;
Introduction

Objectives

In this chapter, you learn how

  • SAS stores date and time values

  • to use SAS informats to read common date and time expressions

  • to handle two-digit date values

  • to calculate time intervals by subtracting two dates

  • to multiply a time interval by a rate

  • to display various date and time values.

How SAS Stores Date Values

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.

How SAS Stores Date Values

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.

14611

01-02-2000

MMDDYYw

14611

02/01/00

DDMMYYw.

14611

2000/01/02

YYMMDDw.

14611

Storing dates and times as numeric values enables you to use dates and times in calculations much as you would use any other number.

How SAS Stores Time Values

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.

How SAS Stores Time Values

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.

How SAS Stores Time Values

More About SAS Date and Time Values

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.

Reading Dates and Times with Informats

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.

Specifying Informats

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.

MMDDYYw. Informat

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

MMDDYY6.

10/15/99

MMDDYY8.

10 15 99

MMDDYY8.

10-15-1999

MMDDYY10.

DATEw. Informat

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

DATE7.

30May2000

DATE9.

30-May-2000

DATE11.

TIMEw. Informat

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

TIME11.

17:00

TIME5.

2:34

TIME5.

TIMEw. Informat
TIMEw. Informat

DATETIMEw. Informat

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

DATETIME20.

30May00 10:03:17.2

DATETIME18.

30May2000/10:03

DATETIME15.

YEARCUTOFF= SAS System Option

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

date7.

06Oct1959

17Mar1783

date9.

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

date7.

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.

YEARCUTOFF= SAS System Option

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.

Using Dates and Times in Calculations

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.

Using Dates and Times in Calculations

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.

Using Dates and Times in Calculations
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;
Using Dates and Times in Calculations

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;
Using Dates and Times in Calculations

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;
Using Dates and Times in Calculations

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;
Using Dates and Times in Calculations

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;
Using Dates and Times in Calculations

Using Date and Time Formats

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;
Using Date and Time Formats
Using Date and Time Formats

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.

The WEEKDATEw. Format

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;
The WEEKDATEw. Format

You can vary the results by changing the w value in the format.

FORMAT Statement

Result

format datein weekdate3.;

Mon

format datein weekdate6.;

Monday

format datein weekdate17.;

Monday, Apr 5, 99

format datein weekdate21.;

Monday, April 5, 1999

The WORDDATEw. Format

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;
The WORDDATEw. Format

You can vary the results by changing the w value in the format.

FORMAT Statement

Result

format datein worddate3.;

Apr

format datein worddate5.;

April

format datein worddate14.;

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;

Chapter Summary

Text Summary

How SAS Stores Date and Time Values

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.

Reading Dates and Times with Informats

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.

Using Dates and Times In Calculations

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.

Using Date and Time Formats

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.

Syntax

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;

Sample Program

  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;

Points to Remember

  • 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.

Chapter Quiz

Select the best answer for each question. After completing the quiz, you can check your answers using the answer key in the appendix.

  1. SAS date values are the number of days since which date?

    1. January 1, 1900

    2. January 1, 1950

    3. January 1, 1960

    4. January 1, 1970

  2. A great advantage of storing dates and times as SAS numeric date and time values is that

    1. they can easily be edited.

    2. they can easily be read and understood.

    3. they can be used in text strings like other character values.

    4. they can be used in calculations like other numeric values.

  3. SAS does not automatically make adjustments for daylight saving time, but it does make adjustments for:

    1. leap seconds

    2. leap years

    3. Julian dates

    4. time zones

  4. An input data file has date expressions in the form 10222001. Which SAS informat should you use to read these dates?

    1. DATE6.

    2. DATE8.

    3. MMDDYY6.

    4. MMDDYY8.

  5. The minimum width of the TIMEw. informat is:

    1. 4

    2. 5

    3. 6

    4. 7

  6. 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?

    1. 30May2000:10:03:17.2 DATETIME20.

    2. 30May00 10:03:17.2 DATETIME18.

    3. 30May2000/10:03 DATETIME15.

    4. 30May2000/1003 DATETIME14.

  7. What is the default value of the YEARCUTOFF= system option?

    1. 1920

    2. 1910

    3. 1900

    4. 1930

  8. Suppose your input data file contains the date expression 13APR2009. The YEARCUTOFF= system option is set to 1910. SAS will read the date as:

    1. 13APR1909

    2. 13APR1920

    3. 13APR2009

    4. 13APR2020

  9. 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?

    1. 08DEC1920

    2. 08DEC1925

    3. 08DEC2019

    4. 08DEC2025

  10. 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:

    1. TotDays=lastday-firstday;

    2. TotDays=lastday-firstday+1;

    3. TotDays=lastday/firstday;

    4. You cannot use date values in calculations.

..................Content has been hidden....................

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