The goal of this macro was to take a file directory and file extension as parameters, and add all the files in the directory with that extension to a SAS data set to be processed later. I found this code segment online: 40934 - Retrieve file size, create time, and last modified date of an external file But it is only for one file. The properties are retrieved no problem in the code below, but the next iteration of the loop changes all the existing values in the data set. I'm guessing this is because SAS is only storing a reference to the actual value, which then changes. Is there anyway to force it to copy the actual value. I'd really like this to be a macro but is that impossible? Code (messy) - displays info in log and prints data set. Data set info incorrect for reasons mentioned above:
%global tfilename Bytes;
%macro fileAttribs(ofilename, dir);
%let filename=&dir\&ofilename;
%let tfilename=&ofilename;
%local rc fid fidc;
%local CreateDT ModifyDT;
%let rc=%sysfunc(filename(onefile,&filename));
%let fid=%sysfunc(fopen(&onefile));
%let Bytes=%sysfunc(finfo(&fid,File Size (bytes)));
%let CreateDT=%sysfunc(finfo(&fid,Create Time));
%let ModifyDT=%sysfunc(finfo(&fid,Last Modified));
%let fidc=%sysfunc(fclose(&fid));
%let rc=%sysfunc(filename(onefile));
%put NOTE: File size of &ofilename is &Bytes bytes;
%put NOTE: Created &CreateDT;
%put NOTE: Last modified &ModifyDT;
%mend fileAttribs;
%macro drive(dir,ext);
%let filrf=mydir;
/* Assigns the fileref of mydir to the directory and opens the directory */
%let rc=%sysfunc(filename(filrf,&dir));
%let did=%sysfunc(dopen(&filrf));
/* Returns the number of members in the directory */
%let memcnt=%sysfunc(dnum(&did));
/* Loops through entire directory */
data filespec; /* Create data set to capture file name and size */
length ffname$ 30 ffsize$ 10;
%do i = 1 %to &memcnt;
/* Returns the extension from each file */
%let name=%qscan(%qsysfunc(dread(&did,&i)),-1,.);
/* Checks to see if file contains an extension */
%if %qupcase(%qsysfunc(dread(&did,&i))) ne %qupcase(&name) %then
%do;
/* Checks to see if the extension matches the parameter value */
/* If condition is true prints the full name to the log */
%if (%superq(ext) ne and %qupcase(&name) = %qupcase(&ext)) or
(%superq(ext) = and %superq(name) ne) %then
%do;
%fileAttribs(%qsysfunc(dread(&did,&i)), &dir)
ffname=symget('tfilename');
ffsize=symget('Bytes');
output;
%end;
%end;
%end;
run; /* Close local dataset filespec */
/* Closes the directory */
%let rc=%sysfunc(dclose(&did));
%mend drive;
%drive( <DIRECTORY>, xls )
proc print data = filespec;
title 'Created data set filespec';
run;
... View more