Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Programming
- /
- SAS Procedures
- /
- Re: Using arrays for log and sqrt functions

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 02-22-2016 04:26 PM
(1843 views)

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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;

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;

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

⏰

Time is running out to save with the early bird rate. Register by Friday, March 1 for just $695 - $100 off the standard rate.

Check out the agenda and get ready for a jam-packed event featuring workshops, super demos, breakout sessions, roundtables, inspiring keynotes and incredible networking events.** **

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.