BookmarkSubscribeRSS Feed
jakestat
Obsidian | Level 7

I have an unknown number of files to read in from the F:\DATA directory.  The following code is directly from SAS documentation... but I can't dicipher how to make it work for my directory where the file names are .dat extentions with an unknown/unlimited number batch files.   Where do I put my infile statement, and where do I specify the file names (data01.dat, data02.dat, data03.dat....etc.)?

Thank you for any help!

 

LAN directory %macro drive(dir,ext);
%local filrf rc did memcnt name i;

/* Assigns a fileref to the directory and opens the directory */
%let rc=%sysfunc(filename(filrf,&dir));
%let did=%sysfunc(dopen(&filrf));

/* Make sure directory can be open */
%if &did eq 0 %then %do;
%put Directory &dir cannot be open or does not exist;
%return;
%end;

/* Loops through entire directory */
%do i = 1 %to %sysfunc(dnum(&did));

/* Retrieve name of each file */
%let name=%qsysfunc(dread(&did,&i));

/* Checks to see if the extension matches the parameter value */
/* If condition is true print the full name to the log */
%if %qupcase(%qscan(&name,-1,.)) = %upcase(&ext) %then %do;
%put &dir\&name;
%end;
/* If directory name call macro again */
%else %if %qscan(&name,2,.) = %then %do;
%drive(&dir\%unquote(&name),&ext)
%end;

%end;

/* Closes the directory and clear the fileref */
%let rc=%sysfunc(dclose(&did));
%let rc=%sysfunc(filename(filrf));

%mend drive;

/* First parameter is the directory of where your files are stored. */
/* Second parameter is the extension you are looking for. */
%drive(F:\Viking45\DATA,sas)  

1 REPLY 1
Reeza
Super User

The macro you pasted only creates a list of files. 

 

Maybe this maybe more helpful?

https://communities.sas.com/t5/SAS-Communities-Library/How-do-I-write-a-macro-to-import-multiple-tex...

 

If not, search on here, at least 3 solutions to this problem in last week. 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 832 views
  • 0 likes
  • 2 in conversation