Skip to content
9to5sas
  • Index
  • Glossary
Facebook Twitter Instagram Email RSS

  • Start Here
  • Base SAS
  • Advanced SASExpand
    • SAS Macros
    • PROC SQL
  • SAS/STATSExpand
    • SAS Analytics
    • Statistics
  • SAS Programs
9to5sas

9to5sas » SAS PROGRAMS » Retrieve file size or last modified date of an external file in SAS

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

BySubhro Posted onFebruary 16, 2020March 26, 2022 Last Updated:March 26, 2022
2 Comments

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.

Retrieve file size or last modified date of an external file

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!

Check your inbox or spam folder to confirm your subscription.

Post Tags: #Working with files
Subhro

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.

Facebook Twitter Linkedin

Post navigation

Previous Previous
7 Table lookup techniques for SAS Programmers
NextContinue
Business Analytics Definition and Overview

SAS Tips in your inbox

Subscribe to 9to5sas for timely SAS tips and news delivered each month.
Learn about the latest articles, and code samples to keep your SAS skills fresh!

Your subscription is successful!

Recent Posts

  • Concatenate strings in SAS: The CAT Functions Demystified
  • 5 Techniques for Quickly Removing Leading Zeros in SAS
  • Troubleshoot Your Proc SQL Code Like a Pro with These 7 Automatic Macro Variables
  • 7 PROC SQL Options You Should Use to Debug Queries
  • How To Use The SAS Proc SQL Order By Statement?
9to5sas
  • Privacy Policy
  • Disclaimer
  • About
  • Contact
Facebook Twitter Instagram RSS Email Pinterest

Copyright © 2023 9TO5SAS, All rights reserved.

Scroll to top
  • 9to5sas Blueprint
  • About
  • About
  • Acceptable use policy
  • calculator
  • Confirm email
  • Contact
  • Contact
  • Cookie Policy
  • DISCLAIMER
  • Getting Started with SAS
  • Glossary
  • Index
  • Post #13801
  • Privacy Policy
  • Privacy policy
  • SAS Programs
  • Styles
  • Subscription confirmed
  • Terms and conditions
  • Thank You