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
Rhodochrosite | Level 12

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
Rhodochrosite | Level 12

 

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
Rhodochrosite | Level 12

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
Rhodochrosite | Level 12
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-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 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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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