As the first step in the decommissioning of the site has been converted to read-only mode.

Here are some tips for How to share your SAS knowledge with your professional network.

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
%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"
 /*store source /* */  
 /*store & compile must have options mStored SASmStore=libref*/
;/*** ---------------------------------- ---------------------------- **
RJF2 02Nov22
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
      %sysfunc(putn(%eval(   &NOW. - &ACTION.),time8.0))       
> 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;                  
%else %if &Action. eq START %then %do;
      %sysfunc(datetime()         )
%else %if &Action. eq TIME  %then %do;
%else %do;
      %sysfunc(putn(%sysevalf(%sysfunc(datetime()         )
                              - &Action.),&Fmt_Time.))

Test Data

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




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