I am trying to use a macro program to dynamically create the WHEN clauses
in a DATA Step SELECT Statement. The code I am trying to create looks like
this:
data whatever;
set something;
select (cat);
when ("G") do;
vg = 1;
select;
when (decile = '0') vg0 = 1;
when (decile = '1') vg1 = 1;
when (decile = '2') vg2 = 1;
when (decile = '3') vg3 = 1;
when (decile = '4') vg4 = 1;
when (decile = '5') vg5 = 1;
when (decile = '6') vg6 = 1;
when (decile = '7') vg7 = 1;
when (decile = '8') vg8 = 1;
when (decile = '9') vg9 = 1;
end;
end;
when ("H") do;
vh = 1;
select;
when (decile = '0') vh0 = 1;
when (decile = '1') vh1 = 1;
when (decile = '2') vh2 = 1;
when (decile = '3') vh3 = 1;
when (decile = '4') vh4 = 1;
when (decile = '5') vh5 = 1;
when (decile = '6') vh6 = 1;
when (decile = '7') vh7 = 1;
when (decile = '8') vh8 = 1;
when (decile = '9') vh9 = 1;
end;
end;
when ("I") do;
vi = 1;
select;
when (decile = '0') vi0 = 1;
when (decile = '1') vi1 = 1;
when (decile = '2') vi2 = 1;
when (decile = '3') vi3 = 1;
when (decile = '4') vi4 = 1;
when (decile = '5') vi5 = 1;
when (decile = '6') vi6 = 1;
when (decile = '7') vi7 = 1;
when (decile = '8') vi8 = 1;
when (decile = '9') vi9 = 1;
end;
end;
end;
run;
I used the following macro program to cut down on lines of code:
data whatever;
set something;
%macro catdec (lettr);
when ("&lettr") do;
v&lettr = 1;
select;
%do xcnt = 0 %to 9;
when (decile = "&xcnt") v&lettr.&xcnt = 1;
%end;
end;
end;
%mend catdec;
%catdec (G);
%catdec (H);
%catdec (I);
end;
run;
The job runs properly when I type out the code, but when I switch to using
the macro, I get an error in the log:
1600 %catdec (G);
MPRINT(CATDEC): when ("G") do;
MPRINT(CATDEC): vG = 1;
MPRINT(CATDEC): select;
MPRINT(CATDEC): when (decile = "0") vG0 = 1;
MPRINT(CATDEC): when (decile = "1") vG1 = 1;
MPRINT(CATDEC): when (decile = "2") vG2 = 1;
MPRINT(CATDEC): when (decile = "3") vG3 = 1;
MPRINT(CATDEC): when (decile = "4") vG4 = 1;
MPRINT(CATDEC): when (decile = "5") vG5 = 1;
MPRINT(CATDEC): when (decile = "6") vG6 = 1;
MPRINT(CATDEC): when (decile = "7") vG7 = 1;
MPRINT(CATDEC): when (decile = "8") vG8 = 1;
MPRINT(CATDEC): when (decile = "9") vG9 = 1;
MPRINT(CATDEC): end;
MPRINT(CATDEC): end;
ERROR: Expecting "WHEN", "OTHERWISE", or "END".
The MPRINT logic seems to show the code generated by the macro is the same
as what I am trying to create, but obviously there is a problem. I think
it may have something to do with the SELECT statement not wanting to see
the macro call when it is expecting "WHEN", "OTHERWISE" or "END", but I
thought the ampersand in front of the macro call would alleviate this
problem?
Any suggestions would be appreciated. Thank you.