<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Import zipped XML files in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Import-zipped-XML-files/m-p/322532#M271094</link>
    <description>&lt;P&gt;You're almost there. &amp;nbsp;You just need to add a middle step in which you use DATA step to read the XML file out of the zip archive and then write it to a temp space in your session. &amp;nbsp;Then you can use LIBNAME XML2 to read the XML as data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a similar example &lt;A href="http://blogs.sas.com/content/sasdummy/2015/05/11/using-filename-zip-to-unzip-and-read-data-files-in-sas/" target="_self"&gt;with an Excel file in this blog post&lt;/A&gt;.&lt;/P&gt;</description>
    <pubDate>Wed, 04 Jan 2017 22:39:58 GMT</pubDate>
    <dc:creator>ChrisHemedinger</dc:creator>
    <dc:date>2017-01-04T22:39:58Z</dc:date>
    <item>
      <title>Import zipped XML files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-zipped-XML-files/m-p/322500#M271093</link>
      <description>&lt;P&gt;All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I get multiple&amp;nbsp;zipped folders that contain XML files. I want to "point" SAS into the zipped folder and parse out the XML files into records in a SAS dataset.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's a diagram of what I'm dealing with:&lt;/P&gt;&lt;PRE&gt;data.zip&lt;BR /&gt;  |__sub_folder
        |__ file1.xml
        |__ file2.xml&lt;BR /&gt;        ...&lt;BR /&gt;        |__file500.xml&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I know I can use a FILENAME with the ZIP option then read in the contents of a zipped folder:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename inzip ZIP "C:\input_data\data.zip";&lt;BR /&gt;&lt;BR /&gt;data folder_contents;
 length memname $200 isFolder 8;
 fid=dopen("inzip");
 if fid=0 then
  stop;
 memcount=dnum(fid);
 do i=1 to memcount;
	/*Scans and gives only the name of the xml file*/
  	memname=scan(dread(fid,i), 2, '\');
  	/*check for trailing / in folder name */
  	output;
 end;
 rc=dclose(fid);
run;&lt;BR /&gt;&lt;BR /&gt;title "Files in the ZIP file";&lt;BR /&gt;proc print data=folder_contents noobs N;&lt;BR /&gt;run;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;And I know I can use the LIBNAME engine with the XML option (in this case XMLV2) with an XML map (made from the XML Mapper) to read in XML files into a dataset:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename SXLEMAP "C:\xml_map\my_map2.map";
libname my_xml_file XMLV2 "C:\XML_files\test.xml" xmlmap = SXLEMAP;&lt;BR /&gt;libname out "C:\output";

data out.xml_contents;&lt;BR /&gt;    set my_xml_file.test;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;How do I get a LIBNAME&amp;nbsp;pointing "inside" a zipped folder, if I have to use the FILENAME engine to look inside said folder?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Right now, I believe we can only use the ZIP options in the FILENAME engine, not the LIBNAME.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to avoid unzipping since it takes a while to unzip all of the XML files (and IT at my agency has strong restrictions on using X commands. Otherwise, I'd use X commands&amp;nbsp;to move the XML files from the zipped folder to a staging&amp;nbsp;folder, then use a macro to import the XML files).&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Jan 2017 21:30:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-zipped-XML-files/m-p/322500#M271093</guid>
      <dc:creator>athomson</dc:creator>
      <dc:date>2017-01-04T21:30:26Z</dc:date>
    </item>
    <item>
      <title>Re: Import zipped XML files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-zipped-XML-files/m-p/322532#M271094</link>
      <description>&lt;P&gt;You're almost there. &amp;nbsp;You just need to add a middle step in which you use DATA step to read the XML file out of the zip archive and then write it to a temp space in your session. &amp;nbsp;Then you can use LIBNAME XML2 to read the XML as data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a similar example &lt;A href="http://blogs.sas.com/content/sasdummy/2015/05/11/using-filename-zip-to-unzip-and-read-data-files-in-sas/" target="_self"&gt;with an Excel file in this blog post&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Wed, 04 Jan 2017 22:39:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-zipped-XML-files/m-p/322532#M271094</guid>
      <dc:creator>ChrisHemedinger</dc:creator>
      <dc:date>2017-01-04T22:39:58Z</dc:date>
    </item>
    <item>
      <title>Re: Import zipped XML files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-zipped-XML-files/m-p/322732#M271095</link>
      <description>&lt;P&gt;Got it!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is what I got:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename inzip ZIP "C:\input_data\data.zip";

data folder_contents;
 length memname $200 isFolder 8;
 fid=dopen("inzip");
 if fid=0 then
  stop;
 memcount=dnum(fid);
 do i=1 to memcount;
	/*Scans and gives only the name of the xml file*/
  	memname=scan(dread(fid,i), 2, '\');
  	output;
 end;
 rc=dclose(fid);
run;

/* create a report of the ZIP contents */
title "Files in the ZIP file";
proc print data=folder_contents noobs N;
run;

/* identify a temp folder in the WORK directory */
filename xl "%sysfunc(getoption(work))/file1.xml" ;
 
/* hat tip: "data _null_" on SAS-L */
data _null_;
   /* using member syntax here */
   infile inzip(sub_folder\file1.xml) 
       	lrecl=256 
		recfm=F 
		length=length 
		eof=eof unbuf;
   file   xl lrecl=256 recfm=N;
   input;
   put _infile_ $varying256. length;
   return;
 eof:
   stop;
run;

filename SXLEMAP "C:\xml_map\my_map2.map';
libname my_xml XMLV2 "%sysfunc(getoption(work))/file1.xml" xmlmap=sxlemap access=readonly;&lt;BR /&gt;libname out "C:\Output";

data out.xml_contents;
	set my_xml.test;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;It's the intermediate data _null_ step I'm a little shaky on. So data step basically tells SAS to pluck the xml file from inside the zipped folder and stick it in a temporary work directory? This then allows me to set a LIBNAME on that file in the temporary work directory and leverage the XML engine to parse out the XML?&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jan 2017 17:36:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-zipped-XML-files/m-p/322732#M271095</guid>
      <dc:creator>athomson</dc:creator>
      <dc:date>2017-01-05T17:36:55Z</dc:date>
    </item>
    <item>
      <title>Re: Import zipped XML files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-zipped-XML-files/m-p/322743#M271096</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/120146"&gt;@athomson&lt;/a&gt;&amp;nbsp;- Exactly! &amp;nbsp;Good job!&lt;/P&gt;</description>
      <pubDate>Thu, 05 Jan 2017 18:06:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-zipped-XML-files/m-p/322743#M271096</guid>
      <dc:creator>ChrisHemedinger</dc:creator>
      <dc:date>2017-01-05T18:06:37Z</dc:date>
    </item>
    <item>
      <title>Re: Import zipped XML files</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Import-zipped-XML-files/m-p/323846#M271097</link>
      <description>&lt;P&gt;Hi Chris (and anyone else reading this post)!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;We've made some pretty good progress so far in trying to "macro-tize" the above so that it will read into multiple zipped folders and then parse out multiple XML files into records in a dataset.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;See the post below to offer any thoughts how we can overcome a couple of problems, and even make the macro run more efficiently!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://communities.sas.com/t5/General-SAS-Programming/Improving-a-macro-that-parses-out-multiple-XML-files-from/m-p/323844#M42600&amp;nbsp;" target="_blank"&gt;https://communities.sas.com/t5/General-SAS-Programming/Improving-a-macro-that-parses-out-multiple-XML-files-from/m-p/323844#M42600&amp;nbsp;&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Jan 2017 03:15:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Import-zipped-XML-files/m-p/323846#M271097</guid>
      <dc:creator>athomson</dc:creator>
      <dc:date>2017-01-11T03:15:40Z</dc:date>
    </item>
  </channel>
</rss>

