Reading Dates and Times by Using the ISO 860 Basic and Extended Notations

ISO 8601 Formatting Symbols

The following list explains the formatting symbols that are used to notate the ISO 8601 dates, time, datetime, durations, and interval values:
n
specifies a number that represents the number of years, months, or days
P
indicates that the duration that follows is specified by the number of years, months, days, hours, minutes, and seconds
T
indicates that a time value follows. Any value with a time must begin with T.
Requirement:Time values that are read by the extended notation informats that begin with the characters E8601 must use an uppercase T.
W
indicates that the duration is specified in weeks.
Z
indicates that the time value is the time in Greenwich, England, or UTC time.
+|-
the + indicates the time zone offset to the east of Greenwich, England. The - indicates the time zone offset to the west of Greenwich, England.
yyyy
specifies a four-digit year
mm
as part of a date, specifies a two-digit month, 01–12
dd
specifies a two-digit day, 01–1
hh
specifies a two-digit hour, 00–24
mm
as part of a time, specifies a two-digit minute, 00–59
ss
specifies a two-digit second, 00–59
fff | ffffff
specifies an optional fraction of a second using the digits 0–9:
fff use 1 - 3 digits for values read by the $N8601B informat and the $N8601E informat
ffffff use 1 - 6 digits for informat other than the $N8601B informat and the $N8601E informat
Y
indicates that a year value proceeds this character in a duration
M
as part of a date, indicates that a month value proceeds this character in a duration
D
indicates that a day value proceeds this character in a duration
H
indicates that an hour value proceeds this character in a duration
M
as part of a time, indicates that a minute value proceeds this character in a duration
S
indicates that a seconds value proceeds this character in a duration

Reading ISO 8601 Date, Time, and Datetime Values

SAS reads ISO 8601 dates, times, and datetimes using various informats, and the resulting values are SAS date, time, or datetime values. The following table shows different date, time, and datetime forms and the informats that you use to read them:
Date, Time, or Datetime
ISO 8601 Notation
Example
Informat
Basic Notations
Date
YYYYMMDD
20120915
B8601DAw.
Time
hhmmssnnnnnn
155300322348
B8601TMw.d
Time with time zone
hhmmss+|-hhmm
155300+0500
B8601TZw.d
hhmmssZ
155300Z
B8601TZw.d
Convert to local time with time zone
hhmmss+|-hhmm
155300+0500
B8601TZw.d
Datetime
YYYYMMDDThhmmssnnnnnn
20120915T155300
B8601DTw.d
Datetime with timezone
YYYYMMDDThhmmss+|-hhmm
20120915T155300+0500
B8601DZw.d
YYYYMMDDThhmmssZ
20120915T155300Z
B8601DZw.d
Read the date from a datetime
YYYYMMD
20120915
B8601DNw.
Extended Notations
Date
YYYY-MM-DD
2012-09-15
E8601DAw.
Time
hh:mm:ss.nnnnnn
15:53:00.322348
E8601TMw.d
Time with time zone
hh:mm:ss.nnnnnn+|-hh:mm
15:53:00+05:00
E8601TZw.d
Convert to local time with time zone
hh:mm:ss.nnnnnn+|-hh:mm
15:53:00+05:00
E8601LZw.d
Datetime
YYYY-MM-DDThh:mm:ss.nnnnnn
2012-09-15T15:53:00
E8601DTw.d
Datetime with time zone
YYYY-MM-DDThh:mm:ss.nnnnnn+|-hh:mm
2012-09-15T15:53:00+05:00
E8601DZw.d
Read date from a datetime
YYYY-MM-DD
2012-09-15
E8601DNw.
When SAS reads an ISO 8601 value that specifies a time zone offset (+|-hh:mm or +|-hhmm), the time or datetime value is adjusted to account for the offset. A SAS time or datetime value for an ISO 8601 value with a time zone offset is the time or datetime for the zero meridian (Greenwich, England). For example, if SAS reads the datetime 2011–09–15T15:53:00+05:00 using the E8601DZ informat, the datetime value 1631703180 has been adjusted for the five hour time zone difference. This datetime value is the datetime value for the zero meridian. If you write this value using the E8601DZ format, the value is 2011–09–15T10:53:00+00:00. The hour specified after the T shows the five hour adjustment.

Reading ISO 8601 Duration, Interval, and Datetime Values

Informats That Read Duration, Interval, and Datetime Values

SAS uses two informats that reads ISO datetime, duration, and interval values.
$N8601B informat
reads duration, interval, and datetime values that are specified in either the basic notation or the extended notation
$N8601E informat
reads duration, interval, and datetime values that are specified only in the extended notation
Use the $N8601E informat when you want to make sure that you are in compliance with the extended notation.
The datetime values that are read by these informats result in a SAS character representation. If you want a datetime value to be read as a numeric value, use the B8601DT informat, the B8601DZ informat, the E8601DT informat, or the E8601DZ informat.

Complete Duration, Interval, and Datetime Notations

The following table shows the formatting of duration, datetime, and interval values that can be read in the complete form:
Complete Component Forms
Time Component
ISO 8601 Notation
Example
Duration - Basic Notation
PYYYYMMDDThhmmss
P20120915T155300
-PYYYYMMDDThhmmss
-P20080915T155300
Duration - Extended Notation
PYYYY-MM-DDThh:mm:ss
P2012-09-15T15:53:00
-PYYYY-MM-DDThh:mm:ss
-P2012-09-15T15:53:00
Duration - Basic and Extended Notation
PnYnMnDTnHnMnS
P2y10m14dT20h13m45s
-PnYnMnDTnHnMnS
-P2n10m14dT20h13m45s
PnW (weeks)
P6w
Interval - Basic Notation
YYYYMMDDThhmmss/YYYYMMDDThhmmss
20120915T155300/20141113T000000
PnYnMnDTnHnMnS/YYYYMMDDThhmmss
P2y10M14dT20h13m45s/20120915T155300
YYYYMMDDThhmmss/PnYnMnDTnHnMnS
20120915T155300/P2y10M14dT20h13m45s
Interval- Extended Notation
YYYY-MM-DDThh:mm:ss/YYYY-MM-DDThh:mm:ss
2012-09-15T15:53:00/2014-11-13T00:00:00
PnYnMnDTnHnMnS/YYYY-MM-DDThh:mm:ss
P2y10M14dT20h13m45s/2012-09-15T15:53:00
YYYY-MM-DDThh:mm:ss/PnYnMnDTnHnMnS
2012-09-15T15:53:00/P2y10M14dT20h13m45s
Datetime-Basic Notation
YYYYMMDDThhmmss.fff+|-hhmm
20120915T155300
(all blank)
Datetime-Extended Notation
YYYY-MM-DDThh:mm:ss.fff+|-hhmm
2012-09-15T15:53:00
+04:30
(all blank)

Reading Omitted Components

One or more date or time components can be omitted from a datetime value or a duration value that is in the form Pyyyymmdd. SAS reads omitted components using the $N8601B informat or the $N8601E informat, and the omitted component must be represented by a hyphen ( - ).
The following examples show duration, datetime, and interval values with omitted components:
p0003-02--T10:31:33
The omitted component is the number of days.
-p0003-02-02T-:31:33
The omitted component is the number of hours.
x-09-15T15:x:x
The omitted components are the number of years, minutes, and seconds.
2012-09-15T15:x:00/2010-09-15T15:x:00
The omitted components are the minutes.
When reading values that contain a time zone offset, omitted components are not allowed. Use 00 in place of omitted components.

Truncated Values

SAS reads truncated duration, datetime, and interval values, where one or more lower order components is truncated because the value is 0 or the value is not significant.
The following list shows examples of truncated values:
  • p00030202T1031
  • 2012-09-15T15/2014-09-15T15:53
  • -p0003-03-03T-:-:-
  • P2y3m4dT5h6m
  • 2012-09-xTx:x:x
  • 2012
When reading values that contain a time zone offset, truncation is not allowed. Use 00 in place of truncated values.

Normalizing Duration Components

When a value for a duration component is greater than the largest standard value for a component, SAS normalizes the component except when the duration component is a single component. The following table shows examples of normalized duration components:
Duration
Extended Normalized Duration
p3y13m
p0004-01
pt24h24m65s
P----01T-:25:05
p3y13mT24h61m
P0004-01-01T01:01
p0004-13
p0005-01
p0003-02-61T15:61:61
P0003-04-01T16:02:01
p13m
P13M
If a component contains the largest value, such as 60 for minutes or seconds, SAS normalizes the value and replaces the value with a hyphen. For example, pT12:60:13 becomes PT13:-:13.
Thirty days is used to normalize a month.
Dates and times in a datetime value that are greater than the standard value for the component are not normalized. They produce an error.

Fractions in Durations, Datetime, and Interval Values

Ending components can contain a fraction that consists of a period or a comma, followed by one to three digits. The following examples show the use of fractions in duration, datetime, and interval values:
  • 201209.5
  • P2012-09-15T10.33
  • 2012-09-15/P0003-03-03,333