| Exporting XML Documents |
This example exports an XML document from a SAS data set to use for transporting across hosts. In addition, the example illustrates controlling how to write SAS data set observations to the XML document using the XMLCOMPRESS= option.
The following output shows the SAS data set MYFILES.GRADES to be exported.
Obs Student Test1 Test2 Final
1 Fred 66 80 70
2 Wilma 97 91 98
The following SAS program exports an XML document using the EXPORT markup type and specifies XMLCOMPRESS=NO, which writes the observations in the default format.
libname myfiles 'C:\My Documents\myfiles'; 1 libname export xml92 'C:\My Documents\XML\Export.xml' xmltype=export xmlcompress=no; 2 data export.GRADES; 3 set myfiles.grades; run;
The first LIBNAME statement assigns the libref MYFILES to the physical location of the SAS library that stores the SAS data set GRADES. The V9 engine is the default.
The second LIBNAME statement assigns the libref EXPORT to the physical location of the file that will store the exported XML document and specifies the XML92 engine nickname. The engine option XMLTYPE=EXPORT produces tags that are in a machine-independent format. XMLCOMPRESS=NO, which is the default, writes observations to the XML document in expanded element form.
The DATA step reads the SAS data set MYFILES.GRADES and writes its content in machine-independent format to the specified XML document.
Here is the resulting XML document.
Exported XML Document in Expanded Element Form
<?xml version="1.0" encoding="windows-1252" ?>
<LIBRARY xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="SXLEexport.xsd"
type="EXPORT" version="9.2">
<LIBRARY-HEADER>
<Provider>SAS Institute Inc.</Provider>
<Version>9.2</Version>
<VersionLong>9.02.02B0D03112008</VersionLong>
<CreationDateTime>2008-03-12T09:58:48</CreationDateTime>
</LIBRARY-HEADER>
<TABLE name="GRADES">
<TABLE-HEADER>
<Provider>SAS Institute Inc.</Provider>
<Version>9.2</Version>
<VersionLong>9.02.02B0D03112008</VersionLong>
<CreationDateTime>2008-03-12T09:58:48</CreationDateTime>
<ModifiedDateTime>2008-03-12T09:58:48</ModifiedDateTime>
<Protection />
<DataSetType />
<DataRepresentation />
<Encoding>windows-1252</Encoding>
<ReleaseCreated />
<HostCreated />
<FileName>C:\My Documents\XML\Export.xml</FileName>
<Observations length="32" />
<Compression compressed="No" number="1" length="44" />
<SortAssertion sorted="false" />
<SystemOption missing="." />
<Variables number="4" />
</TABLE-HEADER>
<TABLE-METADATA>
<COLUMN order="1" name="Student">
<TYPE>character</TYPE>
<DATATYPE>string</DATATYPE>
<LENGTH>8</LENGTH>
<Offset>0</Offset>
</COLUMN>
<COLUMN order="2" name="Test1">
<TYPE>numeric</TYPE>
<DATATYPE>float</DATATYPE>
<Offset>8</Offset>
</COLUMN>
<COLUMN order="3" name="Test2">
<TYPE>numeric</TYPE>
<DATATYPE>float</DATATYPE>
<Offset>16</Offset>
</COLUMN>
<COLUMN order="4" name="Final">
<TYPE>numeric</TYPE>
<DATATYPE>float</DATATYPE>
<Offset>24</Offset>
</COLUMN>
</TABLE-METADATA>
<TABLE-DATA>
<DATA>
<DATUM name="Student">Fred</DATUM>
<DATUM-NUMERIC name="Test1" rawvalue="QkIAAAAAAAA=">66</DATUM-NUMERIC>
<DATUM-NUMERIC name="Test2" rawvalue="QlAAAAAAAAA=">80</DATUM-NUMERIC>
<DATUM-NUMERIC name="Final" rawvalue="QkYAAAAAAAA=">70</DATUM-NUMERIC>
</DATA>
<DATA>
<DATUM name="Student">Wilma</DATUM>
<DATUM-NUMERIC name="Test1" rawvalue="QmEAAAAAAAA=">97</DATUM-NUMERIC>
<DATUM-NUMERIC name="Test2" rawvalue="QlsAAAAAAAA=">91</DATUM-NUMERIC>
<DATUM-NUMERIC name="Final" rawvalue="QmIAAAAAAAA=">98</DATUM-NUMERIC>
</DATA>
</TABLE-DATA>
</TABLE>
</LIBRARY>
The following SAS program exports an XML document using the EXPORT markup type. This program specifies XMLCOMPRESS=YES, which writes observations to the XML document in base64 encoding. The base64 encoding method converts binary data into ASCII text, which results in an XML document in a compressed format.
libname myfiles 'C:\My Documents\myfiles'; libname export xml92 'C:\My Documents\XML\ExportCompress.xml' xmltype=export xmlcompress=yes; data export.GRADES; set myfiles.grades; run;
Here is the resulting XML document.
Exported XML Document in Compressed Format
<?xml version="1.0" encoding="windows-1252" ?>
<LIBRARY xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="SXLEexport.xsd"
type="EXPORT" version="9.2">
<LIBRARY-HEADER>
<Provider>SAS Institute Inc.</Provider>
<Version>9.2</Version>
<VersionLong>9.02.02B0D03112008</VersionLong>
<CreationDateTime>2008-03-12T10:14:34</CreationDateTime>
</LIBRARY-HEADER>
<TABLE name="GRADES">
<TABLE-HEADER>
<Provider>SAS Institute Inc.</Provider>
<Version>9.2</Version>
<VersionLong>9.02.02B0D03112008</VersionLong>
<CreationDateTime>2008-03-12T10:14:34</CreationDateTime>
<ModifiedDateTime>2008-03-12T10:14:34</ModifiedDateTime>
<Protection />
<DataSetType />
<DataRepresentation />
<Encoding>windows-1252</Encoding>
<ReleaseCreated />
<HostCreated />
<FileName>C:\My Documents\XML\ExportCompress.xml</FileName>
<Observations length="32" />
<Compression compressed="Yes" number="1" length="44" />
<SortAssertion sorted="false" />
<SystemOption missing="." />
<Variables number="4" />
</TABLE-HEADER>
<TABLE-METADATA>
<COLUMN order="1" name="Student">
<TYPE>character</TYPE>
<DATATYPE>string</DATATYPE>
<LENGTH>8</LENGTH>
<Offset>0</Offset>
</COLUMN>
<COLUMN order="2" name="Test1">
<TYPE>numeric</TYPE>
<DATATYPE>float</DATATYPE>
<Offset>8</Offset>
</COLUMN>
<COLUMN order="3" name="Test2">
<TYPE>numeric</TYPE>
<DATATYPE>float</DATATYPE>
<Offset>16</Offset>
</COLUMN>
<COLUMN order="4" name="Final">
<TYPE>numeric</TYPE>
<DATATYPE>float</DATATYPE>
<Offset>24</Offset>
</COLUMN>
</TABLE-METADATA> <TABLE-DATA>
<DATA>
<BUFFER number="1" length="44">
<![CDATA[RnJlZCAgICAAAAAAAIBQQAAAAAAAAFRAAAAAAACAUUA=]]>
</BUFFER>
</DATA>
<DATA>
<BUFFER number="1" length="44">
<![CDATA[V2lsbWEgICAAAAAAAEBYQAAAAAAAwFZAAAAAAACAWEA=]]>
</BUFFER>
</DATA>
</TABLE-DATA>
</TABLE>
</LIBRARY>
Copyright © 2008 by SAS Institute Inc., Cary, NC, USA. All rights reserved.