BookmarkSubscribeRSS Feed
zjhansen30
Fluorite | Level 6

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;

6 REPLIES 6
Reeza
Super User

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

zjhansen30
Fluorite | Level 6

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

Reeza
Super User

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;
zjhansen30
Fluorite | Level 6
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;
Rick_SAS
SAS Super FREQ

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;
Astounding
PROC Star

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.

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
  • 6 replies
  • 1868 views
  • 1 like
  • 4 in conversation