Importing XML Documents Using an XMLMap |
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">E8601TM</INFORMAT> 1 <FORMAT width="8">E8601TM</FORMAT> </COLUMN> <COLUMN name="LOCALZONE"> <PATH syntax="XPath">/Root/TIME/LOCAL</PATH> <TYPE>numeric</TYPE> <DATATYPE>time</DATATYPE> <INFORMAT width="8">E8601TM</INFORMAT> 2 <FORMAT width="14">E8601LZ</FORMAT> </COLUMN> <COLUMN name="UTC"> <PATH syntax="XPath">/Root/TIME/UTC</PATH> <TYPE>numeric</TYPE> <DATATYPE>time</DATATYPE> <INFORMAT width="9">E8601TZ</INFORMAT> 3 <FORMAT width="9">E8601TZ</FORMAT> </COLUMN> <COLUMN name="OFFSET"> <PATH syntax="XPath">/Root/TIME/OFFSET</PATH> <TYPE>numeric</TYPE> <DATATYPE>time</DATATYPE> <INFORMAT width="14">E8601TZ</INFORMAT> 4 <FORMAT width="14">E8601TZ</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 E8601TM 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 E8601TM 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 E8601LZ SAS format, which writes time values in the extended format hh:mm:ss[.fffff][Z][+|-]hh:mm]. The E8601LZ format appends the UTC offset to the value as determined by the local, current SAS session. Using the E8601LZ 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.
For the UTC variable, the INFORMAT and FORMAT elements specify the E8601TZ 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 E8601TZ 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-05:00 09:00:00Z 09:00:00+00:00
Copyright © 2010 by SAS Institute Inc., Cary, NC, USA. All rights reserved.