When developing and debugging macro code, it's crucial to turn on the option MPRINT and review your log, so that you can see the SAS code that the macro generated. When I do that with your code, I get:
22 options mprint ;
23 %pickname(tb=test, cl=class, n1=name1, n2=name2);
MPRINT(PICKNAME): data test;
MPRINT(PICKNAME): set test;
MPRINT(PICKNAME): run;
You can see that there is no assignment statement. Why not? Because both %IF statements evaluate to false.
It looks like you are trying to evaluate the value of a data step variable, and conditionally execute a data step assignment statement. To do that, you can use the data step IF statement, not the macro %IF statement.
If you do that, your code will look like:
%macro pickname(tb=, cl=, n1=, n2=);
data &tb; set &tb;
if &cl="a" then do;
name3 = &n1;
end;
else if &cl="b" then do;
name3 = &n2;
end;
run;
%mend;
And when you run it, the log will show that the macro generated a data step which looks like what you want:
23 %pickname(tb=test, cl=class, n1=name1, n2=name2);
MPRINT(PICKNAME): data test;
MPRINT(PICKNAME): set test;
MPRINT(PICKNAME): if class="a" then do;
MPRINT(PICKNAME): name3 = name1;
MPRINT(PICKNAME): end;
MPRINT(PICKNAME): else if class="b" then do;
MPRINT(PICKNAME): name3 = name2;
MPRINT(PICKNAME): end;
MPRINT(PICKNAME): run;
As you work with the macro language, it's important to understand (and remember) the difference between %IF (a macro language statement for evaluating macro variable expressions) and IF (a data step statement for evaluating data step variable expressions), %DO vs DO, etc.
--Q.
... View more