## PROC IML inside SAS Macro.

Hello everyone, what is wrong with the following code? I cannot get it to work.

```%macro mymacro(mydata,X,Y,M);
proc corr data=&mydata outs=M;
var &X &Y;
run;

/*Now let's begin Proc IML*/
proc iml;
use &mydata; read all var{&X} into xvar;
use &mydata; read all var{&Y} into yvar;
create combined var{xvar,yvar};
append;
close combined;

use M;
close M;
RR = &Y;

print RR;
AA=j(&M,1,0);
quit;

proc iml;
do i = 1 to &M;
U=uniform(repeat(0,nrow(&X),1));
create together var{&X,U};
append;
close together;

proc sort data=together;
by U;
run;

data together;
merge together &mydata;
run;

proc corr data=together spearman outs=MM noprint;
var &X &Y;
run;

use MM;
close MM;
R = &Y;
*print R;

if abs(R) > RR then AA[i]=1;
else  AA[i]=0;

end;
quit;
p_value=mean(AA);
print p_value;

%mend mymacro;```

1 ACCEPTED SOLUTION

Accepted Solutions

## Re: PROC IML inside SAS Macro.

Okay, so this is a bootstrap analysis (or maybe a permutation test) for the Spearman correlation.

I've recently written a Guide to Bootstrapping in SAS that you might want to read. Click on the link that says "The basic bootstrap in SAS/IML" for an overview of how to use SAS/IML (although you can also compute this analysis without IML.)

The list includes a link to the article "How to compute a p-value for a bootstrap analysis." I suggest the second formula by Davison and Hinkley (1997, p 141) .

Is this for a class? If so, let me give you some hints rather than the complete solution:

• In SAS/IML, the CORR function supports the "Spearman" option, so you can compute the statistics in IML, if you want.
• The SAMPLE function in SAS/IML supports random sampling with replacement. So if X is a vector, then bootX = sample(X); is a bootstrap sample from X.

If you do it in SAS/IML, it will take about a dozen lines, with no need to call any other SAS procedures.

8 REPLIES 8

## Re: PROC IML inside SAS Macro.

Please remember to include the portion of the SAS log that shows the erro.

In this case, the problem is that inside the (second) call to PROC IML you have a loop, but then you call PROC SORT and other procedures, which causes PROC IML to exit.

``````
proc iml;
do i = 1 to &M;
U=uniform(repeat(0,nrow(&X),1));
create together var{&X,U};
append;
close together;

/**** The next statement QUITs PROC IML and starts a new procedure */
proc sort data=together;
by U;
run;

data together;
merge together &mydata;
run;

proc corr data=together spearman outs=MM noprint;
var &X &Y;
run;

/**** The next statement is invalid b/c you are no longer in PROC IML */
use MM;
close MM;
R = &Y;
*print R;

if abs(R) > RR then AA[i]=1;
else  AA[i]=0;

end;
quit;

/**** The next stmt appears to be an IML stmt, but you've already QUIT! */
p_value=mean(AA);
print p_value;
``````

## Re: PROC IML inside SAS Macro.

Thank you very much for your correction. If I quit the PROC IML after close together, will the do loop continue down to next line even after quitting PROC IML?

## Re: PROC IML inside SAS Macro.

A loop inside of IML terminates when IML terminates. So you can't do this looping inside of IML.

It's not clear why you are even using IML here, as you don't seem to be doing any matrix arithmetic.

You could do this looping with a macro %DO loop.

Or, if you really want to do this inside of an IML loop, you could do the sorting, correlations and data combining all using IML commands instead of PROCs and data steps.

--
Paige Miller

## Re: PROC IML inside SAS Macro.

PS. A programming tip that I often use is to completely write and debug the program before trying to wrap it into a macro.

For this program, I'd use

%LET mydata,= DSName;

%LET X = XVar;

%LET Y = YVar;

%LET M = OutCorr;

and get the whole program working with those macro variables before attempting to define %MYMACRO.

## Re: PROC IML inside SAS Macro.

It looks like this might be some sort of simulation study of the correlation between ... something? If you tell us (in words) what you are trying to accomplish, perhaps we could provide a way to compute it. That might be more efficient than you posting many failed attempts.

## Re: PROC IML inside SAS Macro.

Yes you are right. So I have two variables X and Y coming from mydata and M is the number of simulations. I calculate the spearman correlation between X and Y at first. Then I loop over M times and resample X and compute correlation M times between resampled X and Y in each loop. Y is not resampled. Finally I find the  proportion of times that the correlation in each loop is greater than the original correlation.

## Re: PROC IML inside SAS Macro.

Okay, so this is a bootstrap analysis (or maybe a permutation test) for the Spearman correlation.

I've recently written a Guide to Bootstrapping in SAS that you might want to read. Click on the link that says "The basic bootstrap in SAS/IML" for an overview of how to use SAS/IML (although you can also compute this analysis without IML.)

The list includes a link to the article "How to compute a p-value for a bootstrap analysis." I suggest the second formula by Davison and Hinkley (1997, p 141) .

Is this for a class? If so, let me give you some hints rather than the complete solution:

• In SAS/IML, the CORR function supports the "Spearman" option, so you can compute the statistics in IML, if you want.
• The SAMPLE function in SAS/IML supports random sampling with replacement. So if X is a vector, then bootX = sample(X); is a bootstrap sample from X.

If you do it in SAS/IML, it will take about a dozen lines, with no need to call any other SAS procedures.

## Re: PROC IML inside SAS Macro.

Okay. Thank you very much. You don't have to provide me complete solution. These links are helpful and I should be good now.
From The DO Loop