以下是用于将文件属性提取到日志的代码。
参考代码:http://support.sas.com/kb/40/934.html
%macro FileAttribs(filename);
%global rc fid fidc;
%global Bytes CreateDT ModifyDT;
%let rc=%sysfunc(filename(onefile,&filename));
%let fid=%sysfunc(fopen(&onefile));
%let Bytes=%sysfunc(finfo(&fid,File Size (bytes)));
%let CreateDT=%qsysfunc(finfo(&fid,Create Time));
%let ModifyDT=%qsysfunc(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;
data DSN ;
length CreateDT_ ModifyDT_ $200. ;
/*Path of the file along with the file extension*/
%FileAttribs ( C:\Derived\GRSL.log ) ;
/*Creation date of the file*/
CreateDT_ = "&CreateDT" ;
/*Modification date of the file*/
ModifyDT_ = "&ModifyDT" ;
run;
我将宏变量中的值复制到SAS变量中。宏变量保持 2016年8月19日09:55:09 ,并且不属于SAS9.2的可接受日期和时间格式。我想将CreateDT_和ModifyDT_转换为数字。我尝试通过手动转换搜索字符串与SUBSTR函数来做到这一点。有没有办法动态处理它而无需手动搜索字符串的日期月,年和时间。有没有办法控制文件属性格式,例如上面的程序在几次运行中返回 2017年3月1日05:22:30 o'时钟,其他几次日期 01 2017年3月5日05:22:30 。日期格式不断变化。
答案 0 :(得分:0)
您可以使用DATETIME18.
功能格式input()
:
CreateDT_num=input(CreateDT_,DATETIME18.);
ModifyDT_num=input(ModifyDT_,DATETIME18.);
这里是你自己的代码
data DSN ;
format CreateDT_num ModifyDT_num DATETIME18.;
length CreateDT_ ModifyDT_ $200. ;
/*Path of the file along with the file extension*/
%FileAttribs ( C:\Derived\GRSL.log ) ;
/*Creation date of the file*/
CreateDT_ = "&CreateDT" ;
/*Modification date of the file*/
ModifyDT_ = "&ModifyDT" ;
CreateDT_num=input(CreateDT_,DATETIME18.);
ModifyDT_num=input(ModifyDT_,DATETIME18.);
run;
如果您只想要日期,则可以使用
datepart()
答案 1 :(得分:0)
您可以将它们变为DateTime常量,或使用input
。这是两个例子。 "[datetime]"dt
是日期时间常量。
一个注意事项 - 虽然将宏放在您所在的位置是合法的,但最好将其置于数据步骤之外,因为将它放在放置它的地方有点令人困惑。它并没有真正运行数据步骤,它在datastep之前运行。
%macro FileAttribs(filename);
%global rc fid fidc;
%global Bytes CreateDT ModifyDT;
%let rc=%sysfunc(filename(onefile,&filename));
%let fid=%sysfunc(fopen(&onefile));
%let Bytes=%sysfunc(finfo(&fid,File Size (bytes)));
%let CreateDT=%qsysfunc(finfo(&fid,Create Time));
%let ModifyDT=%qsysfunc(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;
/*Path of the file along with the file extension*/
%FileAttribs ( c:\temp\test.txt ) ;
data DSN ;
createDT = input(symget("CreateDT"),datetime18.);
modifyDT = input(symget("modifyDT"),datetime18.);
*alternately;
createDT2 = "&createDT"dt;
put createDT datetime. createDT2 datetime.;
run;