Hello
I am using macro with condition.
In second run of the macro I get an error
31 Run=2);
ERROR: Required operator not found in expression: &condition ^=
ERROR: The macro mmacro will stop executing.
May anyone help please to solve it and understand the error
%macro mmacro(Field,factor,condition,Run);
data RawData&Run.(where=(hativa_char ne 'Other'
and CC>5000));
set tbl1;
Simulation=max(min(&Field.*&factor.,CC),5000);
%if &condition ^= %then &condition;
Run;
%mend mmacro;
/*Run1-Have no error*/
%mmacro(Field=niz_crd_max,
factor=1.2,
condition=,
Run=1);
/*Run2-Have error*/
%mmacro(Field=niz_crd_max,
factor=1.2,
condition=%str(IF Simulation>50000
AND abs(Simulation-CC)/CC>0.5
then Simulation=0.5*CC;),
Run=2);
Please try to always provide sample data so we can actually run your code. Providing sample data also allows to post desired results.
I believe some quoting around macro variable &condition will resolve the issue.
data tbl1;
cc=6000;
hativa_char=' ';
niz_crd_max=1;
output;
niz_crd_max=2;
output;
stop;
run;
%macro mmacro(Field,factor,condition,Run);
data RawData&Run.;
set tbl1;
if hativa_char = 'Other' or CC>5000 then delete;
Simulation=max(min(&Field.*&factor.,CC),5000);
%if %nrbquote(&condition) ne %nrbquote() %then
&condition;
Run;
%mend mmacro;
options mprint;
/*Run1-Have no error*/
%mmacro(Field=niz_crd_max,
factor=1.2,
condition=,
Run=1);
/*Run2-Have error*/
%mmacro(Field=niz_crd_max,
factor=1.2,
condition=%str(IF Simulation>50000
AND abs(Simulation-CC)/CC>0.5
then Simulation=0.5*CC;),
Run=2);
Please note that in your condition in case CC is <=0.5 you'll end up with a division by zero - which will result in SAS throwing an error.
-> if condition CC>0.5 is False then the returned value is 0
Eventually consider using the DIVIDE() function documented here.
There are several ways to check for a null value in a macro variable. There is an entire paper on the subject (Chung & King, I believe). This is my preference:
%if %length(&condition) %then &condition;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.