Understanding How the XML LIBNAME Engine Works

Assigning a Libref

The XML LIBNAME engine works much like other SAS engines. That is, you execute a LIBNAME statement in order to assign a libref and specify an engine. You use that libref throughout the SAS session where a libref is valid.
A libref for the XML LIBNAME engine can be associated with either a specific XML document or, for the XMLV2 engine nickname, the physical location of a SAS library in a directory-based environment. When you use the libref, SAS either translates the data in a SAS data set into XML markup, or translates the XML markup into SAS format.

Importing an XML Document

To import an XML document as a SAS data set, the following LIBNAME statement assigns a libref to a specific XML document and specifies the XML engine:
libname myxml xml 'C:\My Files\XML\Students.xml';
Executing the DATASETS procedure shows that SAS interprets the XML document as a SAS data set:
proc datasets library=myxml;
DATASETS Procedure Output for MYXML Library
DATASETS Procedure Output for MYXML Library
The PRINT procedure results in the following output:
proc print data=myxml.students;
run;
PRINT Procedure Output for MYXML.STUDENTS
PRINT Procedure Output for MYXML.STUDENTS

Exporting an XML Document

To export an XML document from a SAS data set, the LIBNAME statement for the XML engine assigns a libref to the XML document to be created.
In the following code, the first LIBNAME statement assigns the libref MYFILES to the SAS library that contains the SAS data set Singers. The second LIBNAME statement assigns the libref MYXML to the physical location of the XML document that is to be exported from Myfiles.Singers:
libname myfiles 'C:\My Files\';

libname myxml xml 'C:\My Files\XML\Singers.xml';
Executing these statements creates the XML document named Singers.XML:
data myxml.Singers;
   set myfiles.Singers;
run;
XML Document Singers.XML
<?xml version="1.0" encoding="windows-1252" ?>    
<TABLE>                                           
   <SINGERS>                                      
      <FirstName> Tom </FirstName>                
      <Age> 62 </Age>                             
   </SINGERS>                                     
   <SINGERS>                                      
      <FirstName> Willie </FirstName>             
      <Age> 70 </Age>                             
   </SINGERS>                                     
   <SINGERS>                                      
      <FirstName> Randy </FirstName>              
      <Age> 43 </Age>                             
   </SINGERS>                                     
</TABLE>