BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ajeet1
Calcite | Level 5

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
WarrenKuhfeld
Ammonite | Level 13

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;

View solution in original post

7 REPLIES 7
WarrenKuhfeld
Ammonite | Level 13

 

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;

 

WarrenKuhfeld
Ammonite | Level 13

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;
Ajeet1
Calcite | Level 5

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

Ajeet1
Calcite | Level 5

Hi Warren,

 

Could you please help with the array with the same problem

Ajeet1
Calcite | Level 5
Any hint if I do it with Arrays as it has got many variables?
WarrenKuhfeld
Ammonite | Level 13
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;
Ksharp
Super User

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;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 1400 views
  • 1 like
  • 3 in conversation