Reading Dates and Times with Informats

Overview

SAS date and time informats read date and time expressions and convert them to SAS date and time values. Like other SAS informats, date and time informats have several parts:
  • 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. This chapter covers commonly used date and time informats such as these:
  • DATEw.
  • DATETIMEw.
  • MMDDYYw.
  • TIMEw.
There are several ways to represent a date. For example, all the following expressions represent the date October 15, 2009. Each of these common date expressions can be read using the appropriate SAS date informat.
Table 19.1 Date Expressions and Corresponding SAS Date Informats
Date Expression
SAS Date Informat
10/15/09
MMDDYYw.
15Oct09
DATEw.
10-15-09
MMDDYYw.
09/10/15
YYMMDDw.

INPUT Statement Syntax

Using the INPUT statement with an informat after a variable name is the simplest way to read date and time values into a variable.
Syntax, INPUT statement with an informat:
INPUT <pointer-control> variable informat.;
  • pointer-control specifies the absolute or relative position to move the pointer.
  • variable is the name of the variable being read.
  • informat. is any valid SAS informat. Note that the informat includes a final period.
For example, the following INPUT statement uses two informats:
input @15 Style $3. @21 Price 5;
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) reads values, starting at column 21, into the variable Price.

The MMDDYYw. Informat

The informat MMDDYYw. reads date values in the form 10/15/99.
Syntax, values read with MMDDYYw. informat:
mmddyy or mmddyyyy
  • mm is an integer between 01 and 12, representing the month.
  • dd is an integer between 01 and 31, representing the day.
  • yy or yyyy is an integer that represents the year.
In the MMDDYYw. informat, the month, day, and year fields can be separated by blanks or delimiters such as - or /. If delimiters are present, they must occur 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:
Table 19.2 Date Expressions and Corresponding SAS Date Informats
Date Expression
SAS Date Informat
101509
MMDDYY6.
10/15/09
MMDDYY8.
10 15 09
MMDDYY8.
10-15-2009
MMDDYY10.

The DATEw. Informat

The DATEw. informat reads date values in the form 30May2000.
Syntax, values read with DATEw. informat:
ddmmmyy or ddmmmyyyy
  • dd is an integer from 01 to 31, representing the day.
  • mmm is the first three letters of the month's name.
  • yy or yyyy is an integer that represents the year.
Blanks or other special characters can appear 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:
Table 19.3 Date Expressions and Corresponding SAS Date Informats
Date Expression
SAS Date Informat
30May10
DATE7.
30May2010
DATE9.
30-May-2010
DATE11.

The TIMEw. Informat

The TIMEw. informat reads values in the form hh:mm:ss.ss.
Syntax, values read with TIMEw. informat:
hh:mm:ss.ss
  • hh is an integer from 00 to 23, representing the hour.
  • mm is an integer from 00 to 59, representing the minute.
  • ss.ss is an optional field that represents seconds and hundredths of seconds.
If you do not enter a value for ss.ss, a value of zero is assumed. Here are some examples of time expressions that you can read using the TIMEw. informat:
Table 19.4 Time Expressions and Corresponding SAS Time Informats
Time Expression
SAS Time Informat
17:00:01.34
TIME11.
17:00
TIME5.
2:34
TIME5.
Note: Five is the minimum acceptable field width for the TIMEw. informat. If you specify a w value less than 5, you will receive an error message in the SAS log.

The DATETIMEw. Informat

The DATETIMEw. informat reads expressions that consist of two parts, a date value and a time value, in the form: ddmmmyy hh:mm:ss.ss.
Syntax, values read with DATETIMEw. informat:
ddmmmyy hh:mm:ss.ss
  • ddmmmyy is the date value, the same form as for the DATEw. informat
  • The time value must be in the form hh:mm:ss.ss.
  • hh is an integer from 00 to 23, representing the hour.
  • mm is an integer from 00 to 59, representing the minute.
  • ss.ss is an optional field that represents seconds and hundredths of seconds.
  • The date value and time value are separated by a blank or other delimiter.
If you do not enter a value for ss.ss, a value of zero is assumed.
Note: In the time value, you must use delimiters to separate the values for hour, minutes, and seconds.
Table 19.5 Date and Time Expressions and Corresponding SAS Datetime Informats
Date and Time Expression
SAS Datetime Informat
30May2010:10:03:17.2
DATETIME20.
30May10 10:03:17.2
DATETIME18.
30May2010/10:03
DATETIME15.

The YEARCUTOFF= System Option

A date value that contains a four-digit year value is interpreted correctly even if it does not fall within the 100-year span that is set by the YEARCUTOFF= system option.
Table 19.6 Date Expressions
Date Expression
SAS Date Informat
Result
06Oct09
date7.
06Oct2009
17Mar2013
date9.
17Mar2013
If you specify an inappropriate field width, you receive incorrect results. 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.
Table 19.7 Date Expressions (Wrong Field Width)
Date Expression
SAS Date Informat
Result
17Mar1783
date7.
17Mar2017
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, remember to do the following:
  • Check the default value of the YEARCUTOFF= system option, and change it if necessary. The default YEARCUTOFF= value is 1926.
  • Specify the proper informat for reading a date value.
  • Specify the correct field width so that the entire date value is read.
Last updated: January 10, 2018
..................Content has been hidden....................

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