<?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: Loop through observations and extract contents in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-observations-and-extract-contents/m-p/626678#M184864</link>
    <description>&lt;P&gt;Thank you all for your comments.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have currently have a lot of zipfiles (~120). And each zip file contains about 22 txt files. What I ultimately want to do is import each of the txt files. They are very large txt files, so my plan of attack is to loop through the following process:&lt;/P&gt;&lt;P&gt;1).&amp;nbsp; Unzip the first txt file in the current zip (file 1)&lt;/P&gt;&lt;P&gt;2). Import file (file 1)&lt;/P&gt;&lt;P&gt;3). Process file (file 1) to shrink its size&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;1) Unzip&amp;nbsp;the second txt file in the current zip (file 2)&lt;/P&gt;&lt;P&gt;2). Import file (file 2)&lt;/P&gt;&lt;P&gt;3). Process file (file 2)&amp;nbsp;to shrink its size&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;1) Unzip the nth txt file in the current zip (file n)&lt;/P&gt;&lt;P&gt;2). Import file (file n)&lt;/P&gt;&lt;P&gt;3). Process file (file n) to shrink its size&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;Finally append datasets (1,2,..n)&lt;/P&gt;&lt;P&gt;Then move on to the next zip file and do then same routine.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope this clears up my intentions.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I currently have done is create a dataset (CONTENTS), that collects the names of the txt files in the current zip file. I think if I can loop through the filenames, I can run the routine above.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 22 Feb 2020 22:19:39 GMT</pubDate>
    <dc:creator>AdamMadison</dc:creator>
    <dc:date>2020-02-22T22:19:39Z</dc:date>
    <item>
      <title>Loop through observations and extract contents</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-observations-and-extract-contents/m-p/626542#M184822</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the names of files as observations in a dataset and I would like to loop through them to pipe the filenames into my next step.&lt;/P&gt;&lt;P&gt;For more context, I have the dataset in the image below (work.contents). And I want to pipe the contents (just the name) of obs 1 ("&lt;BR /&gt;") into:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename xl "%sysfunc(getoption(work))/&lt;FONT color="#008000"&gt;&lt;STRONG&gt;NSDQsh20100802.txt&lt;/STRONG&gt;&lt;/FONT&gt;" ;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
   /* using member syntax here */
   infile inzip(&lt;FONT color="#008000"&gt;&lt;STRONG&gt;NSDQsh20100802.txt&lt;/STRONG&gt;&lt;/FONT&gt;) 
       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;&amp;nbsp;&lt;/P&gt;&lt;P&gt;where &lt;CODE class=" language-sas"&gt;&lt;FONT color="#008000"&gt;&lt;STRONG&gt;NSDQsh20100802.txt&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/CODE&gt; is the content of obs 1. After that is is complete, then do the same for obs 2, obs3 ...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Files.PNG" style="width: 236px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/36312i83AFBCA3C3117DF2/image-size/large?v=v2&amp;amp;px=999" role="button" title="Files.PNG" alt="Files.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Feb 2020 20:06:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-observations-and-extract-contents/m-p/626542#M184822</guid>
      <dc:creator>AdamMadison</dc:creator>
      <dc:date>2020-02-21T20:06:21Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through observations and extract contents</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-observations-and-extract-contents/m-p/626600#M184847</link>
      <description>&lt;P&gt;Here's a clever example that could be a good guide for you:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="http://pharma-sas.com/a-sas-macro-to-combine-portrait-and-landscape-rtf-files-into-one-single-file/" target="_blank" rel="noopener"&gt;http://pharma-sas.com/a-sas-macro-to-combine-portrait-and-landscape-rtf-files-into-one-single-file/&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The author is looping through RTF files (see "&lt;FONT face="lucida sans unicode,lucida sans"&gt;filename rtffiles (&amp;amp;rtffiles);&lt;/FONT&gt;")&lt;/P&gt;&lt;P&gt;Then this snippet cycles through each file:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&amp;nbsp; do until (eof);
    infile rtffiles lrecl=32767 end=eof filevar=fileloc;
    input;
    rtfcode=_infile_;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; *--- implement your logic ;&lt;BR /&gt;&amp;nbsp; end;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;It's a clever bit of code. Lot's of techniques to learn in that posting.&lt;/P&gt;</description>
      <pubDate>Sat, 22 Feb 2020 04:35:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-observations-and-extract-contents/m-p/626600#M184847</guid>
      <dc:creator>GGO</dc:creator>
      <dc:date>2020-02-22T04:35:42Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through observations and extract contents</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-observations-and-extract-contents/m-p/626655#M184859</link>
      <description>&lt;P&gt;Sounds like you are trying to expand the ZIP file?&lt;/P&gt;
&lt;P&gt;Why not just run an operating system command to unzip the file?&amp;nbsp; Such as the unzip command.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  infile "cd %sysfunc(pathname(work));unzip %sysfunc(pathname(inzip))" pipe;
  input;
  put _infile_;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 22 Feb 2020 19:16:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-observations-and-extract-contents/m-p/626655#M184859</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-02-22T19:16:15Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through observations and extract contents</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-observations-and-extract-contents/m-p/626671#M184863</link>
      <description>&lt;P&gt;What is that you are trying to do? What do you actually have?&lt;/P&gt;
&lt;P&gt;It looks like you have a dataset named CONTENTS with a variable named MEMNAME that that has a list of filenames.&lt;/P&gt;
&lt;P&gt;Do you want to use that to run that little data step?&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A simple pattern is to convert the code to macro definition. Then call the macro once for each value in the CONTENTS dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is that what you want to do?&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 22 Feb 2020 21:39:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-observations-and-extract-contents/m-p/626671#M184863</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-02-22T21:39:07Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through observations and extract contents</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-observations-and-extract-contents/m-p/626678#M184864</link>
      <description>&lt;P&gt;Thank you all for your comments.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have currently have a lot of zipfiles (~120). And each zip file contains about 22 txt files. What I ultimately want to do is import each of the txt files. They are very large txt files, so my plan of attack is to loop through the following process:&lt;/P&gt;&lt;P&gt;1).&amp;nbsp; Unzip the first txt file in the current zip (file 1)&lt;/P&gt;&lt;P&gt;2). Import file (file 1)&lt;/P&gt;&lt;P&gt;3). Process file (file 1) to shrink its size&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;1) Unzip&amp;nbsp;the second txt file in the current zip (file 2)&lt;/P&gt;&lt;P&gt;2). Import file (file 2)&lt;/P&gt;&lt;P&gt;3). Process file (file 2)&amp;nbsp;to shrink its size&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;1) Unzip the nth txt file in the current zip (file n)&lt;/P&gt;&lt;P&gt;2). Import file (file n)&lt;/P&gt;&lt;P&gt;3). Process file (file n) to shrink its size&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;Finally append datasets (1,2,..n)&lt;/P&gt;&lt;P&gt;Then move on to the next zip file and do then same routine.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope this clears up my intentions.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I currently have done is create a dataset (CONTENTS), that collects the names of the txt files in the current zip file. I think if I can loop through the filenames, I can run the routine above.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 22 Feb 2020 22:19:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-observations-and-extract-contents/m-p/626678#M184864</guid>
      <dc:creator>AdamMadison</dc:creator>
      <dc:date>2020-02-22T22:19:39Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through observations and extract contents</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-observations-and-extract-contents/m-p/626689#M184870</link>
      <description>&lt;P&gt;I don't expect the following to be the final solution for you but it should give you the pointers you need.&lt;/P&gt;
&lt;P&gt;The code below is based on Chris H's blog which given your post you've found already.&lt;/P&gt;
&lt;P&gt;I'm also attaching the sample zip file I've used so that below sample code becomes fully workable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code allows you to read a member in a zip file into SAS, "do stuff" with it, and then collect the result in a WANT dataset - and then repeat the process for the next member in the zip file.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* list members in zip file */
%macro zipMemList(source=, outds=memlist);
  /* Assign a fileref wth the ZIP method */
  filename inzip zip "&amp;amp;source";

  /* Read the "members" (files) from the ZIP file */
  data &amp;amp;outds(keep=zip memname);
   length zip $200 memname $200;
   zip="&amp;amp;source";
   fid=dopen("inzip");
   if fid=0 then
    stop;
   memcount=dnum(fid);
   do i=1 to memcount;
    memname=dread(fid,i);
    output;
   end;
   rc=dclose(fid);
  run;

  filename inzip clear;
%mend;
 
/* read member in zip file into SAS dataset */
%macro ReadMemInZip(source=, member=, outds=);
  /* Assign a fileref wth the ZIP method */
  filename inzip zip "&amp;amp;source";

  /* Import a text file directly from the ZIP */
  data _tmp;
   infile inzip(&amp;amp;member) 
     firstobs=1 dsd dlm=',';
   input 
    (var1-var3) ($) var4;
  run;

  /* append to want dataset */
  proc datasets lib=work nolist;
    append base=&amp;amp;outds data=_tmp;
    run;
    delete _tmp;
    run;
  quit;

  filename inzip clear;
%mend;


/* create dataset with members in specific zip file */
%zipMemList(source=~/test/files.zip, outds=memlist);

/* create dataset with all members in all zip files in a specific folder 
   - macro code here:
    https://blogs.sas.com/content/sasdummy/2016/10/16/filename-zip-list-file-contents/
*/
/*%listzipcontents (targdir=~/test, outlist=memlist);*/


/* execute macro %ReadMemInZip() once per member in zip file */
data _null_;
  set memlist;
  cmd=cats('%ReadMemInZip(source=',zip,', member=',memname,', outds=want)');
  call execute(cmd);
run;

title 'data combined';
proc print data=want;
run;
title;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 23 Feb 2020 04:35:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-observations-and-extract-contents/m-p/626689#M184870</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-02-23T04:35:31Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through observations and extract contents</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-observations-and-extract-contents/m-p/626697#M184872</link>
      <description>&lt;P&gt;Are the files all supposed to be in the same format?&amp;nbsp; If so you can read them all at once. You really don't want to use PROC IMPORT for text files. Especially if you are reading a lot of files in the same format.&amp;nbsp; It will have to GUESS how to define the variables and is likely to define the dataset differently for every file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How are you making them smaller?&amp;nbsp; Can you do it in the same step that is reading the text?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  infile '/path to file/filename.zip' zip member='*' truncover ;
  input ... ;
  if (...) then delete;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 23 Feb 2020 05:25:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-observations-and-extract-contents/m-p/626697#M184872</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-02-23T05:25:34Z</dc:date>
    </item>
  </channel>
</rss>

