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,
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
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.