BookmarkSubscribeRSS Feed
SASPhile
Quartz | Level 8

two columns colA and Colb are present in dataset have and both are numeric. When I use the following I'm getting Error "A Character operand was found in %eval function.

 

%let var1=%eval((colA+colB)*colB));

 

data test;

  set have;

va1=&var1.;

run;

3 REPLIES 3
Reeza
Super User

EVAL only works with integers.

 

But since you're just passing the arguments you don't need any of that.

 

%let var1=(weight+height)*height;

 

data test;

  set sashelp.class;

va1=&var1.;

run;
ballardw
Super User

@SASPhile wrote:

two columns colA and Colb are present in dataset have and both are numeric. When I use the following I'm getting Error "A Character operand was found in %eval function.

 

%let var1=%eval((colA+colB)*colB));

 

data test;

  set have;

va1=&var1.;

run;


Macro functions generally do not reference values of data step variables.

If you want a statement to appear as data step code:

%let var1 = ((colA+colB)*colB);

%eval was attempting to add the text values colA, colB and multiply by the colB.

 

If at some point you are making the code generated dynamic then you would use

%let var1 = ((&colA+&colB)*&colB);

where the macro variable &colA and &colB resolve to names of data step variables.

 

Macro code such as this is only evaluated during the compile phase of the data step to generate code lines. Not at the execution of each loop through the data set.

Kurt_Bremser
Super User

@SASPhile wrote:

two columns colA and Colb are present in dataset have and both are numeric. When I use the following I'm getting Error "A Character operand was found in %eval function.

 

%let var1=%eval((colA+colB)*colB));

 

data test;

  set have;

va1=&var1.;

run;


Once again, catastrophic misunderstanding of macro timing. Lots of insights into the force of macro to gain you need, young padawan, before use it you can without harm.

 

The macro PREprocessor sees only text. The text (colA+colB)*colB is nothing numeric that can be evaluated, so the %eval (as it happens immediately in the %let) will fail. I also fail to see the wisdom of packing data step logic into a macro variable. This will only reduce the maintainability of your code.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 3 replies
  • 369 views
  • 0 likes
  • 4 in conversation