SAS_learner
SAS_learner

Reputation: 521

Trying to get file attributes (file size , create date time and last modified date time) in SAS

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.

Upvotes: 4

Views: 4332

Answers (2)

Quentin
Quentin

Reputation: 6378

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.

Upvotes: 2

Joe
Joe

Reputation: 63424

For a list of what's available, you can see the SAS Documentation for FINFO in Unix. That isn't updated for the additions you note above, which are listed here, but they should work.

Your macro retrieves all three information bits for me, in Windows, though that's of course possibly different from Linux/Unix. The Windows documentation does list the three new items - so I wonder if there was an issue with them in Unix/Linux and they aren't fully supported in 9.3. (They're still not listed in the 9.4 docs, either, if that's relevant.)

Upvotes: 0

Related Questions