Retrieve file size or last modified date of an external file in SAS

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;
last modified date of an external file
last modified date of an external file

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;
Filename, File Path, File Size and Last Modified date of an external file in SAS
Filename, File Path, File Size and Last Modified date of an external file in SAS

Every week we'll send you SAS tips and in-depth tutorials

JOIN OUR COMMUNITY OF SAS Programmers!

Subhro Kar is an Analyst with over five years of experience. As a programmer specializing in SAS (Statistical Analysis System), Subhro also offers tutorials and guides on how to approach the coding language. His website, 9to5sas, offers students and new programmers useful easy-to-grasp resources to help them understand the fundamentals of SAS. Through this website, he shares his passion for programming while giving back to up-and-coming programmers in the field. Subhro’s mission is to offer quality tips, tricks, and lessons that give SAS beginners the skills they need to succeed.