Solved
New Contributor
Posts: 4

# Array help

Hi Guys,

I have below table :

Data Abc;

input x y z;

datalines;

2 3 1

3 2 1

1 2 3

2 2 2

;

run;

Problem :

I want my to multiply all the observation one by one for their respective columns. Hence my output would look like below :

X Y  Z

2   3    1

6   6    1

6   12  3

12 24  6

Accepted Solutions
Solution
‎12-26-2017 05:47 AM
SAS Super FREQ
Posts: 502

## Re: Array help

I don't understand why code I post recently has linefeed issues.  My code was perfect before I hit post.  Now all the line feeds are gone.  Trying again.

``````data Abc0;
input a b c;
datalines;
2 3 1
3 2 1
1 2 3
2 2 2
;
data abc(keep= x y z);
retain x y z 1;
do i = 1 to nobs;
set abc0 point=i nobs=nobs;
x = x * a; y = y * b; z = z * c;
output;
end;
stop;
run;

proc print; run;``````

All Replies
SAS Super FREQ
Posts: 502

## Re: Array help

Here is one way.  If you have more than 3 variables, you can easily switch to use arrays.

``data Abc0; input a b c; datalines;2 3 13 2 11 2 32 2 2;data abc(keep= x y z); retain x y z 1; do i = 1 to nobs;   set abc0 point=i nobs=nobs;   x = x * a; y = y * b; z = z * c;   output; end; stop; run;proc print; run;``

Solution
‎12-26-2017 05:47 AM
SAS Super FREQ
Posts: 502

## Re: Array help

I don't understand why code I post recently has linefeed issues.  My code was perfect before I hit post.  Now all the line feeds are gone.  Trying again.

``````data Abc0;
input a b c;
datalines;
2 3 1
3 2 1
1 2 3
2 2 2
;
data abc(keep= x y z);
retain x y z 1;
do i = 1 to nobs;
set abc0 point=i nobs=nobs;
x = x * a; y = y * b; z = z * c;
output;
end;
stop;
run;

proc print; run;``````
New Contributor
Posts: 4

## Re: Array help

Thank you! You are right I have many variables and trying to do it with array but it is not working for me.

New Contributor
Posts: 4

Hi Warren,

New Contributor
Posts: 4

## Re: Array help

Any hint if I do it with Arrays as it has got many variables?
SAS Super FREQ
Posts: 502

## Re: Array help

``````data Abc0;
input a b c;
datalines;
2 3 1
3 2 1
1 2 3
2 2 2
;
data abc(keep= x y z);
array a1[3] a b c;
array a2[3] x y z;
retain x y z 1;
do i = 1 to nobs;
set abc0 point=i nobs=nobs;
do j = 1 to 3; a2[j] = a2[j] * a1[j]; end;
output;
end;
stop;
run;

proc print; run;``````
Super User
Posts: 10,770

## Re: Array help

If you have SAS/IML, could make it more simple.

``````Data Abc;
input x y z;
datalines;
2 3 1
3 2 1
1 2 3
2 2 2
;
run;
proc iml;
use abc;
read all var _all_ into x[c=vname];
close;
want=j(nrow(x),ncol(x),.);
do i=1 to ncol(x);
want[,i]=cuprod(x[,i]);
end;

create want from want[c=vname];
append from want;
close want;

quit;``````
☑ This topic is solved.