BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
vanmon1
Obsidian | Level 7

I have a map that works with my xml files and I managed to process all of them with xmlmap and get all proc contents for them but I can't view the datasets in sas to start working with the data. I attach one of the xml files as an example  . Could someone please help? Thanks in advance.


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

/*
 *  Catalog
 */

proc datasets lib=SXLELIB; run;

/*
 *  Contents
 */

proc contents data=SXLELIB.alerta varnum; run;

 *  Printing
 */

title 'Table alerta';
proc print data=SXLELIB.alerta; run;
title;

/*
 *  Local Extraction
 */

DATA alerta; SET SXLELIB.alerta; run;


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

data file_list; 
length fname $20; 
infile prueba truncover; /* infile statement for file names */
input fname $20.; /* read the file names from the directory */
call symput ('num_files',_n_); /* store the record number in a macro variable */
run; 


%macro fileread; 
%do j=1 %to &num_files; 

data _null_; 
set file_list; 
if _n_=&j; 
call symput ('filein',fname); 
run; 



filename  SXLELIB "\\tsclient\C\Transferencia de Archivos\IMPORT TO SAS\SIAM\&filein..xml";
filename  SXLEMAP '\\tsclient\C\Transferencia de Archivos\IMPORT TO SAS\SIAM\siammapV2.map';
libname   SXLELIB xmlv2 xmlmap=SXLEMAP access=READONLY;

 
proc datasets lib=SXLELIB; run;

proc contents data=sxlelib._all_ varnum; run;




%end; /* end of do-loop with index j */
%mend fileread; 
%fileread; 


proc copy in=sxlelib out=work;
run;

  

1 ACCEPTED SOLUTION

Accepted Solutions
vanmon1
Obsidian | Level 7

GOT IT! Partly thanks to "A Macro for Reading Multiple Text Files"  by Debbie Miller, National Park Service, Denver, CO " and user Reeza who made a question that made me think about one piece of my code.

/*libname root created by xml mapper*/

libname   SXLELIB xmlv2 xmlmap=SXLEMAP access=READONLY;

/*create macro to read all file names in folder*/
filename prueba  pipe 'dir "\\tsclient\C\Transferencia de Archivos\IMPORT TO SAS\siam\*.xml" /b';

data file_list; 
length fname $20; 
infile prueba truncover; /* infile statement for file names */
input fname $20.; /* read the file names from the directory */
call symput ('num_files',_n_); /* store the record number in a macro variable */
run; 


%macro fileread; 
%do j=1 %to &num_files; 

data _null_; 
set file_list; 
if _n_=&j; 
call symput ('filein',fname); 
run; 


/*code to retrieve xml map created*/
filename  SXLELIB "\\tsclient\C\Transferencia de Archivos\IMPORT TO SAS\SIAM\&filein..xml";
filename  SXLEMAP '\\tsclient\C\Transferencia de Archivos\IMPORT TO SAS\SIAM\siammapv2.map';
libname   SXLELIB xmlv2 xmlmap=SXLEMAP access=READONLY ;

 
proc datasets lib=SXLELIB; run;

proc contents data=sxlelib._all_ varnum; run;

/*copy sxlelib to your library*/

proc copy in=sxlelib out=work;
run;

/*save each file read named 'alerta' as in xml mapper in a single sas dataset call data_all*/
%if &j=1 %then %do;
 data data_all;
 set alerta;
 run;
%end;
%else %do;
 data data_all;
 set data_all
 alerta;
 run;
%end;


%end; 
%mend fileread; 
%fileread; 

View solution in original post

6 REPLIES 6
Reeza
Super User

> I can't view the datasets in sas to start working with the data.

 

What does that mean? Post your log instead.

vanmon1
Obsidian | Level 7

Thanks for your interest. I'm trying with 4 xml files but have hundreds to import.

vanmon1
Obsidian | Level 7

I also attach the output. Thanks.

Reeza
Super User

Forget the macro for now, do you have a process to read a single file working and imported data. 

 

I don't actually see any steps (PROC COPY?) where you try to store the data...the first attempt seems to work fine but it's hard to follow. I won't have time to test anything until later but wanted to make sure I understand where you're currently at.

vanmon1
Obsidian | Level 7

Ok, now because of your question I realised I placed proc copy outside the macro so I changed my code and now I can see one resulting dataset in work library, but only one, the last file generated. I supose that's because they all get the same name 'alerta' given in sas xml mapper application and the next dataset overwrites the previous one.. how can I change that and see all of them? Your help is being much appreciated.

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

data file_list; 
length fname $20; 
infile prueba truncover; /* infile statement for file names */
input fname $20.; /* read the file names from the directory */
call symput ('num_files',_n_); /* store the record number in a macro variable */
run; 


%macro fileread; 
%do j=1 %to &num_files; 

data _null_; 
set file_list; 
if _n_=&j; 
call symput ('filein',fname); 
run; 



filename  SXLELIB "\\tsclient\C\Transferencia de Archivos\IMPORT TO SAS\SIAM\&filein..xml";
filename  SXLEMAP '\\tsclient\C\Transferencia de Archivos\IMPORT TO SAS\SIAM\siammapV2.map';
libname   SXLELIB xmlv2 xmlmap=SXLEMAP ;

 
proc datasets lib=SXLELIB; run;

proc contents data=sxlelib._all_ varnum; run;



proc copy in=sxlelib out=work;
run;


%end; /* end of do-loop with index j */
%mend fileread; 
%fileread; 

 

vanmon1
Obsidian | Level 7

GOT IT! Partly thanks to "A Macro for Reading Multiple Text Files"  by Debbie Miller, National Park Service, Denver, CO " and user Reeza who made a question that made me think about one piece of my code.

/*libname root created by xml mapper*/

libname   SXLELIB xmlv2 xmlmap=SXLEMAP access=READONLY;

/*create macro to read all file names in folder*/
filename prueba  pipe 'dir "\\tsclient\C\Transferencia de Archivos\IMPORT TO SAS\siam\*.xml" /b';

data file_list; 
length fname $20; 
infile prueba truncover; /* infile statement for file names */
input fname $20.; /* read the file names from the directory */
call symput ('num_files',_n_); /* store the record number in a macro variable */
run; 


%macro fileread; 
%do j=1 %to &num_files; 

data _null_; 
set file_list; 
if _n_=&j; 
call symput ('filein',fname); 
run; 


/*code to retrieve xml map created*/
filename  SXLELIB "\\tsclient\C\Transferencia de Archivos\IMPORT TO SAS\SIAM\&filein..xml";
filename  SXLEMAP '\\tsclient\C\Transferencia de Archivos\IMPORT TO SAS\SIAM\siammapv2.map';
libname   SXLELIB xmlv2 xmlmap=SXLEMAP access=READONLY ;

 
proc datasets lib=SXLELIB; run;

proc contents data=sxlelib._all_ varnum; run;

/*copy sxlelib to your library*/

proc copy in=sxlelib out=work;
run;

/*save each file read named 'alerta' as in xml mapper in a single sas dataset call data_all*/
%if &j=1 %then %do;
 data data_all;
 set alerta;
 run;
%end;
%else %do;
 data data_all;
 set data_all
 alerta;
 run;
%end;


%end; 
%mend fileread; 
%fileread; 

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
  • 6 replies
  • 2198 views
  • 1 like
  • 2 in conversation