BookmarkSubscribeRSS Feed
AaronAardvaark
Calcite | Level 5

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. 

Can anyone please help me understand why this do loop is not working.

Thanks for your help

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;

5 REPLIES 5
1zmm
Quartz | Level 8

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

AaronAardvaark
Calcite | Level 5

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.

1zmm
Quartz | Level 8

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.

AaronAardvaark
Calcite | Level 5

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.

Rick_SAS
SAS Super FREQ

Linking to the answer as posted in the SAS/IML Support Community: https://communities.sas.com/message/170978#170978

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 1559 views
  • 0 likes
  • 3 in conversation