BI Tools

Tips and tricks for building information maps, OLAP cubes, reports, and dashboards

BI Admin

Learn your way around a SAS BI installation.

Visual Analytics

Learn your way around the SAS Visual Analytics tool

Coding & Data

Extract, transform, and load your data into the SAS BI toolset

Stored Processes

Create and design stored processes like a rock star

Home » BI Tools, Coding & Data

Does your SAS code know what time it is? Add a timestamp.

Submitted by on 2012-12-20 – 6:12 AM 7 Comments

In 1986 the Beastie Boys released their classic debut album, License to Ill, and the last track posed a question: “What’s the time?”  The Beasties answered with an anthem, “It’s time to get ill.”  I think Peter Crawford, SAS programmer extraordinaire, would have answered more simply.  What time is it?  It’s %now.

%Now – a Timestamp Macro

%Now generates a timestamp.   It’s a simple SAS macro, but despite its simplicity, it has a lot to teach about macro design.  In fact, Peter wrote an entire paper about the macro, so I won’t repeat the lessons.

The macro is:

%MACRO now( fmt= DATETIME23.3 ) /DES= 'timestamp' ; 
  %SYSFUNC( DATETIME(), &fmt )
%MEND now ;

It’s a utility macro, and I have it in my autocall library, so that it’s always there when I want it.

I often use it in footnotes. This code snippet:

Will produce a footnote with a timestamp in it:

 

I also add timestamps to file names.  Here, I add a timestamp to the log file from a nightly job.  The default format (DATETIME23.3) is fine for a footnote.  For file names, I typically format the timestamp as YYYYMMDDThhmmss, which maintains readability and also sorts in chronological order.

 

 

I’ve written before about macro utilities, and about minimalist programming, and %Now combines both of those interests.   %Now has a special place in my heart, since it is the first one-liner I learned, and it helped me realize the purpose of the macro language.  Other useful one-liners I’ve seen include %isblank and %age.

Learn all about SAS BI

You can learn more about SAS Business Intelligence from the "SAS BI Bible." Take a peek inside the Building Business Intelligience with SAS book.

 

The following two tabs change content below.

Quentin McMullen

Quentin McMullen has been programming in SAS for 15 years, and for the past year has been working on SAS BI projects. He has presented at national and regional SAS user group conferences, and can often be found corresponding with colleagues on SAS-L.

Tags:

7 Comments »

  • Quentin McMullen says:

    Thanks Andy. I agree, you could just write the %SYSFUNC(..) in the title statement (or anywhere else), so in some sense the macro is unnecessary. But only in the same way that the whole macro language is unnecessary. That is, you can always decide to type SAS code rather than write a macro that will generate SAS code for you. To me, the purpose of many macros is to decrease typing (and increase modularization/readability/reusability).

    It’s much easier for me to type “Produced at %now()” than “Produced at %SYSFUNC( DATETIME(),datetime23.3)” . And I find it more readable as well.

    Thanks for mentioning the DTRESET option.
    –Q.

  • I think the macro can be used in a lot of places and is useful. In particular I use to run a batch production job where this macro would have saved some coding and kept things consistent.

  • Quentin says:

    Thanks for sharing Don,
    That’s a nifty one!
    –Q.

  • Here is one of my favorite one liners (well, one line excluding the comments). I use macros a lot in SAS Server Pages and so I use the %sysfunc macro with the SAS data set access functions to get data. Getting the value of a specific variable for a given row can be tedious. So I wrote two macros to echo the getvarc and getvarn SCL functions. Here is the %getvarc macro (%getvarn is same – just change the C to an N):

    %macro getvarc
    (dsid, /* dsid for the data set to be queried */
    name, /* name of character variable whose value is to be retrieved */
    format /* optional format for the returned value */
    );

    /*——————————————————————————————
    Copyright (c) 2010 Henderson Consulting Services
    PROGRAMMER : Don Henderson
    PURPOSE : Obtains the value of a specified variable. It is meant to serve as an
    analog to the getvarc function which requires the second argument to be
    a variable number. This macro is for the case where the variable name is
    specified.

    The getvarn macro performs a similar function for numeric variables.

    Note that position parms are used so the macro call looks similar to the
    function call.
    |——————————————————————————————|
    | MAINTENANCE HISTORY |
    |——————————————————————————————|
    | DATE | BY | DESCRIPTION |
    |———-|———–|——————————————————————-|
    | Dec 2010 | Don H | Initial Creation
    |—————————————————————————————–*/

    %if &dsid gt 0 %then %sysfunc(getvarc(&dsid,%sysfunc(varnum(&dsid,&name))),&format);

    %mend getvarc;

  • Andrew Karp says:

    This is an interesting approach to the “timestamp” problem, but you can execute %sysfunc from within the TITLE or FOOTNOTE statement, which I think makes the Macro unnecessary.

    Also, check out the new to SAS 9 DTRESET (date time reset) System Option.

  • After I read this tip – I was like – Why didn’t I think of that? 🙂

  • Seshu says:

    good one..