Sample 35344: How to read all XML files in a directory with the XML92 LIBNAME engine
The new XML92 LIBNAME engine allows you to import all XML files in a directory using a wildcard on the LIBNAME statement. Prior to SAS® 9.2, reading all XML files in a directory required the use of macro logic along with the XML LIBNAME engine.
Please see the sample code on the Full Code tab for an illustration of both methods.
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
The example below first generates three sample XML files. The code then demonstrates how to read all of the XML files in a directory in SAS® 9.2 using the XML92 LIBNAME engine. The code also illustrates how to read all of the XML files in a directory using the XML LIBNAME engine with SAS 9.2 and earlier releases.
/**********************************/
/* Create three sample XML files. */
%macro test;
%do i=1 %to 3;
libname temp xml "c:\temp\text&i..xml";
proc copy in=sashelp out=temp;
select class;
run;
%end;
%mend;
%test
libname temp clear;
/********************************************************/
/* The following code reads all of the .XML files in a */
/* directory in SAS 9.2 using the XML92 LIBNAME engine. */
libname temp xml92 "c:\temp\*.xml";
proc copy in=temp out=work;
run;
/*******************************************************************/
/* The following code reads all of the .XML files in a directory */
/* in SAS 9.2 and prior releases using the the XML LIBNAME engine. */
libname templib "c:\temp\xml_data"; /* Location to store output data sets */
options mprint sgen mlogic;
%macro drive(dir,mapfile);
/** Creates a fileref **/
%let filerf=mydir;
/** Assigns the fileref to the directory you
passed in **/
%let rc=%sysfunc(filename(filrf,&dir));
/** Opens the directory to be read **/
%let did=%sysfunc(dopen(&filrf));
/** Returns the number of members in the directory that
you passed in **/
%let memcnt=%sysfunc(dnum(&did));
%let cnt=0;
%do i = 1 %to &memcnt;
/** Return the extension of the file found **/
%let ext=%upcase(%scan(%qsysfunc(dread(&did,&i)),2));
/** Return the first name of the file **/
%let fname=%scan(%qsysfunc(dread(&did,&i)),1);
%if &ext=XML %then %do;
%let cnt=%eval(&cnt+1);
/** Create a different macro variable for each file
found **/
%let name&cnt=&fname..&ext;
%end;
%end;
%do j = 1 %to &cnt;
%if &mapfile ne %then %do;
filename mapfile "&mapfile";
libname sxle&i xml "&dir.&&name&j" xmlmap=mapfile;
%end;
%else %do;
libname sxle&i xml "&dir.&&name&j";
%end;
proc copy in=sxle&i out=work;
run;
/* Copy the files to a temporary work file */
proc sql;
select count(memname) into:total
from dictionary.tables
where libname="WORK" and memtype="DATA" ;
select memname into:mem1-:mem%left(&total)
from dictionary.tables
where libname="WORK" and memtype="DATA" ;
run;
/* Copy the files from the temporary work file to a library */
%do i=1 %to &total;
proc append base=templib.&&mem&i data=&&mem&i force;
run;
proc datasets lib=work;
delete &&mem&i;
run;
quit;
%end;
libname sxle&i clear;
%end;
/** Close the directory **/
%let rc=%sysfunc(dclose(&did));
%mend drive;
/** Replace the path below with your own directory to search for the files and the map **/
%drive(c:\temp\xml_data\)
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
This sample illustrates how to read all XML files in a directory using the XML92 LIBNAME engine.
| Date Modified: | 2010-08-30 16:21:56 |
| Date Created: | 2009-03-27 10:15:17 |
Operating System and Release Information
| SAS System | Base SAS | z/OS | | |
| OpenVMS VAX | | |
| Microsoft® Windows® for 64-Bit Itanium-based Systems | | |
| Microsoft Windows Server 2003 Datacenter 64-bit Edition | | |
| Microsoft Windows Server 2003 Enterprise 64-bit Edition | | |
| Microsoft Windows XP 64-bit Edition | | |
| Microsoft® Windows® for x64 | | |
| OS/2 | | |
| Microsoft Windows 7 | | |
| Microsoft Windows 95/98 | | |
| Microsoft Windows 2000 Advanced Server | | |
| Microsoft Windows 2000 Datacenter Server | | |
| Microsoft Windows 2000 Server | | |
| Microsoft Windows 2000 Professional | | |
| Microsoft Windows NT Workstation | | |
| Microsoft Windows Server 2003 Datacenter Edition | | |
| Microsoft Windows Server 2003 Enterprise Edition | | |
| Microsoft Windows Server 2003 Standard Edition | | |
| Microsoft Windows Server 2008 | | |
| Microsoft Windows XP Professional | | |
| Windows Millennium Edition (Me) | | |
| Windows Vista | | |
| 64-bit Enabled AIX | | |
| 64-bit Enabled HP-UX | | |
| 64-bit Enabled Solaris | | |
| ABI+ for Intel Architecture | | |
| AIX | | |
| HP-UX | | |
| HP-UX IPF | | |
| IRIX | | |
| Linux | | |
| Linux for x64 | | |
| Linux on Itanium | | |
| OpenVMS Alpha | | |
| OpenVMS on HP Integrity | | |
| Solaris | | |
| Solaris for x64 | | |
| Tru64 UNIX | | |