Help using Base SAS procedures

Array help

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

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

Posted in reply to WarrenKuhfeld

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


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 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;

 

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

Re: Array help

Posted in reply to WarrenKuhfeld

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

Posted in reply to WarrenKuhfeld

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

Re: Array help

Posted in reply to WarrenKuhfeld

Hi Warren,

 

Could you please help with the array with the same problem

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.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 306 views
  • 0 likes
  • 3 in conversation