Help using Base SAS procedures

Using arrays for log and sqrt functions

Reply
Occasional Contributor
Posts: 14

Using arrays for log and sqrt functions

Trying to use arrays for log and square root functions.  First, how do I get each individual log and square root function, for example, it is giving me the log for all of x1-x5, however I just want it for each subset of data, for example, x1 is 1 and 11, I want the log of just that.  In addition, how do I get the x1-x5 and the y1-y3 on the same output, right now, (using seperate statements), I can get the log of x1-x5 (not individually) and if I run a seperate statement I can get the square root of sy1-sy3, not inidivdually....

 

data speed;
input X1-X5 Y1-Y3;
datalines;
1 2 3 4 5 6 7 8
11 22 33 44 55 66 77 88
;

data speed2 (drop=x1-x5 i);
set work.speed;
array num {5} lx1-lx5;
do i= 1 to 5;
log = (i);

end;

data speed2 (drop=y1-y3 i);

array let {3} sy1-sy3;
do i=1 to 3;
sqrt = (i);
end;

 


run;

Super User
Posts: 19,789

Re: Using arrays for log and sqrt functions

Posted in reply to zjhansen30

Can you please post some sample output data, what are you expecting?

Occasional Contributor
Posts: 14

Re: Using arrays for log and sqrt functions

It is giving me the log of all 5 combined for x1-x5, I wanted it for each column, for example x1 is 1 and 11, then x2 is 2 and 22, etc...

X1   X2  X3 X4 X5 LX1 LX2 LX3 LX4 LX5 Log

1     2     3    4   5   .         .      .     .       .     5
11   22   33  44 55  .         .      .    .       .      5

Super User
Posts: 19,789

Re: Using arrays for log and sqrt functions

Posted in reply to zjhansen30

You're making a couple of mistakes, in how you're using the functions. Also, you need variables to store the results. I'm assuming new variables, but you could reuse the same variables.

 

data speed;
input X1-X5 Y1-Y3;
datalines;
1 2 3 4 5 6 7 8
11 22 33 44 55 66 77 88
;

data want;
set speed;
array x(5);
array y(3);
array lx(5);
array ly(3);
array sqrt_x(5);
array sqrt_y(3);

do i=1 to dim(x);
	lx(i)=log(x(i));
	sqrt_x(i)=sqrt(x(i));
end;

do i=1 to dim(y);
	ly(i)=log(y(i));
	sqrt_y(i)=sqrt(y(i));
end;
run;
Occasional Contributor
Posts: 14

Re: Using arrays for log and sqrt functions

Thank you so much. I was getting confused and that was over my head, but I used this

data speed2 (drop=y1-y3 x1-x5 i);
set speed;
array x(5);
array y(3);
array lx(5);
array sy(3);


do i=1 to 5;
lx(i)=log(x(i));

end;

do i=1 to 3;

sy(i)=sqrt(y(i));
end;

run;
SAS Super FREQ
Posts: 3,752

Re: Using arrays for log and sqrt functions

Posted in reply to zjhansen30

You have to read the existing variables into an arry and then access the arrays and call the LOG and SQRT functions.  I've put all results into a single data set, but you can create two data seta sets if you prefer:

 

data speed2 (drop=x1-x5 y1-y3 i);
set work.speed;
array logX {5} lx1-lx5;     /* for new variables */
array X[5] x1-x5;           /* existing vars */
array sqrtY {3} sy1-sy3;    /* for new vars */
array Y[3] y1-y3;           /* existing vars */

do i= 1 to 5;
   logX[i] = log(x[i]);
end;
do i=1 to 3;
   sqrtY[i] = sqrt(y[i]);
end;
run;

proc print; run;
Super User
Posts: 5,504

Re: Using arrays for log and sqrt functions

Posted in reply to zjhansen30

You will need two arrays for each transformation.  One is for the input, one for the output.  For example:

 

 

data want;

set speed;

array in {3} y1-y3;

array out {3} sy1-sy3;

do _i_=1 to 3;

   out{_i_} = sqrt(in{_i_});

end;

run;

 

If you consider what this statement means:

 

sqrt = (1);

 

It is saying assign a value to the variable named SQRT.  There is no function being applied.

 

Good luck.

Ask a Question
Discussion stats
  • 6 replies
  • 427 views
  • 1 like
  • 4 in conversation