SAS macro function to get file modification date on linux

Working with the macro function to return the changed file date as a SAS date that will work on Linux (SAS 9.3). I want to avoid using OS commands (for example, passing LS command results), since the code should work in an environment with NOXCMD. Below is the first draft (without error handling code, etc.) Using finfo ().

I was disappointed with the date format returned by finfo (), for example, "Fri. April 10, 14:54:10 2015." Then it was more disappointed with my inability to enter () this line without the ugly parsing below. I generally avoided using ANYDTDTE informat in the past, out of fear that he guesses too much and doesn't throw mistakes. But in order to process this line, it seems to you that the superfluous will write special date and time information.

I would read thoughts about the best ways to convert a date string to a SAS date, the best ways to get the file modification date and any errors 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
source share
1 answer

This does not happen on Windows at least. I get a good time and SAS time.

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

, , Linux , , , ANYDTDTE, .

:

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

Source: https://habr.com/ru/post/1620820/


All Articles