I keep getting the following error: ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: 234982340.88/60
Here is the code below.
*Start SAS timer;
%let _sdtm = %sysfunc(datetime());
***LOTS OF CODE***;
*End SAS timer;
%let _edtm = %sysfunc(datetime());
*Show runtime;
%let _runtm = %eval(%sysfunc(putn(&_edtm - &_sdtm, 12.4))/60);
%put The code took &_runtm to work;
Below should do what you want.
%let _sdtm = %sysfunc(datetime());
data _null_;
call sleep(2,1);
run;
%let _edtm = %sysfunc(datetime());
%let _runtm = %sysfunc(sum(&_edtm,-&_sdtm),time14.3);
%put The code took &_runtm to work;
The %eval() function is for integers but you're dealing with strings that represent floating point numbers for which you need to use %sysevalf().
%let _runtm2 = %sysfunc(putn(%sysevalf(&_edtm - &_sdtm),time14.3));
%put The code took &_runtm2 to work;
Most of the time using %sysfunc() is preferable because it gives you access to SAS functions and also lets you define the output format within the same function call.
Your code returned an error because you tried to pack multiple operations (putn, subtraction) into a single %sysfunc() call. Additionally as the name %sysfunc() implies its for calling functions. If you want to subtract values you need to do it via a function call: %sysfunc(sum(var1,-var2))
Below should do what you want.
%let _sdtm = %sysfunc(datetime());
data _null_;
call sleep(2,1);
run;
%let _edtm = %sysfunc(datetime());
%let _runtm = %sysfunc(sum(&_edtm,-&_sdtm),time14.3);
%put The code took &_runtm to work;
The %eval() function is for integers but you're dealing with strings that represent floating point numbers for which you need to use %sysevalf().
%let _runtm2 = %sysfunc(putn(%sysevalf(&_edtm - &_sdtm),time14.3));
%put The code took &_runtm2 to work;
Most of the time using %sysfunc() is preferable because it gives you access to SAS functions and also lets you define the output format within the same function call.
Your code returned an error because you tried to pack multiple operations (putn, subtraction) into a single %sysfunc() call. Additionally as the name %sysfunc() implies its for calling functions. If you want to subtract values you need to do it via a function call: %sysfunc(sum(var1,-var2))
Thank you for your thoughtful explanation. I suspected I was dealing with a string vs. integer vs. float issue, but I am new to using SAS and couldn't figure out the syntax to make this work. Your response was supremely helpful. Cheers.
You need %SYSEVALF() to do floating point arithmetic in MACRO code. %EVAL() only does integer arithmetic.
But you don't need to call SUM() to do arithmetic (unless you want missing values to be treated as zero).
63 %let sdtm = "&sysdate9:&systime"dt; 64 %let edtm = %sysfunc(datetime()); 65 %let minutes = %sysevalf((&edtm - &sdtm)/60); 66 %put Code took &=minutes ; Code took MINUTES=813.0412833333
There is a good macro for timing already:
https://github.com/sasutils/macros/blob/master/bench.sas
%macro bench
/*----------------------------------------------------------------------
Measures elapsed time (in seconds) between sucessive invocations
----------------------------------------------------------------------*/
(mvar /* Macro variable used for recording start time (default=_bench)*/
);
/*----------------------------------------------------------------------
Call it once to start the timing and then a second time to report the
elapsed time and clear the saved time.
Use different values for MVAR to time multiple overlapping periods.
----------------------------------------------------------------------*/
%if (&mvar=) %then %let mvar=_bench;
%if ^%symexist(&mvar) %then %global &mvar;
%if (&&&mvar =) %then %let &mvar = %sysfunc(datetime());
%else %do;
%put NOTE: Elapsed seconds = %sysevalf(%sysfunc(datetime()) - &&&mvar);
%let &mvar =;
%end;
%mend bench;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.