| ISO 8601 SAS Formats and Informats |
| Importing Both Basic Format and Extended Format Dates |
This simple example illustrates importing an XML document that contains date values in both the basic format and the extended format. The XMLMap uses the FORMAT and INFORMAT elements to specify the appropriate SAS format and SAS informat in order to represent the dates according to ISO 8601 standards.
First, here is the XML document:
<?xml version="1.0" ?>
<Root>
<ISODATE>
<BASIC>20010911</BASIC>
<EXTENDED>2001-09-11</EXTENDED>
</ISODATE>
</Root>
The following XMLMap imports the XML document using the SAS informats and formats to read and write the date values:
<?xml version="1.0" encoding="UTF-8"?>
<SXLEMAP version="1.2" name="ISOdate"
description="Reading a Basic and Extended format ISO date field">
<!-- ############################################################ -->
<TABLE name="ISODATE">
<TABLE-PATH syntax="XPath">/Root/ISODATE</TABLE-PATH>
<COLUMN name="BASIC">
<PATH syntax="XPath">/Root/ISODATE/BASIC</PATH>
<TYPE>numeric</TYPE>
<DATATYPE>date</DATATYPE>
<FORMAT width="10">IS8601DA</FORMAT>
<INFORMAT width="8">ND8601DA</INFORMAT>
</COLUMN>
<COLUMN name="EXTENDED">
<PATH syntax="XPath">/Root/ISODATE/EXTENDED</PATH>
<TYPE>numeric</TYPE>
<DATATYPE>date</DATATYPE>
<FORMAT>IS8601DA</FORMAT>
<INFORMAT>IS8601DA</INFORMAT>
</COLUMN>
</TABLE>
</SXLEMAP>
The following explains the XMLMap syntax that imports the date values:
For the Basic variable, the FORMAT element specifies the IS8601DA SAS format, which writes data values in the extended format YYYY-MM-DD.
For the Basic variable, the INFORMAT element specifies the ND8601DA SAS informat, which reads date values into a variable in the basic format YYYYMMDD.
Note: As recommended, when you read values
into a variable with a basic format SAS informat, this example writes the
values with the corresponding extended format SAS format. ![[cautionend]](../common.hlp/images/cautend.gif)
For the Extended variable, the FORMAT element specifies the IS8601DA SAS format, which writes data values in the extended format YYYY-MM-DD.
For the Extended variable, the INFORMAT element specifies the IS8601DA SAS informat, which reads date values into a variable in the basic format YYYY-MM-DD.
The following SAS statements import the XML document and display PRINT procedure output:
filename dates 'c:\My Documents\XML\ISOdate.xml'; filename map 'c:\My Documents\XML\ISOdate.map'; libname dates xml xmlmap=map; proc print data=dates.isodate; run;
PRINT Procedure Output for Imported Data Set DATES.ISODATE
The SAS System 1
Obs BASIC EXTENDED
1 2001-09-11 2001-09-11
| Importing Time Values with a Time Zone |
This example illustrates importing an XML document that contains time values in various forms. The XMLMap uses the FORMAT and INFORMAT elements to specify the appropriate SAS formats and SAS informats in order to represent the times appropriately.
First, here is an XML document that contains a variety of time values:
<?xml version="1.0" ?>
<Root>
<TIME>
<LOCAL>09:00:00</LOCAL>
<UTC>09:00:00Z</UTC>
<OFFSET>14:00:00+05:00</OFFSET>
</TIME>
</Root>
The following XMLMap imports the XML document using the SAS informats and formats to read and write the time values:
<?xml version="1.0" encoding="UTF-8"?>
<SXLEMAP version="1.2" name="ISOtime">
description="Reading time values with and without offsets">
<!-- ############################################################ -->
<TABLE name="TIME">
<TABLE-PATH syntax="XPath">/Root/TIME</TABLE-PATH>
<COLUMN name="LOCAL">
<PATH syntax="XPath">/Root/TIME/LOCAL</PATH>
<TYPE>numeric</TYPE>
<DATATYPE>time</DATATYPE>
<INFORMAT width="8">IS8601TM</INFORMAT>
<FORMAT width="8">IS8601TM</FORMAT>
</COLUMN>
<COLUMN name="LOCALZONE">
<PATH syntax="XPath">/Root/TIME/LOCAL</PATH>
<TYPE>numeric</TYPE>
<DATATYPE>time</DATATYPE>
<INFORMAT width="8">IS8601TM</INFORMAT>
<FORMAT width="14">IS8601LZ</FORMAT>
</COLUMN>
<COLUMN name="UTC">
<PATH syntax="XPath">/Root/TIME/UTC</PATH>
<TYPE>numeric</TYPE>
<DATATYPE>time</DATATYPE>
<INFORMAT width="9">IS8601TZ</INFORMAT>
<FORMAT width="9">IS8601TZ</FORMAT>
</COLUMN>
<COLUMN name="OFFSET">
<PATH syntax="XPath">/Root/TIME/OFFSET</PATH>
<TYPE>numeric</TYPE>
<DATATYPE>time</DATATYPE>
<INFORMAT width="14">IS8601TZ</INFORMAT>
<FORMAT width="14">IS8601TZ</FORMAT>
</COLUMN>
</TABLE>
</SXLEMAP>
The following explains the XMLMap syntax that imports the time values:
For the Local variable, the INFORMAT and FORMAT elements specify the IS8601TM SAS informat and format, which reads and writes time values in the extended format hh:mm:ss[.fffff]. Because there is no time zone indicator, the context of the value is local time.
For the Localzone variable, which reads the same value as the Local variable, the INFORMAT element specifies the IS8601TM SAS informat, which reads time values in the extended format hh:mm:ss[.fffff. Because there is no time zone indicator, the context of the value is local time.
The FORMAT element, however, specifies the IS8601LZ SAS format, which writes time values in the extended format hh:mm:ss[.fffff][Z][+|-]hh:mm]. The IS8601LZ format appends the UTC offset to the value as determined by the local, current SAS session. Using the IS8601LZ format enables you to provide a time notation in order to eliminate the ambiguity of local time.
Note: Even with the time notation, it is recommended
that you do not mix time-based values. ![[cautionend]](../common.hlp/images/cautend.gif)
For the UTC variable, the INFORMAT and FORMAT elements specify the IS8601TZ SAS informat and format, which reads and writes time values in the extended format hh:mm:ss[.fffff][Z][+|-]hh:mm]. Because there is a time zone indicator, the value is assumed to be expressed in UTC. No adjustment or conversion is made to the value.
For the Offset variable, the INFORMAT and FORMAT elements specify the IS8601TZ SAS informat and format, which reads and writes time values in the extended format hh:mm:ss[.fffff][Z][+|-]hh:mm]. Because there is a time zone offset present, when the time value is read into the variable using the time zone sensitive SAS informat, the value is adjusted to UTC as requested via the time zone indicator, but the time zone context is not stored with the value. When the time value is written using the time zone sensitive SAS format, the value is expressed as UTC with a zero offset value and is not adjusted to or from local time.
The following SAS statements import the XML document and display the PRINT procedure output:
filename timzn 'c:\My Documents\XML\Time.xml'; filename map 'c:\My Documents\XML\Time.map'; libname timzn xml xmlmap=map; proc print data=timzn.time; run;
PRINT Procedure Output for Imported Data Set TIMZN.TIME
The SAS System 1
Obs LOCAL LOCALZONE UTC OFFSET
1 09:00:00 09:00:00-04:00 09:00:00Z 09:00:00+00:00
Copyright © 2007 by SAS Institute Inc., Cary, NC, USA. All rights reserved.