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
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;
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 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;
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;
Thank you! You are right I have many variables and trying to do it with array but it is not working for me.
Hi Warren,
Could you please help with the array with the same problem
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;
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;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.