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; read all var {&Y}; close M; RR = &Y[4]; 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; read all var {&Y}; close MM; R = &Y[4]; *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;
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:
If you do it in SAS/IML, it will take about a dozen lines, with no need to call any other SAS procedures.
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;
read all var {&Y};
close MM;
R = &Y[4];
*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;
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?
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.
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.
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.
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.
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:
If you do it in SAS/IML, it will take about a dozen lines, with no need to call any other SAS procedures.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.
Find more tutorials on the SAS Users YouTube channel.