<?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 unzip more than one file in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/unzip-more-than-one-file/m-p/815157#M321738</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename indata pipe 'dir P:\ALG/b';
/*to read the name of files in a folder*/
data file_list_ALG;
length fname $70;
infile indata truncover;
input fname $70.;
call symput ('num_files_alg',_n_);
run;
filename inzip ZIP 'P:\ALG\????';
 
/* Read the "members" (files) from the ZIP file */
data contents(keep=memname isFolder);
 length memname $200 isFolder 8;
 fid=dopen("inzip");
 if fid=0 then
  stop;
 memcount=dnum(fid);
 do i=1 to memcount;
  memname=dread(fid,i);
  /* check for trailing / in folder name */
  isFolder = (first(reverse(trim(memname)))='/');
  output;
 end;
 rc=dclose(fid);
run;



filename xl "%sysfunc(getoption(work))/?????.txt" ;
 
/* hat tip: "data _null_" on SAS-L */
data _null_;
   /* using member syntax here */
   infile inzip(?????.txt) 
       lrecl=256 recfm=F length=length eof=eof unbuf;
   file   xl lrecl=256 recfm=N;
   input;
   put _infile_ $varying256. length;
   return;
 eof:
   stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;I have a folder that includes zipped folders. I want to unzip those files and get the data. Here is what i am trying to do:&lt;/P&gt;
&lt;P&gt;1. At the first step i am getting name of zipped folders.&lt;/P&gt;
&lt;P&gt;2. Then i am trying to use those names. The question marks mean how I can use those folders name that I got from the first step.&lt;/P&gt;
&lt;P&gt;3.I am trying to use the files in the folders which i get from the second step.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since i have more than one zipped folder i don't want to do manually.&lt;/P&gt;
&lt;P&gt;any ideas??&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
    <pubDate>Thu, 26 May 2022 02:06:52 GMT</pubDate>
    <dc:creator>dustychair</dc:creator>
    <dc:date>2022-05-26T02:06:52Z</dc:date>
    <item>
      <title>unzip more than one file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/unzip-more-than-one-file/m-p/815157#M321738</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename indata pipe 'dir P:\ALG/b';
/*to read the name of files in a folder*/
data file_list_ALG;
length fname $70;
infile indata truncover;
input fname $70.;
call symput ('num_files_alg',_n_);
run;
filename inzip ZIP 'P:\ALG\????';
 
/* Read the "members" (files) from the ZIP file */
data contents(keep=memname isFolder);
 length memname $200 isFolder 8;
 fid=dopen("inzip");
 if fid=0 then
  stop;
 memcount=dnum(fid);
 do i=1 to memcount;
  memname=dread(fid,i);
  /* check for trailing / in folder name */
  isFolder = (first(reverse(trim(memname)))='/');
  output;
 end;
 rc=dclose(fid);
run;



filename xl "%sysfunc(getoption(work))/?????.txt" ;
 
/* hat tip: "data _null_" on SAS-L */
data _null_;
   /* using member syntax here */
   infile inzip(?????.txt) 
       lrecl=256 recfm=F length=length eof=eof unbuf;
   file   xl lrecl=256 recfm=N;
   input;
   put _infile_ $varying256. length;
   return;
 eof:
   stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;I have a folder that includes zipped folders. I want to unzip those files and get the data. Here is what i am trying to do:&lt;/P&gt;
&lt;P&gt;1. At the first step i am getting name of zipped folders.&lt;/P&gt;
&lt;P&gt;2. Then i am trying to use those names. The question marks mean how I can use those folders name that I got from the first step.&lt;/P&gt;
&lt;P&gt;3.I am trying to use the files in the folders which i get from the second step.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since i have more than one zipped folder i don't want to do manually.&lt;/P&gt;
&lt;P&gt;any ideas??&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Thu, 26 May 2022 02:06:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/unzip-more-than-one-file/m-p/815157#M321738</guid>
      <dc:creator>dustychair</dc:creator>
      <dc:date>2022-05-26T02:06:52Z</dc:date>
    </item>
    <item>
      <title>Re: unzip more than one file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/unzip-more-than-one-file/m-p/815158#M321739</link>
      <description>&lt;P&gt;It is not clear what you are trying to do with the ZIP files.&lt;/P&gt;
&lt;P&gt;Are you trying to find out the names of the files in the ZIP files?&lt;/P&gt;
&lt;P&gt;Or are you trying to unzip the files?&amp;nbsp; &amp;nbsp;Since it looks like you can run operating system commands why not just run an operating system command that can unzip the zip file?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But whatever you are trying to do get it to work for ONE zip file.&lt;/P&gt;
&lt;P&gt;Convert that code into a macro that takes the name of the ZIP file as input.&amp;nbsp; So something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro unzip(filename);
filename zip zip "&amp;amp;filename";
....
%mend unzip;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then use the data from your first data step (the list of file names) to call that macro once for each file.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set file_list_ALG;
  where lowcase(scan(fname,-1,'.'))='zip';
  call execute(cats('%nrstr(%unzip)(',fname,')'));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 26 May 2022 02:17:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/unzip-more-than-one-file/m-p/815158#M321739</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-05-26T02:17:05Z</dc:date>
    </item>
    <item>
      <title>Re: unzip more than one file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/unzip-more-than-one-file/m-p/815159#M321740</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;. Long time ago I asked how to read (I meant unzip) csv files, you answered my question again but I could not resolve it. so, since i still don't know how to unzip files i am trying to find a way to do it. i used&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4"&gt;@ChrisHemedinger&lt;/a&gt;'s example but since I have more than one folder i failed.&lt;/P&gt;
&lt;P&gt;that's the story of my question.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;simple and silly question: Should i use "filename" in the parenthesis after unzip?&lt;/P&gt;</description>
      <pubDate>Thu, 26 May 2022 02:32:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/unzip-more-than-one-file/m-p/815159#M321740</guid>
      <dc:creator>dustychair</dc:creator>
      <dc:date>2022-05-26T02:32:45Z</dc:date>
    </item>
    <item>
      <title>Re: unzip more than one file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/unzip-more-than-one-file/m-p/815160#M321741</link>
      <description>&lt;P&gt;On my Unix system where SAS runs I can use the command unzip to unzip a ZIP file. The syntax is just&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;unzip filename&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Where FILENAME is the name of the zip file.&amp;nbsp; What ever files were in the ZIP file will be written to the current directory.&amp;nbsp; If the files in the ZIP are organized into folders then those folders are created under the current directory.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So if I wanted the unzip the files in /home/xyz/FRED.ZIP and write them into the directory /home/xyz/target I could just run this code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  infile "cd /home/xyz/target ; unzip /home/xyz/FRED.ZIP" pipe ;
  input;
  put _infile_;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So the easiest thing to do is to find out what is the command line command you can use to unzip a file on the machine where SAS is running and then generate that command.&amp;nbsp; For example a lot of users are using 7Zip as the tool for working with ZIP files.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;Should i use "filename" in the parenthesis after unzip?&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I assume you are asking what you should pass to the macro when you call it?&amp;nbsp; That is totally up to you and depends on how you write the macro.&amp;nbsp; In the example I posted the macro parameter FILENAME is used to create a SAS fileref using the ZIP engine.&amp;nbsp; So the value that it should have is the fully qualified name of the a ZIP file.&amp;nbsp; So something like one of these depending on whether SAS it running on Windows or Unix.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%unzip(c:\users\xyz\myfile.zip)
%unzip(/home/xyz/myfile.zip)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Once you have a flieref named ZIP that points to a ZIP file you can open it with the DOPEN() command and use the DREAD() command to get the list of members in the file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Once you have one member name in the ZIP you could use that to point to that specific member in that specific zip file by using syntax like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename next zip "zipfilename" member="membername" ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which you might be able to use the FCOPY() function to copy to an actual physical file somewhere.&amp;nbsp; Make sure you create a valid filename for the new file (and one that does not conflict with any existing file).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 May 2022 02:50:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/unzip-more-than-one-file/m-p/815160#M321741</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-05-26T02:50:26Z</dc:date>
    </item>
  </channel>
</rss>

