Statistical programming, matrix languages, and more

Proc IML iterative do loop sum

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 19
Accepted Solution

Proc IML iterative do loop sum

I have a problem I am trying to solve using a do loop with proc iml.  What I am trying to do is take each data point from column "y" one at a time, and multiply it by the vector x, and create a column "z" which is the sum of each iteration of the loop.

 

 

data example;
input x y;
datalines;
1 2
3 6
5 10
7 14
9 18
;

proc iml;
use work.example;
read all var{x} into x;
do i=1 to 5;
read point i var{y} into y;
z=x*y;
end;
print z;

For example if I run this it will give me the z output as:

 

18

54

90

126

162

This is just taking the last iteration of the loop with y=18 and multiplying by the "x" column.  What I am trying to do is get results like:

z

2+6+10+14+18=50 (first iteration)

.

.

.

.

18+54+90+126+162=450 (last iteration)


Accepted Solutions
Solution
‎04-24-2017 09:46 AM
Grand Advisor
Posts: 9,335

Re: Proc IML iterative do loop sum

How about this one .



data example;
input x y;
datalines;
1 2
3 6
5 10
7 14
9 18
;

proc iml;
use work.example;
read all var{x y} ;
close;
z=(y*x`)[,+];

print z;
quit;


View solution in original post


All Replies
Frequent Contributor
Posts: 93

Re: Proc IML iterative do loop sum

data example;
input x y;
datalines;
1 2
3 6
5 10
7 14
9 18
;

PROC SQL;
CREATE TABLE MYJOIN AS
SELECT EXAMPLE1.X, EXAMPLE2.Y, EXAMPLE1.X * EXAMPLE2.Y AS MULT
FROM EXAMPLE AS EXAMPLE1, EXAMPLE AS EXAMPLE2;
QUIT;

PROC SQL;
CREATE TABLE SUMS AS
SELECT MYJOIN.X, SUM(MULT) AS TOTAL FROM MYJOIN GROUP BY MYJOIN.X;
QUIT;
Solution
‎04-24-2017 09:46 AM
Grand Advisor
Posts: 9,335

Re: Proc IML iterative do loop sum

How about this one .



data example;
input x y;
datalines;
1 2
3 6
5 10
7 14
9 18
;

proc iml;
use work.example;
read all var{x y} ;
close;
z=(y*x`)[,+];

print z;
quit;


Occasional Contributor
Posts: 19

Re: Proc IML iterative do loop sum

Wow that is some witchcraft! I can't believe how simple that is.  Could you explain the syntax?  Particularly what the backtick symbol does in this situation, and how [,+] works?

Grand Advisor
Posts: 9,335

Re: Proc IML iterative do loop sum

For example:

 

x   y

1   4

2   5

3   6

 

 

` means transpose operator

 

y*x` =

4                              4 8 12

5    *   1 2 3    =        5 10 15

6                              6 12 18

 

[,+]  mean sum each row   

 

4 8 12   = 24

.......

.......

SAS Super FREQ
Posts: 3,234

Re: Proc IML iterative do loop sum

I like KSharp's solution, which doesn't require a loop.  If you are required to use a loop (maybe for a homework assignment or because this is a simplification of a more complicated computation), the following program shows how to iterate over the elements of x to form each element of z:

 

proc iml;
use work.example;
read all var {x y};      /* read all data */
close;
z = j(nrow(x),1,.);      /* allocate result vector */
do i=1 to nrow(x);
   z[i] = sum(x[i] * y); 
end;
print z;

KSarp's solution uses a concept called "vectorizing" a program. For more about how to vectorize a SAS/IML program, see the article "How to vectorize computations in a matrix language."

Post a Question
Discussion Stats
  • 5 replies
  • 168 views
  • 6 likes
  • 4 in conversation