I am trying to conditionally call a macro based on identifiers in an array, and have the macro variable change based on the value of the array. Here is my code:
data;
array file(*) $ file1-file7;
array prog(*) $ prog1-prog7;
do i=1 to dim(prog);
if prog(i) = "Y" then
call execute("%findit(name(file(i)))");
end;
run;
As an example:
%let file1 = C:\file.doc
%let prog1 = Y
I would expect that my code would create arrays for the file and prog variables that are defined earlier in my code. Then, it would begin cycling through the array. The first iteration would conditionally execute the macro statement, because the prog1 variable is Y. Then, the macro would execute using the value of file1 ("C:\file.doc") as the macro variable.
The conditional logic appears to be working. However, the macro executes on "name(file(I)))" instead of on "C:\file.doc".
Does anyone see my mistake?
You passed a string literal to CALL EXECUTE()
instead of using the value of your variable. But you also never set your variables to any values.
So if you have list of PROG/FILE combinations in a dataset. Like this:
data have ;
infile cards truncover ;
input prog $1. file $100. ;
cards;
YC:\file.doc
NC:\file2.doc
;
Then you can use them to generate calls to your macro like this:
data _null_;
set have ;
if prog = "Y" then call execute(cats('%nrstr(%findit)(',file,')'));
run;
So you should see this line in the SAS LOG to show what command you pushed onto the stack to execute after the DATA _NULL_ step.
1 + %findit(C:\file.doc)
If you have a series of macro variables then use symget()
function to retrieve the values. You can use CATS()
to generate the macro variable name to pass to the symget()
function.
%let prog1=Y;
%let file1=C:\file.doc;
%let n=1;
data _null_;
do n=1 to &n ;
if symget(cats('prog',n))='Y' then
call execute(cats('%nrstr(%findit)(&file',n,')'))
;
end;
run;