BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
themanoj20080
Fluorite | Level 6

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

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.

View solution in original post

8 REPLIES 8
Rick_SAS
SAS Super FREQ

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;
themanoj20080
Fluorite | Level 6

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?

PaigeMiller
Diamond | Level 26

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
Rick_SAS
SAS Super FREQ

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.

 

Rick_SAS
SAS Super FREQ

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. 

 

 

themanoj20080
Fluorite | Level 6

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.

 

Rick_SAS
SAS Super FREQ

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.

themanoj20080
Fluorite | Level 6
Okay. Thank you very much. You don't have to provide me complete solution. These links are helpful and I should be good now.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Multiple Linear Regression in SAS

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.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 8 replies
  • 3363 views
  • 0 likes
  • 3 in conversation