DATA Step, Macro, Functions and more

How to add numbers using %eval?

Reply
Super Contributor
Posts: 426

How to add numbers using %eval?

I've a code like below,

 

if %eval(&end_title)+%eval(&end_title1<_n_<%eval(&end_title)+%eval(&end_title1)+%eval(&end_title2) then do; put option;end

 

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: 1558<_n_<1558+1558+1558

 

Could someone tell me how to sum  using %eval?

Frequent Contributor
Posts: 144

Re: How to add numbers using %eval?

Why do you need to add so many evals? the logic function should work on the same way without it. Even though, if you want to add the %eval function, if macro variables END_TITLE, END_TITLE1 and END_TITLE2 are numeric should work.

 

if %eval(&end_title +&end_title1) < _n_ < %eval(&end_title+&end_title1+&end_title2) then do; put option;end

In words, what do you want to do with this condition?

 

Super User
Super User
Posts: 7,401

Re: How to add numbers using %eval?

Your mixing boolean logic &end_title1 < _n_ < ..., with numerical operations %eval().  Again, as per several other posts, why are you trying to do all this in macro language.  Your clearly trying to force programming through macro.  Step back and just write base SAS code to do what you want.  If you see repeats of code, then put that in a macro, but don't try to program everything in macro code its crazy.

PROC Star
Posts: 1,231

Re: How to add numbers using %eval?

As others have mentioned, your code is unusual.  It is rare to use the %EVAL macro function on a data step IF statement.  %EVAL is for doing integer arithmetic in the macro language. The DATA step language does not need it. 

 

That said, I'm surprised you are getting an error message.  The below sample code "works".  It is possible you have some unprintable characters in your actual example, or even decimal points.  Second DATA step shows that %EVAL is not needed in this setting.

 

%let end_title=1;
%let end_title1=2;
%let end_title2=3;


data _null_;
  set sashelp.class;
  if %eval(&end_title +&end_title1) < _n_ < %eval(&end_title+&end_title1+&end_title2) then do;
    put _n_=;
  end;
run;


data _null_;
  set sashelp.class;
  if (&end_title +&end_title1) < _n_ < (&end_title+&end_title1+&end_title2) then do;
    put _n_=;
  end;
run;

 

Super User
Posts: 5,083

Re: How to add numbers using %eval?

You need to show more of the context of your program.

 

If you are working in a DATA step, all the %EVALs can go.  The DATA step never uses them.

 

If you are not working in a DATA step, _N_ does not exist.

 

In either case, adding parentheses might help clarify your intention.

Ask a Question
Discussion stats
  • 4 replies
  • 238 views
  • 1 like
  • 5 in conversation