BookmarkSubscribeRSS Feed
MariaD
Barite | Level 11

Hi folks, 

 

I have an XML file with multiple values for the same element. For example, the element <Info> in some cases has 2 or 3 different values:

 

<Op id="AA" MOD="214" ZIP="AAAA">

<Info="122">

<info="213" Cd="56" Ident="oooo">

</Op>

 

When I import the XML on SAS, the results only have the last <Info> element, in my example, 213. Is there any way to have the 2 values on SAS table? 

 

Thanks,

 

 

1 REPLY 1
mtnbikerjoshua
Obsidian | Level 7

Hi @MariaD ,

 

The example XML you have posted is not valid XML, so that makes it hard to tell exactly what your issue is. However, I suspect you need an XML map. Here is a nice straightforward article on how to have SAS automatically create an XML map for you using the automap= option on the libname statement: Tips for reading XML files into SAS® software - SAS Users

 

There are two issues with your XML: 1) the middle two elements do not have an element name, and 2) the middle two elements do not have closing tags. Below is some example code with two possible corrections of your XML. In either case, there are two elements with the same name, and both get imported.

 

/* Create temporary files to use for the two XML files and the XML map */
filename xmlfile1 temp;
filename xmlfile2 temp;
filename mapfile temp;

/* Create the two XML files */
data _null_;
  file xmlfile1;
  put '<Op id="AA" MOD="214" ZIP="AAAA">' /
    '<elementname info="122" />' /
    '<elementname info="213" Cd="56" Ident="oooo" />' /
    '</Op>';
run;

data _null_;
  file xmlfile2;
  put '<Op id="AA" MOD="214" ZIP="AAAA">' /
    '<info>122</info>' /
    '<info Cd="56" Ident="oooo">213</info>' /
    '</Op>';
run;

/* Import the two XML files into libraries called xmlfile1 and xmlfile2 respectively */
libname xmlfile1 xmlv2 xmlmap=mapfile automap=replace;
libname xmlfile2 xmlv2 xmlmap=mapfile automap=replace;

 

 Hope this helps!

 

Regards,

Joshua

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 496 views
  • 0 likes
  • 2 in conversation