DATA Step, Macro, Functions and more

Read unknown number .dat of batch files

Reply
Contributor
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;
%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)  

Super User
Posts: 19,789

Re: Read unknown number .dat of batch files

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. 

Ask a Question
Discussion stats
  • 1 reply
  • 125 views
  • 0 likes
  • 2 in conversation