1
votes

Below is the code which is used to pull the file attributes to the log.

Reference code: 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;

I am copying the values in the macro variables into SAS variables. The macro variables hold the 19 August 2016 09:55:09 and does not fall into the acceptable date and time formats of SAS9.2. I want to convert the CreateDT_ and ModifyDT_ to numeric. I tried doing it by manually convert search the string with SUBSTR function. Is there a way to handle it dynamically without manually searching the string for date month, year and time. Is there a way to control the file attribute formats, for example the above program returns 01 March 2017 05:22:30 o'clock during few runs and few other times the date01 MAR 2017 05:22:30. The date format keeps changing.

2
Edited my post. &root was just a macro variable I was using for the file path.Nimit_ZZ

2 Answers

0
votes

You can make them into DateTime constants, or use input. Here are two examples. "[datetime]"dt is a datetime constant.

One note - while it's legal to place the macro where you do, it's better to put it outside of the data step as it's a bit confusing to put it where you put it. It doesn't really run with the data step, it runs before the datastep does.

%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;
0
votes

You can use the format DATETIME18. with the input() function :

CreateDT_num=input(CreateDT_,DATETIME18.);
ModifyDT_num=input(ModifyDT_,DATETIME18.);

here in your own code

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;

and if you want only the date you can then use datepart()