Retrieve file size or last modified date of an external file

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

Subhro provides valuable and informative content on SAS, offering a comprehensive understanding of SAS concepts. We have been creating SAS tutorials since 2019, and 9to5sas has become one of the leading free SAS resources available on the internet.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This Post Has 2 Comments

  1. VanessaInick

    Very nice articles and well explained.

  2. MariaWep

    Very useful!!. Thanks