DATA Step, Macro, Functions and more

Read unknown number .dat of batch files

Posts: 30

Read unknown number .dat of batch files

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;

/* 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;
/* If directory name call macro again */
%else %if %qscan(&name,2,.) = %then %do;


/* 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. */

Super User
Posts: 17,784

Re: Read unknown number .dat of batch files

The macro you pasted only creates a list of files. 


Maybe this maybe more helpful?


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

Ask a Question
Discussion stats
  • 1 reply
  • 2 in conversation