Tips for reading XML files into SAS® software

8

ProblemSolversXML has become one of the major standards for moving data across the Internet. Some of XML’s strengths are the abilities to better describe data and to be more extensible than any of its predecessors such as CSV. Due to the increased popularity of XML for moving data, I provide a few tips in this article that will help when you need to read XML files into SAS software.

Reading XML Files

You can read XML files into SAS using either the XML engine or the XMLV2 engine. The XML engine was the first engine that SAS created to read XML files. The XMLV2 engine includes new functionality and enhancements and is aliased to the XML92 engine in SAS® 9.2.

It is easy to read XML files using the XMLV2 engine when the XML file that you read uses the GENERIC markup type and conforms to a very rectangular definition. Here is an example of the XML file layout that is required to be read natively using the XMLV2 engine:

If the file is not in this format, the following informative error message is generated in the SAS log:

reading XML files into SAS® software

If the file is not in this format, the following informative error message is generated in the SAS log:

Reading XML files into SAS Software02

The XMLMap file referenced in the error message is a SAS specific file that instructs the engine about how to create rows, columns, and other information from the XML markup. You can use either of the following methods to generate the XMLMap file:

  • the AUTOMAP= option within the SAS session beginning with SAS® 9.3 TS1M2
  • the SAS XML Mapper, which is a stand-alone Java application

Generating Dynamic Maps within the SAS Session

Using the XMLV2 engine, you can create a dynamic XMLMap file and use it to generate SAS data sets by specifying the AUTOMAP= and the XMLMAP= options in the LIBNAME statement. The AUTOMAP= option has two possible values: REPLACE and REUSE. REPLACE updates an existing XMLMap file, whereas REUSE uses an existing XMLMap file. Both values create a new XMLMap file if one does not currently exist. When the file is automapped, one or more data sets is created. This method creates a representation that is as relational as possible based on the XML markup. In addition, it generates surrogate keys that enable you to combine the data using the SQL procedure or the DATA step. The following example demonstrates this method.

Here is the XML data that is read:

Reading XML files into SAS Software03

Here is the SAS session code:

filename datafile 'c:\test\students2.xml';
filename mapfile "c:\test\students2.map";
libname datafile xmlv2 xmlmap=mapfile automap=replace;

proc copy in=datafile out=work;
run;

Here is the output:

Reading XML files into SAS Software04

For more information about using the XMLV2 engine, refer to the “SAS® 9.4 XMLV2 LIBNAME Engine Tip Sheet.”

Using SAS XML Mapper

If you answer “yes” to the following questions, you should use SAS XML Mapper to generate the XMLMap file rather than generating this file dynamically within the SAS session:

  • Do you need to generate an XMLMap from an XML schema file?
  • Do you need to generate a custom XMLMap file or map only a portion of the file?
  • Do you need to view the data to be imported before reading the XML file?
  • Do you need to add specific metadata such as data types, informats/formats, column lengths, or names?
  • Do you want to generate sample code for the import of the XML file?

The following image shows SAS XML Mapper once you click the icon that runs the automapping feature, which enables you to quickly generate tables from the XML markup:

Reading XML files into SAS Software05

Using SAS XML Mapper, you can also generate a custom XMLMap file. My colleague Chris Hemedinger has an example of this in his post, Read RSS feeds with SAS using XML or JSON.

For more information, refer to the following video tutorials about creating XMLMap files:

I hope this article helps you easily read XML files into SAS regardless of the file structure. This information should also help you determine the appropriate method to use when generating an XMLMap file.

Share

About Author

Chevell Parker

SAS Technical Support Analyst

Chevell is a Senior Principal Technical Support Analyst in the Foundation SAS group within SAS Technical Support. His main support areas include the Output Delivery System and XML technologies.

8 Comments

    • Sander Doorneveld on

      What kind of typo did you encounter? I'm having the same problem. Generated an map-file based on a XML with auto-generate. In XML Mapper I can see the tables, content, records, everything, but when I try to import into SAS-table I see the metadata but no records.

        • Chevell Parker
          Chevell Parker on

          Jose, I believe the issue that you are having maybe a little different as tables are not showing up in the XML Mapper which I expect to be an issue with the installation.

  1. Sharon Schiro on

    I wrote my code to import XML files while using SAS 9.3, but I'm now using 9.4. I'm getting no error messages, but all my resulting files contain 0 records - though I know that there are records in the XML file. I can open the file in SAS XML Mapper 9.41 and apply the previously generated map - and the records show up. But, when I try to read the code through SAS9.4, I see no records. This code worked fine when I used SAS 9.3. The statements I'm using are:
    filename SASEXPOR "c:\test\foo.xml";
    filename SXLEMAP 'c:\test\mymap.map';
    libname SASEXPOR xmlv2 xmlmap=SXLEMAP access=READLONG;
    proc datasets lib=SASEXPOR; run; quit;

    Thanks in advance for your help, Sharon

  2. Arnold Hopman on

    Hi Chevell,

    The post is good, thanks for that.
    But SAS is less helpfull: everytime I try to read a validates XML-file into SAS with the above standing code, I receive non-informational feedback instead of the red messages above:
    ERROR: 3
    ERROR: Error in the LIBNAME statement.
    Only the '3' is variable (sometimes it's just '?' or any other character.
    What I would like to know: what could be wrong in the XML, that causes this problem?

    Thanks in advance,
    Arnold

    • Chevell Parker
      Chevell Parker on

      Hello Arnold,

      Thanks for the information regarding the less than informative error when reading XML. This is something that we are researching in hope of improving the error in the future.

      Sincerely,
      Chevell

  3. Thanks for the post. It is very clear and helpful!

    Can you tell me if there is a JDK requirement to run SAS XML MAPPER? If so, what version of the JDK is required to run SAS XML MAPPER 9.3?

    Thanks,
    Frank

Leave A Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Top