Retrieve file size or last modified date of an external file – The FINFO functions return the value of a file specified. You can get six attributes named ‘info items’ through the use of the FINFO function.
filename fileref "/home/9to5sas/dsn/sales.sas7bdat";
data a(drop=fid);
infile fileref truncover obs=1;
fid=fopen('fileref');
Bytes=finfo(fid, 'File Size (bytes)');
crdate=finfo(fid, 'Create Time');
moddate=finfo(fid, 'Last Modified');
run;
Macro Technique Retrieve file size or last modified date of an external file
%macro FileAttribs(filename);
%local rc fid fidc;
%local Bytes CreateDT ModifyDT;
%let rc=%sysfunc(filename(file, &filename));
%let fid=%sysfunc(fopen(&file));
%let Bytes=%sysfunc(finfo(&fid, File Size (bytes)));
%let CreateDT=%qsysfunc(finfo(&fid, Create Time));
%let ModifyDT=%qsysfunc(finfo(&fid, Last Modified));
%let fidc=%sysfunc(fclose(&fid));
%let rc=%sysfunc(filename(file));
%put NOTE: File size of &filename is &Bytes bytes;
%put NOTE- Created &CreateDT;
%put NOTE- Last modified &ModifyDT;
%mend FileAttribs;
/** Just pass in the path and file name **/
%FileAttribs(/home/sasExamples/demo.sas)
Here is the output of the below code.
NOTE: File size of /home/subhroster20070/choosec.sas is 429 bytes
Created
Last modified 01 February 2021 11:23:23
The DOPEN function is similar to the FOPEN function and it returns a directory identifier that allows the directory to be opened for input.
The DNUM function counts the number of files within the opened location. You can use this to loop through all the files in a location.
data fileinfo (keep=filename1 filepath Bytes moddate);
filename files '/home/subhroster20070/examples';
dirid=dopen('files');
countfiles=dnum(dirid);
do i=1 to countfiles;
filename1=dread(dirid, i);
filepath='/home/subhroster20070/examples/'||filename1;
sysrc=filename('fnames'||left(i), filepath);
exist=pathname('fnames'||left(i));
fid=fopen('fnames'||left(i));
Bytes=finfo(fid, 'File Size (bytes)');
moddate=finfo(fid, 'Last Modified');
sysrc=filename('fnames');
output;
sysrc=close(fid);
end;
rc=dclose(dirid);
run;
Very nice articles and well explained.
Very useful!!. Thanks