BookmarkSubscribeRSS Feed
kparikh
Calcite | Level 5

Hi,

 

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.

 

Best,

Kaushal

3 REPLIES 3
Patrick
Opal | Level 21

@kparikh

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" ?>

 

LinusH
Tourmaline | Level 20
Perhaps it's possible to first append the data sets and then write to the XML file in a single step?
Data never sleeps
Patrick
Opal | Level 21

@kparikh

 

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;

 

demo.xml

<?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;

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 1313 views
  • 0 likes
  • 3 in conversation