Try to get the file attributes (file size, create date time and last date modification date) in SAS

advertisements

I'm using following macro to get Linux file attributes using SAS. I'm getting values for size and Last modified time but not getting any values for "Create Date Time".

%macro FileAttribs(filename);
  %local rc fid fidc;
  %local Bytes 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 &filename is &Bytes bytes;
    %put NOTE: Created &CreateDT;
    %put NOTE: Last modified &ModifyDT;
%mend FileAttribs;

%FileAttribs(/path/test.csv);

I couldn't figure what I'm missing. Are there any other file attributes that we can get other than size, create and modified dates?

Thanks, Sampath.


According to this answer, many Linux systems don't store file creation date (or if they store it, it's not accessible with a standard name): https://unix.stackexchange.com/questions/91197/how-to-find-creation-date-of-file .

If you want to get the value of all file attributes available via the FINFO(), you can use FOPTNUM() to find the number of attributes available, and then loop over them. Here is a macro:

%macro GetAllAttributes(file);

  %local rc fref fid i AttributeName AttributeValue;

  %let rc=%sysfunc(filename(fref,&file));
  %let fid=%sysfunc(fopen(&fref));

  %do i=1 %to %sysfunc(foptnum(&fid));
    %let AttributeName=%sysfunc(foptname(&fid,&i));
    %let AttributeValue=%sysfunc(finfo(&fid,&AttributeName));
    %put &AttributeName : &AttributeValue;
  %end;

  %let fid=%sysfunc(fclose(&fid));
  %let rc=%sysfunc(filename(fref));

%mend GetAllAttributes;

On Windows (SAS 9.3) I get:

78   %GetAllAttributes(d:\junk\somefile.txt)
Filename : d:\junk\somefile.txt
RECFM : V
LRECL : 256
File Size (bytes) : 1011
Last Modified : 06Dec2013:14:14:54
Create Time : 06Dec2013:14:14:52

On Linux (SAS 9.3) I get:

41         %GetAllAttributes(~/somefile.txt)
Filename : /home/Quentin/somefile.txt
Owner Name : Quentin
Group Name : somegroup
Access Permission : rwx------
Last Modified : Fri Dec  6 14:14:54 2013
File Size (bytes) : 1011

Lastly, note that SAS 9.3 on Linux returns the modified date in an ugly format. Tech support told me that 9.4 returns a SAS-friendly date-time format like Windows does. If you're on 9.3, see this question for advice on parsing the date: SAS macro function to get file modified date on linux.