Wednesday, April 10, 2013

Timestamp

Image and video hosting by TinyPic
"What's the time?"
Certainly it's very important for everyone to keep track of the time.
For SAS programmer, sometimes it's also important to label the files or certain place (like footnote or title) with "timestamp".
Here comes a simple SAS macro to perform this function:
%now( ) 
%MACRO now( fmt= DATETIME23.3 ) /DES= 'timestamp'; 
 %sysfunc( compress( %SYSFUNC( DATETIME(), &fmt))  )
%MEND;
It's a utility macro. You can put it in the autocall library whenever you need it.

Example: 
Here I put it in the title and file name for the data set "eg".
ods listing close; 
options nonumber nodate; 
ods pdf file = "C:\Bancova\eg_%now(fmt=b8601dt).pdf"; 
title "eg_%now(fmt=b8601dt)"; 
proc print data = eg; 
run; 
ods pdf close; 
ods listing; 
You will see the result:
Image and video hosting by TinyPic
For file names, the ISO 8601 format YYYYMMDDThhmmss maintains readability and also sorts in chronological order.
Image and video hosting by TinyPic

Remember: 
Although it is very short, the application of this base SAS macro requires knowledge of the following:
  • macro basics (know how to write a macro)
  • format (know how to define a date format)
  • %sysfunc (one of the most important tools to the macro laguage since the %let statement)
  • ISO 8601 standard (It's compliant with CDISC, FYI)
  • ODS pdf (This destination is part of the ODS PRINTER family) 

Simple Quiz: 
  • Why do we need compress inside the %now( ) ? 
References:
Does your SAS code know what time it is? Add a timestamp. 

No comments:

Post a Comment