Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

🔒 This topic is **solved** and **locked**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 12-11-2018 07:31 PM
(3441 views)

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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;
```

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

Paige Miller

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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** is scheduled for May 6-9 in Orlando, FL. Sign up to be **first to learn** about the agenda and registration!

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.