05-03-2017 04:37 PM
We are working on generating XML files leveraging multiple SAS Datasets in DIS (XML mapper and XMLV2) and are not able to append the XML when looping through the SAS datasets. Instead when we move to the next SAS dataset it overwrites the XML file generated with the first SAS Dataset.
Our requirement is to loop through the first dataset and generate XML files and move to the next daataset and append the XML files and so on.
Please let me know.
05-03-2017 06:52 PM
I believe you can't directly append to an existing XML output unless you write the full XML using SAS Datastep and Put statements.
What you could do, is to write to individual files and then post process these to create a single file (ie. using a SAS data step or OS commands like cat and tail in Unix).
Except for the first file make sure to only read from line 2 onwards or you're going to repeat below line.
<?xml version="1.0" encoding="windows-1252" ?>
05-06-2017 06:54 AM - edited 05-06-2017 07:46 AM
I couldn't find where that's documented but it appears you can get what you want by also defining a FILENAME wit MOD pointing to the same location than your XML Libname.
libname test xmlv2 "c:\temp\demo.xml" ; filename test "%sysfunc(pathname(test))" mod; data test.class; set sashelp.class(obs=1); run; data test.air; set sashelp.air(obs=2); run; libname test; filename test;
<?xml version="1.0" encoding="windows-1252" ?> <TABLE> <CLASS> <Name>Alfred</Name> <Sex>M</Sex> <Age>14</Age> <Height>69</Height> <Weight>112.5</Weight> </CLASS> <AIR> <DATE>1949-01-01</DATE> <AIR>112</AIR> </AIR> <AIR> <DATE>1949-02-01</DATE> <AIR>118</AIR> </AIR> </TABLE>
You can also use Proc Datasets for this task so you don't need to loop over tables and rows in tables but simply pass in a list of tables to be copied. And it appears with Proc Datasets you append "out-of-the-box" and don't need the Filename statement.
I've done some testing and Appending even happens with multiple Proc Datasets and even with multiple sessions.
libname test xmlv2 "c:\temp\demo.xml" ; /*filename test "%sysfunc(pathname(test))" mod;*/ proc datasets lib=sashelp nolist; copy in=sashelp out=test; select class airline; run; quit; proc datasets lib=sashelp nolist; copy in=sashelp out=test; select air; run; quit; libname test; /*filename test;*/
I believe what that means: Understand your demo.xml when assigned via a Libname XML statement like any other library. You wouldn't expect there that you wipe out all the tables only because you replace one of the tables. It appears the Libname statement with a SAS datastep but without the Filename Mod statement behaves here actually a bit inconsistent.
And now given all of the above the way I'd implement: First have a data step so you wipe out and re-create your xml whenever you re-run the code, then use Proc Datasets for all tables to be appended.
libname test xmlv2 "c:\temp\demo.xml" ; data test.class; set sashelp.class; run; proc datasets lib=sashelp nolist; copy in=sashelp out=test; select air airline; run; quit; libname test;