<?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 files in folder and import if not empty in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-files-in-folder-and-import-if-not-empty/m-p/560236#M156609</link>
    <description>&lt;P&gt;Just update your first step that is getting the filenames to also try to call the FINFO() function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There is an example in the documentation.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*This example stores information items about an external file in a SAS data set;
data info;
   length infoname infoval $60;
   drop rc fid infonum i close;
   rc=filename('abc', 'physical-filename');
   fid=fopen('abc');
   infonum=foptnum(fid);
   do i=1 to infonum;
      infoname=foptname(fid, i);
      infoval=finfo(fid, infoname);
      output;
   end;
   close=fclose(fid);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 20 May 2019 17:42:41 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2019-05-20T17:42:41Z</dc:date>
    <item>
      <title>Loop through files in folder and import if not empty</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-files-in-folder-and-import-if-not-empty/m-p/560206#M156586</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm doing a looped import of a set of files in a folder. So far I have this:&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;%let path=\\server\Monthly;
%let tranloc = 2019-04;

filename folder "&amp;amp;path\&amp;amp;transloc\";
options validmemname=extend;
 
/* Making a list of all files in the folder */
data IL_FilesInFolder;
   length Line 8 File $300;
   List = dopen('folder');
   do Line = 1 to dnum(List);
        File = trim(dread(List,Line));
		output;
   end;
   drop list line;
run;

DATA IL_FIF;
SET IL_FilesInFolder;
WHERE File CONTAINS 'Monthly_Transaction_Extract';
RUN;

data _null_;
     set IL_FIF end=final;
     call symput(cats('File', _N_), trim(File));
	 call symput(cats('Name', _N_), trim(nliteral(substr(File,1,LENGTH(File)-4))));
	 call symput(cats('Table', _N_), trim(nliteral(substr(File,41,10))));
     if final then call symputx(trim('Total'), _N_); /* replaced symput by symputx */
run;

%macro inloop;
%do i = 1 %to &amp;amp;Total;
  proc import datafile="&amp;amp;path\&amp;amp;transloc\&amp;amp;&amp;amp;name&amp;amp;i...csv"
       out=WORK.&amp;amp;&amp;amp;Table&amp;amp;i
       dbms=csv
       replace;
	   GUESSINGROWS=5000;
  run;
%end;
%mend inloop;
%inloop&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This month I had an error during the inloop section. The problem... one of the transaction extract files was empty. This is entirely possible so no problem there.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I then considered was conditionally importing based on whether the file was empty. I figured file size was the best way to do this. I found the following:&lt;/P&gt;&lt;P&gt;&lt;A href="https://blogs.sas.com/content/sasdummy/2017/09/08/filename-zip-details/" target="_self"&gt;https://blogs.sas.com/content/sasdummy/2017/09/08/filename-zip-details/&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Which seemed like it might be the right starting point, but I'm struggling to adapt this to loop through my list of files, I'm guessing because this assigns the file name based on a single zip as opposed to the folder.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help would be appreciated, but if a different approach is better I'm happy to consider this. For reference, I have no access to the file production process so I can't step in earlier to resolve this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 May 2019 16:03:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-files-in-folder-and-import-if-not-empty/m-p/560206#M156586</guid>
      <dc:creator>Seb_A_Sanders</dc:creator>
      <dc:date>2019-05-20T16:03:28Z</dc:date>
    </item>
    <item>
      <title>Re: Loop through files in folder and import if not empty</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-through-files-in-folder-and-import-if-not-empty/m-p/560236#M156609</link>
      <description>&lt;P&gt;Just update your first step that is getting the filenames to also try to call the FINFO() function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There is an example in the documentation.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*This example stores information items about an external file in a SAS data set;
data info;
   length infoname infoval $60;
   drop rc fid infonum i close;
   rc=filename('abc', 'physical-filename');
   fid=fopen('abc');
   infonum=foptnum(fid);
   do i=1 to infonum;
      infoname=foptname(fid, i);
      infoval=finfo(fid, infoname);
      output;
   end;
   close=fclose(fid);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 20 May 2019 17:42:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-through-files-in-folder-and-import-if-not-empty/m-p/560236#M156609</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-05-20T17:42:41Z</dc:date>
    </item>
  </channel>
</rss>

