DATA Step, Macro, Functions and more

Importing multiple xml files into 1 sas dataset.

Reply
Contributor
Posts: 23

Importing multiple xml files into 1 sas dataset.

Hello,

I have hundreds of xml files to import, all of them with the same layout. I have made the xml map with SAS XML mapper and used code from user called dapstat to import multiple files but it does not read the data eventhough I get a list of the files in the directory.

Could someone please help me in case dapstat is not available? I have already contacted him through his original post. I attach the log.

Or maybe there is a different way?

Thank you in advance.

filename  SXLELIB '\\tsclient\C\Transferencia de Archivos\IMPORT TO SAS\sgfxml\02303_20171016070200.xml';
filename  SXLEMAP '\\tsclient\C\Transferencia de Archivos\IMPORT TO SAS\SGFXML\siammapV2.map';
libname   SXLELIB xmlv2 xmlmap=SXLEMAP access=READONLY;
 

DATA alerta; SET SXLELIB.alerta; run;



libname siamia 'C:\Users\SAS\Desktop\siamia';
filename DIRLIST pipe 'dir "\\tsclient\C\Transferencia de Archivos\IMPORT TO SAS\sgfxml\0*.xml" /b';

data dirlist ;
infile dirlist lrecl=200 truncover;
input file_name $100.;
run;
 
data _null_;
set dirlist end=end;
count+1;
call symputx('read'||put(count,4.-l),cats("\\tsclient\C\Transferencia de Archivos\IMPORT TO SAS\SIAM\",file_name));
call symputx('dset'||put(count,4.-l),scan(file_name,1,'.'));
if end then call symputx('max',count);
run;



data siamia.dirlist(rename=(file_name=leadfile));
set dirlist;
order=_n_;run;
 
options mprint symbolgen;
%macro readin;
%do i=1 %to &max;
 
data siamia.leadtarget&i;
set siamia.dirlist;
if &i=order;
run;
 
proc sql noprint;
select leadfile
into :lf
from siamia.leadtarget&i;
quit;
 
filename sgfxml "C:\Users\SAS\Desktop\siamia\&lf";
filename map "\\tsclient\C\Transferencia de Archivos\IMPORT TO SAS\SGFXML\siammapV2.map";
libname sgfxml xmlv2 xmlmap=map;
 
data siamia.lead;
set siamia.lead;
run;

%end;
%mend readin;
%readin;

 

Super User
Posts: 10,611

Re: Importing multiple xml files into 1 sas dataset.

Could you use the following code to import XML automatically ,not need offer XML map.

 

filename x temp;
libname   SXLELIB xmlv2  'c:\temp\temp.xml'  xmlmap=x automap=replace; 
proc copy in=SXLELIB  out=work ;
run;
Contributor
Posts: 23

Re: Importing multiple xml files into 1 sas dataset.

Hi, thanks for your reply, but this is only for one file, right?

I need to import hundreds into 1 single sas dataset. thanks

Super User
Super User
Posts: 9,200

Re: Importing multiple xml files into 1 sas dataset.

[ Edited ]

Well to start with you seem to have a network patch for the map:

"\\tsclient\C\Transferencia de Archivos\IMPORT
 22 ! TO SAS\SIAM\

 

And yet refer to a local c drive for the file:

filename sgfxml "C:\Users\SAS\Desktop\siamia\&lf";

 To be honest though your code is so hard to read, consider simplifying it:

/* These dont change */
filename  SXLELIB '\\tsclient\C\Transferencia de Archivos\IMPORT TO SAS\sgfxml\02303_20171016070200.xml';
filename  SXLEMAP '\\tsclient\C\Transferencia de Archivos\IMPORT TO SAS\SGFXML\siammapV2.map';
libname   SXLELIB xmlv2 xmlmap=SXLEMAP access=READONLY;

/* Macro to read in data */
%macro importfile (fn=,ds=);
  filename sgfxml "...";
  filename map "\\tsclient\C\Transferencia de Archivos\IMPORT TO SAS\SGFXML\siammapV2.map";
  libname sgfxml xmlv2 xmlmap=map;

  data siamia.lead;
    set siamia.lead;
  run;
%mend importfile;

filename DIRLIST pipe 'dir "\\tsclient\C\Transferencia de Archivos\IMPORT TO SAS\sgfxml\0*.xml" /b';

data _null_;
  infile dirlist lrecl=200 truncover dlm="¬";
  input;
  call execute(cats('%importfile (fn=',_input_,',ds=',scan(_input_,1,"."),');'));
run;

Contributor
Posts: 23

Re: Importing multiple xml files into 1 sas dataset.

Thanks, but both are correct, it doesn´t make any diference.

Ask a Question
Discussion stats
  • 4 replies
  • 208 views
  • 0 likes
  • 3 in conversation