SAS宏函数在linux上获取文件修改日期 [英] SAS macro function to get file modified date on linux

查看:61
本文介绍了SAS宏函数在linux上获取文件修改日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用宏函数将文件的修改日期作为 SAS 日期返回,该函数将在 Linux (SAS 9.3) 上运行.我想避免使用 OS 命令(例如,通过管道传输 LS 命令的结果),因为代码需要在使用 NOXCMD 的环境中工作.下面是使用 finfo() 的初稿(没有错误处理代码等).

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().

对 finfo() 返回的日期格式感到失望,例如Fri Apr 10 14:54:10 2015".然后更失望的是我无法在没有下面丑陋解析的情况下输入()这个字符串.过去我通常避免使用 ANYDTDTE 信息,因为担心它会做太多猜测并且不会抛出错误.但是编写自定义日期时间信息来处理这个字符串感觉有点过头了.

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.

希望能提出将日期字符串转换为 SAS 日期的更好方法、获取文件修改日期的更好方法以及以下任何陷阱的想法.

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;

推荐答案

至少在 Windows 上不会发生这种情况.我得到了一个不错的 SAS 日期时间.

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

添加一些调试:

%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)

返回

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

如果 Linux 确实按照您想要的方式工作,我不确定是否有更好的方法,不过,如果您已经开始努力手动解析它,则可以进行一些改进以避免 ANYDTDTE.

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.

例如:

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

这篇关于SAS宏函数在linux上获取文件修改日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆