2017-02-15 18 views
1

下面是用於將文件屬性拉入日誌的代碼。要從字符轉換爲數字的文件屬性值

參考代碼: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變量。宏變量保存的是不可接受的日期和時間格式SAS9.2。我想將CreateDT_和ModifyDT_轉換爲數字。我試圖通過手動轉換搜索與SUBSTR函數的字符串。有沒有辦法動態處理它,而無需手動搜索日期月份,年份和時間的字符串。有沒有辦法控制文件屬性格式,例如上面的程序返回2017年3月01日05:22:30點在幾次運行和其他幾次日期01 MAR 2017 05:22:30。日期格式不斷變化。

+0

編輯我的文章。 &root只是我用於文件路徑的一個宏變量。 –

回答

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

+0

14不夠長,你需要18. – Joe

+0

@Joe謝謝!我沒有看到 –

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; 
相關問題