BookmarkSubscribeRSS Feed
Seb_A_Sanders
Calcite | Level 5

Hi,

 

I'm doing a looped import of a set of files in a folder. So far I have this:

 

 

%let path=\\server\Monthly;
%let tranloc = 2019-04;

filename folder "&path\&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 &Total;
  proc import datafile="&path\&transloc\&&name&i...csv"
       out=WORK.&&Table&i
       dbms=csv
       replace;
	   GUESSINGROWS=5000;
  run;
%end;
%mend inloop;
%inloop

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.

 

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:

https://blogs.sas.com/content/sasdummy/2017/09/08/filename-zip-details/

 

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.

 

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.

 

Thanks

 

1 REPLY 1
Tom
Super User Tom
Super User

Just update your first step that is getting the filenames to also try to call the FINFO() function.

 

There is an example in the documentation. 

*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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 6940 views
  • 1 like
  • 2 in conversation