Macro Clock

From sasCommunity
Jump to: navigation, search

Q: How do I write time-elapsed notes in my log?

A: Use Macro Clock as a stop-watch.

Author: Ronald_J._Fehd

Macro Clock

/*CLOCK macro function returns time or elapsed time
                        input  : action: help start time
                                 mVar containing Start-Time
                        process: according to Action
                                 or substract Start-Time from End-Time
                        output : returns date- and time-stamp
                                 according to format
usage:
%Clock();
%Put Started:%Clock(Time);
%Let Time0 = %Clock(Start);
%Let Time1 = %Clock(Start);
*procedures here;
%Put Time1 = %Clock(&Time1.) Of Proc;
%Put Time0 = %Clock(&Time0.) Of Job;
%Put Ended@@: %Clock(Time);
*** .................................... ............................ */
%MACRO Clock
      (Action     /* in (? HELP START TIME &MVAR.)                    */
      ,PrevTime = /*mVar of previous usage: Start-Time                */
      ,Fmt_Time =     time12.3
      ,Fmt_Date = datetime22.3
)/des = "site: function returns time or elapsed time"
        /*SASmacro.catalog.DEScription*/
 /*store source /* */  
 /*store & compile must have options mStored SASmStore=libref*/
;/*** ---------------------------------- ---------------------------- **
RJF2 02Nov22
1234567890123456789012
04DEC2002:14:19:16.05
RJF2 02Dec04 polishing NOTE: passing value, not reference
RJF2 03Oct17 polishing added upcase
> From: Pete Lund 
> Sent: Friday, November 22, 2002 10:50 AM
> Subject: RE: tip macro function CLOCK
> If you change your %eval to %sysevalf you can get the
> fractional seconds - assuming you also take out the
> recalculation of &now and change the output format to time12.2.
no need to substring NOW
%LET NOW = %substr(   &NOW.,1
                   ,%index(&NOW.,.)-1);
%ELSE %DO;
      %sysfunc(putn(%eval(   &NOW. - &ACTION.),time8.0))       
      %END;
> From: Peter Crawford 
uses range function: note different FMT_TIME.
%let   p = %sysfunc( range( "&n"dt, "&&&from"dt ), time11.2 );
RJF2 04Mar15 polishing documentation for repost to SAS-L
*** .................................... ........................... **/
%Let Action = %upcase(&Action);
%if &Action. eq
 or &Action. eq ?
 or &Action. eq HELP  %then %do;
    ;
    %Put Clock Usage:;
    %Put %nrstr(*Put Start:*Clock(Time ););
    %Put %nrstr(*Let Mvar =*Clock(Start););
    %Put %nrstr(*Put Used :*Clock(&Mvar););
    %Put %nrstr(*Put End  :*Clock(Time ););
    %Goto Exit;                  
    %end;
%else %if &Action. eq START %then %do;
      %sysfunc(datetime()         )
      %end;
%else %if &Action. eq TIME  %then %do;
      %sysfunc(datetime(),&Fmt_Date.)
      %end;
%else %do;
      %sysfunc(putn(%sysevalf(%sysfunc(datetime()         )
                              - &Action.),&Fmt_Time.))
      %end;
%EXIT:
%mend;

Test Data

 /*execTest ************************ to enable end this line w/slash **
run;
%Macro Sometime(N);
DATA _Null_;
do I = 1 to 1E&N.;
   end;
stop;
run;
%mend;
%Clock();
%Put Started:%Clock(Time);
%Let Time0 = %Clock(Start);
%Let Time1 = %Clock(Start);
%Sometime(7);
%Put Time1 = %Clock(&Time1.) Of Proc;
%Put Time0 = %Clock(&Time0.) Of Job;
%Put Ended@: %Clock(Time);
run; /** ******************************* ********** end test of Clock */

Log:

*todo;

References


--Ronald_J._Fehd macro.maven == the radical programmer 08:30, 6 August 2012 (EDT)