1
votes

Working on a macro function to return the modified date of a file as a SAS date, that will run on Linux (SAS 9.3). I want to avoid using OS commands (e.g. piping the results of an LS command) as code needs to work in an environment with NOXCMD. Below is a first draft (no error handling code, etc), using finfo().

Was disappointed by the format of the date returned by finfo(), e.g."Fri Apr 10 14:54:10 2015". Then was more disappointed by my inability to input() this string without the ugly parsing below. I have generally avoided using ANYDTDTE informat in the past, out of fear that it does too much guessing and doesn't throw errors. But it feels like overkill to write a custom date-time informat to handle this string.

Would appreciate thoughts on better ways to convert the date string to a SAS date, better ways to get the file modified date, and any pitfalls to below.

%macro GetModDate(file);
  %*Get the modified date of a linux file, as SAS date;
  %local rc fref fid ModDate;

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

  %let ModDate=%sysfunc(finfo(&fid,Last Modified));

  %*Linux Last Modified returns format like: Fri Apr 10 14:54:10 2015;
  %let ModDate=%sysfunc(inputn(%scan(&moddate,2,%str( )) %scan(&moddate,3,%str( )) %scan(&moddate,5,%str( ))
                              ,anydtdte11
                               ));
  %let fid=%sysfunc(fclose(&fid));
  %let rc=%sysfunc(filename(fref));

  &ModDate
%mend GetModDate;
1
While I think this is fine here - technically speaking if this is "finished code" that you want help improving it belongs on Code Review. I don't actually post over there and I don't know if any SAS users do - so I tend to be fine with them here - just noting.Joe
Thanks for the comment, Joe. As I wrote it, I was thinking this might be off-topic for SO, since I couldn't figure out how to make it into a question that would have a correct answer. Will keep this in mind for future posts.Quentin
just couldn't understand what was in that discussion. a question? an answer? asking for suggestion?ZeekDSA

1 Answers

1
votes

That doesn't happen on Windows, at least. I get a nice SAS datetime.

Adding some debugging:

%macro GetModDate(file);
  %*Get the modified date of a linux file, as SAS date;
  %local rc fref fid ModDate;

  %let rc=%sysfunc(filename(fref,&file));
  %put &=rc;
  %let fid=%sysfunc(fopen(&fref));
  %put &=fid;
  %let ModDate=%sysfunc(finfo(&fid,Last Modified));
  %put &=ModDate;
  %*Linux Last Modified returns format like: Fri Apr 10 14:54:10 2015;
  %let ModDate=%sysfunc(inputn(%scan(&moddate,2,%str( )) %scan(&moddate,3,%str( )) %scan(&moddate,5,%str( ))
                              ,anydtdte11
                               ));
  %let fid=%sysfunc(fclose(&fid));
  %let rc=%sysfunc(filename(fref));

  &ModDate
%mend GetModDate;

%getModDate(c:\temp\test.html)

returns

RC=0
FID=2
MODDATE=19Mar2015:10:19:09

I'm not sure there's a better way if Linux does work the way you want, though, though you could make some improvement to avoid ANYDTDTE if you're already going to the effort of parsing it out some by hand.

For example:

%let ModDate=
    %sysfunc(inputn(
        %scan(&moddate,3,%str( ))%scan(&moddate,2,%str( ))%scan(&moddate,5,%str( )),
        date9.)
     );