0

I cannot find the solution to this error. I tried usign %eval, %sysfunc, and %sysevalf, with no success. What is needed to correctly evaluate the "&set" in the macro?

%macro set_istituzionale(set=, varout=);
  %if &set/100 = 1 %then &varout = 'AP';
%mend set_istituzionale;

data soff2; set soff; %set_istituzionale(set=setcon,varout=set); run;

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was:
       &set/100 = 1
ERROR: The macro SET_ISTITUZIONALE will stop executing.
Giuseppe
  • 518
  • 10
  • 22

1 Answers1

1

The logic behind you're macro is wrong.

You are calling that macro inside a datastep, so that macro should contain something (a statement) that could be resolved inside the datastep.

If-else won't be written in macro language but in normal datastep language.

%macro set_istituzionale(set=, varout=);
  if &set/100 = 1 then &varout = 'AP';
%mend set_istituzionale;

now if you use your call you will have your datastep resolved this way:

data soff2; 
set soff;
%set_istituzionale(set=setcon,varout=set);
run;

will became:

data soff2; 
set soff;
if setcon/100 = 1 then set = 'AP';
run;

On your code you were using macro code so your step were resolved internally to the macro boolean statement %if &set/100 resolved were %if setcon/100 where setcon was a string (here we are conditionally speaking with macro language, writing the name of the variable won't catch the value of the variable 'cause it is completelly independent from the datastep).

You should think to macro language only to something that could write down code for you, for example something like your first try could be used to insert a statement conditionally to the value of a macro variable like:

data try;
set want;

%if &macrovar=A %then %do;
if var1=1 and var2='B' then var3='C';
%end;

%if &macrovar=B %then %do 
if var1=2 and var2='A' then var3='D';
%end;

run;

when the macro variable macrovar will be = A the step will be:

data try;
set want;
if var1=1 and var2='B' then var3='C';
run;

if the macro variable macrovar will be = B the step will be:

data try;
set want;
if var1=2 and var2='A' then var3='D';
run;

But you can use macro code outside from the datastep too, for example to execute a datastep or something else conditionally to the value of a macro variable:

%if &macrovar=A %then %do;

data try; set tr;
some code;
run;

%end;

%else %if &macrovar=B %then %do;

proc sort data=tr out=try nodupkey; by var1; run;

%end;
stat
  • 699
  • 3
  • 10
  • unfortunately your suggestion doesn't work. I get a similar error: ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: setcon/100 ERROR: The macro SET_ISTITUZIONALE will stop executing – Giuseppe Oct 02 '15 at 12:44
  • this time macro variable set has value setcon, so you are trying to divide "setcon" string to a number... Please post the complete code including macro call. – stat Oct 02 '15 at 12:45
  • No, setcon is the name of the variable (it is a classification number), and it is numeric (checked that is not a number registered as character). – Giuseppe Oct 02 '15 at 12:47
  • ok, now that we have the macro call I can say that what is wrong it the logic behind the macro you are writing, I'm going to update the answer – stat Oct 02 '15 at 12:49
  • I think I understood the answer - I am not using the right tool. My attempt was to create a "function" to make that operation (map a certain variable to another), store it, and be able to call it from any of the codes in which I need it wothout having to write down that data step every time (obviously I reported a single line of that data step, but it is much longer). Any idea how I could do it (keeping the possibility to choose the name of the variables that I put in/out)? – Giuseppe Oct 02 '15 at 13:12
  • 1
    That's what I wrote at the beginning of the answer, enclosing into the macro one datastep statement. (first code box) – stat Oct 02 '15 at 13:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/91179/discussion-between-stat-and-giuseppe). – stat Oct 02 '15 at 13:18