BookmarkSubscribeRSS Feed
Babloo
Rhodochrosite | Level 12

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?

4 REPLIES 4
arodriguez
Lapis Lazuli | Level 10

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?

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Quentin
Super User

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;

 

BASUG is hosting free webinars Next up: Jane Eslinger presenting PROC REPORT and the ODS EXCEL destination on Mar 27 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
Astounding
PROC Star

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 1646 views
  • 1 like
  • 5 in conversation