DATA Step, Macro, Functions and more

SAS code for XML file

Reply
Super Contributor
Posts: 625

SAS code for XML file

Is there any way to create a SAS code to produce a dataset from the following XML?

 

<?xml version="1.0" encoding="iso-8859-1" ?>
<NHL>
  <CONFERENCE> Eastern 
    <DIVISION> Southeast  
      <TEAM name="Thrashers"  abbrev="ATL" />  
   </DIVISION>
 </CONFERENCE> 
</NHL>
Super User
Posts: 23,306

Re: SAS code for XML file


Babloo wrote:

Is there any way to create a SAS code to produce a dataset from the following XML?

 

<?xml version="1.0" encoding="iso-8859-1" ?>
<NHL>
  <CONFERENCE> Eastern 
    <DIVISION> Southeast  
      <TEAM name="Thrashers"  abbrev="ATL" />  
   </DIVISION>
 </CONFERENCE> 
</NHL>

Yes, XML Mapper or XML libname. Or you can always parse it manually...sometimes that ends up being the fastest solution. 

Super Contributor
Posts: 625

Re: SAS code for XML file

Is there any way that you can help me create the SAS code for the above
example?
Super User
Posts: 23,306

Re: SAS code for XML file

Same as every other question, show what you have, what you expect as output and what you've tried and you'll get help. 

 

 

SAS Super FREQ
Posts: 9,324

Re: SAS code for XML file

Hi:
What code have you tried? For example, this Tech Support note seems to have an example of reading the NHL xml file using an XML Map: http://support.sas.com/documentation/cdl/en/engxml/62845/HTML/default/viewer.htm#a002484891.htm
cynthia
Super Contributor
Posts: 331

Re: SAS code for XML file

Hello,

 

Try the following SAS code :

filename xmldata "c:\temp\test XML SAS\test.xml";
filename xmlmap  "c:\temp\test XML SAS\mapping.map";
libname  xmldata xml xmlmap=xmlmap;


data test;
set xmldata.mytable;
run;

With test.xml the XML file you described and mapping.map as follows

<SXLEMAP version="1.2" name="SXLEMAP">
    <TABLE name="mytable">
        <TABLE-PATH syntax="XPATH">
           /NHL/CONFERENCE/DIVISION/TEAM
         </TABLE-PATH>
        <COLUMN name="Conference">
            <PATH syntax="XPath">/NHL/CONFERENCE</PATH>
            <TYPE>character</TYPE>
            <DATATYPE>string</DATATYPE>
            <LENGTH>10</LENGTH>
        </COLUMN>
        <COLUMN name="Division">
            <PATH syntax="XPath">/NHL/CONFERENCE/DIVISION</PATH>
            <TYPE>character</TYPE>
            <DATATYPE>string</DATATYPE>
            <LENGTH>10</LENGTH>
        </COLUMN>
        <COLUMN name="Team">
            <PATH syntax="XPath">/NHL/CONFERENCE/DIVISION/TEAM@name</PATH>
            <TYPE>character</TYPE>
            <DATATYPE>string</DATATYPE>
            <LENGTH>10</LENGTH>
        </COLUMN>
        <COLUMN name="Abbreviation">
            <PATH syntax="XPath">/NHL/CONFERENCE/DIVISION/TEAM@abbrev</PATH>
            <TYPE>character</TYPE>
            <DATATYPE>string</DATATYPE>
            <LENGTH>10</LENGTH>
        </COLUMN>
    </TABLE>
</SXLEMAP>
Super Contributor
Posts: 625

Re: SAS code for XML file

Received the following error. Could you please guide me to resolve the issue?

 

24         data test;
25         set xmldata.mytable;
ERROR: Physical file does not exist, /wrk1/mapping.map.
       encountered during XMLMap parsing
       occurred at or near line 1, column 1
ERROR: XML describe error: Internal processing error.
26         run;
Super Contributor
Posts: 331

Re: SAS code for XML file

Hello,

 

It is written in the log :

 

ERROR: Physical file does not exist, /wrk1/mapping.map

Check that the path to your map file is correct.

Super Contributor
Posts: 625

Re: SAS code for XML file

Path is correct.But I don't have that .Map file in that folder.
Super Contributor
Posts: 331

Re: SAS code for XML file

You have to create it there with the contents i provided in my first answer.

Super Contributor
Posts: 625

Re: SAS code for XML file

Is there any way to format a XML file which has repetitive elements?

Super Contributor
Posts: 625

Re: SAS code for XML file

Can we also create the mapping.map via SAS XML mapper?

Super Contributor
Posts: 331

Re: SAS code for XML file

Yes, you can use XML mapper to create the map file as suggested by @Reeza.

SAS Employee
Posts: 17

Re: SAS code for XML file

Here is the most recent version of the XML engine book, opened at the topics that discuss the XML Mapper: Using SAS XML Mapper to Generate and Update an XMLMap.

 

See also two videos about XMLMaps listed under "Videos" at http://support.sas.com/documentation/onlinedoc/base/index.html

Ask a Question
Discussion stats
  • 13 replies
  • 220 views
  • 3 likes
  • 5 in conversation