SAS Institute. The Power to Know

FOCUS AREAS

Return to previous page

Base SAS

Example: Importing Time Values with a Time Zone

This example illustrates how to import 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:

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