%let eval

Reply
Super Contributor
Posts: 713

%let eval

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;

Super User
Posts: 23,700

Re: %let eval

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;
Super User
Posts: 13,523

Re: %let eval


@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.

Super User
Posts: 10,229

Re: %let eval


@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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Ask a Question
Discussion stats
  • 3 replies
  • 93 views
  • 0 likes
  • 4 in conversation