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

## Proc IML do loop vectorization SAS 9.4

I'm working with a nested loop using proc iml to replicate exisiting R code. I have a solution but it's computationally very slow. Below is the inner-most loop that I am wondering if I can vectorize for increased performance.

``````do k=1 to nrow(ru);
y={};
x=ru[k,];
submit x;

%ret(&x, data);
endsubmit;``````

This part of the code is looping through the elements of the ru vector and passing them as a macro variable into the ret function.  The ret function produces datasets which end up producing subsequent ru vectors and the process is iterated many times.  I'm pretty sure this is where the bottleneck in the code is coming from.  Is it possible for me to pass the vector of ru elements without looping through each element?

1 ACCEPTED SOLUTION

Accepted Solutions
SAS Super FREQ

## Re: Proc IML do loop vectorization SAS 9.4

Yes, you can pass a vector to the SUBMIT statement. You will get a space-separated list of values. You'll have to make your macro more complicated to handle this situation, but here's the idea:

``````proc iml;
ru = {3, 7, 10, 2};
/* old method */
do k=1 to nrow(ru);
x=ru[k,];
submit x;
%put &x;
endsubmit;
end;

/* new method */
submit ru;
%put &ru;
endsubmit;
``````
2 REPLIES 2
SAS Super FREQ

## Re: Proc IML do loop vectorization SAS 9.4

Yes, you can pass a vector to the SUBMIT statement. You will get a space-separated list of values. You'll have to make your macro more complicated to handle this situation, but here's the idea:

``````proc iml;
ru = {3, 7, 10, 2};
/* old method */
do k=1 to nrow(ru);
x=ru[k,];
submit x;
%put &x;
endsubmit;
end;

/* new method */
submit ru;
%put &ru;
endsubmit;
``````
Super User

## Re: Proc IML do loop vectorization SAS 9.4

You could this function to transform SAS matrix into R dataframe. and do loop in R statement.

```proc iml;
a = {1 2 3, 4 . 6};
call ExportMatrixToR(a, "m");
submit / R;
print(m)
endsubmit;
```
From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
• 2 replies
• 716 views
• 2 likes
• 3 in conversation