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, 2017. Each of these common date expressions can be read using the appropriate SAS date informat.
Table 13.1 Date Expressions and Corresponding SAS Date Informats
Date Expression
SAS Date Informat
10/15/17
MMDDYYw.
15Oct17
DATEw.
15–10–17
DDMMYYw.
17/10/15
YYMMDDw.

The MMDDYYw. Informat

The informat MMDDYYw. reads date values in the form mmddyy or mmddyyyy.
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 13.2 Date Expressions and Corresponding SAS Date Informats
Date Expression
SAS Date Informat
101517
MMDDYY6.
10/15/17
MMDDYY8.
10 15 17
MMDDYY8.
10-15-2017
MMDDYY10.
The DDMMYYw. informat and the YYMMDDw. informat are similar in that you can read the day, month, and year number with or without delimiters. The difference is the order of day, month, and year.

Example: Reading Dates with Formats and Informats

The following example illustrates reading a CSV file with dates in MMDDYY10. informat.
proc import datafile='C:UsersStudent1cert
ew_hires.csv'
  out=newhires
  dbms=csv
  replace;
  getnames=yes;
run;
proc print data=work.newhires;
run;
proc contents data=work.newhires;
run;
Output 13.2 Partial Output of Work.NewHires
Partial Output of Work.NewHires
Output 13.3 Partial Output of PROC CONTENTS Work.NewHires
Partial Output of PROC CONTENTS Work.NewHires

The DATEw. Informat

The DATEw. informat reads date values in the form ddmmmyy or ddmmmyyyy.
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 13.3 Date Expressions and Corresponding SAS Date Informats
Date Expression
SAS Date Informat
30May17
DATE7.
30May2017
DATE9.
30-May-2017
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 13.4 Time Expressions and Corresponding SAS Time Informats
Time Expression
SAS Time Informat
17:00:01.34
TIME11.
17:00
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 13.5 Date and Time Expressions and Corresponding SAS Datetime Informats
Date and Time Expression
SAS Datetime Informat
30May2017:10:03:17.2
DATETIME20.
30May17 10:03:17.2
DATETIME18.
30May2017/10:03
DATETIME15.
Last updated: August 23, 2018
..................Content has been hidden....................

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