BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
sas_user_1001
Obsidian | Level 7

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

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))

View solution in original post

3 REPLIES 3
Patrick
Opal | Level 21

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))

sas_user_1001
Obsidian | Level 7

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.

Tom
Super User Tom
Super User

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 609 views
  • 3 likes
  • 3 in conversation