## Do loops in Proc IML

Occasional Contributor
Posts: 6

# Do loops in Proc IML

I am trying to automate a do loop in a Proc IML statement, but I cant figure out why the matrix called ''vwap'' doesnt update properly.

I have put two versions of the code below.  The first is just the standard code with each iteration hard coded, the second is the code with the do loop inserted.

proc iml;

reset print;

xxx = {10 2 3.01, 10 3 3.05, 10 1 3.07};

volume = xxx[1,2];

price = xxx[1,3];

call symput ('volume', char(volume));

call symput ('price', char(price));

vwap = {[&volume] &price}`;

volume = xxx[2,2];

price = xxx[2,3];

call symput ('volume', char(volume));

call symput ('price', char(price));

vwap = {[&volume] &price}`;

volume = xxx[3,2];

price = xxx[3,3];

call symput ('volume', char(volume));

call symput ('price', char(price));

vwap = {[&volume] &price}`;

quit;

****** The second version of the code with the do loop inserted - VWAP does not update for some reason.

proc iml;

reset print;

xxx = {10 2 2.91, 10 3 3.05, 10 1 3.07};

do i = 1 to 3;

volume = xxx[i,2];

price = xxx[i,3];

call symput ('volume', char(volume));

call symput ('price', char(price));

vwap = {[&volume] &price}`;

end;

quit;

Regular Contributor
Posts: 152

## Re: Do loops in Proc IML

Why are you creating these macro variables in the first place?  If you are trying to extract the columns from the matrix, XXX, to create the matrix, VWAP, why not simply do the following?

VWAP=XXX[,2:3];

Or, if you also need the matrices, VOLUME and PRICE, why not do the following?

VOLUME=XXX[,2];

PRICE=XXX[,3];

Occasional Contributor
Posts: 6

## Re: Do loops in Proc IML

I am not trying to extract the columns - this is a part of a bigger program I am writing whereby I am taking some data and using the matrix language to manipulate it before sending it back.

I have simplified the issue as much as I can to make it easier to understand.

I am using information from each row of the matrix xxx to create a vector (VWAP) whose length changes as each row is considered.

The problem is why does putting it in a loop stop it from working when the attributes of volume and price are exactly the same in both versions.

Regular Contributor
Posts: 152

## Re: Do loops in Proc IML

Does the first, "hard-coded" version work in your bigger problem?

If not, perhaps the problem is that the CALL SYMPUT statements do NOT work to create the macro variables, &VOLUME and &PRICE, in the same PROC IML step.  These statements would not work in a DATA step.  You may have to explain more details about the bigger problem you are trying to solve.  Or, submit this problem to the separate community SAS thread on PROC IML.

Occasional Contributor
Posts: 6

## Re: Do loops in Proc IML

The first code works perfectly, that is why it is so perplexing.  It should be easy to just set a do statement to iterate to the end of the matrix, but when I do the code stops working.

Both price and volume have exactly the same attributes in each section of code, which is why I just cant understand why it no longer works.

SAS Super FREQ
Posts: 3,831