2
votes

Let's say I'm trying to do the following:

%macro test(a=);
%do i=1 %to &a;
    proc iml;
        b=b//(2*i);
    quit;
%end;

proc iml;
    print sum(b);
quit;

%mend;

%test(a=2);

In the code I'm trying to write, I can't put it all in one IML (I need a proc freq within the do loop). The code above gives the error "Matrix b not set to a value." How do I tell SAS what b is so that I can still access it after I've quit the iml statement?

2

2 Answers

2
votes

Two suggestions:

1) Use the STORE statement to write the matrix B to disk at the end of the first call, then use the LOAD statement to read it in during the second call:

store B;
quit;

proc freq data=...;
run;

proc iml;
load B;
...

2) An alternative approach is to call PROC FREQ from within your PROC IML program by using the SUBMIT and ENDSUBMIT statements:

/* compute B */
submit;
proc freq data=...;
run;
endsubmit;

s = sum(b): /* B is still in scope */
0
votes

You need to rework things so the PROC IML; and QUIT; are outside of the macro. This is good practice most of the time even in other scenarios where it's not that important, but here it's necessary.

IE

%macro test(a=);
%do i=1 %to &a;
        b=b//(2*i);
%end;

proc iml;
%test(a=5);
quit;

QUIT ends the PROC IML session and clears its memory.