turn on suggestions

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

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- Two dimentional arrays and use them in calculation

Topic Options

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-01-2016 05:00 AM

**data** test;

input temp speed;

datalines;

2 25

6 30

4 29

5 38

8 20

**run**;

**data** have;

set test;

array temp_array(**5**,**2**) temp speed ;

m=(temp_array{**1**,**1**}-temp_array{**2**,**2**}/temp_array{**2**,**1**}-temp_array{**3**,**2**});

run;

So want to create array or any other way I can refrence the value of temp and speed in my formula?

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-01-2016 05:16 AM

Are the results from that what you expect?

SAS arrays are shortcuts to variables - in the same row.

Your array above does not take on the values in your dataset. I would expect all results to be missing...

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-01-2016 05:35 AM

Hi Reeza

so basically I want to create array on variable values and then use that array as refence in calculation. So I can do create macro variable for each value in temp and speed variable then how to use that in array

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-01-2016 05:36 AM

Hi Reeza

so basically I want to create array on variable values and then use that array as refence in calculation. So I can do create macro variable for each value in temp and speed variable then how to use that in array

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-01-2016 05:44 AM

What are you trying to do overall? You can use your method but it may not be efficient given how SAS processes information and your original question seemed to relate to efficiency.

If you want to work with data as you're stating consider using SAS IML.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-01-2016 06:06 AM

exactly I am trying to find alternamte to proc IML

**proc** **iml**;

use test;

read all;

x=temp||speed;

m=(x[**2**,**2**]-x[**1**,**2**])/(x[**2**,**1**]-x[**1**,**1**]);

l=x[**1**,**1**]*m-x[**1**,**2**];

create line var{m l};

append;

close line;

**run**;

**quit**;

but I want to do without IML.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-01-2016 05:37 AM

You have to load the data into the array "manually", see example code below.

```
data have;
set test end=last;
array temp_array(5,2) _temporary_;
temp_array{_n_, 1} = temp;
temp_array{_n_, 2} = speed;
if last = 1 then do;
m=(temp_array{1,1}-temp_array{2,2}/temp_array{2,1}-temp_array{3,2});
putlog m=;
end;
run;
```

The example uses the _TEMPORARY_ keyword, so no variables are created in the Program Data Vector (PDV), also all the values are retained.

Once the array is loaded, you can use the expression.

Bruno

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-01-2016 05:59 AM

Hi Bruno one thing why you using the end=last and if process I didn't understand and if you do calculation it does not give right answer

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-01-2016 07:16 AM

The END = LAST sets a flag that tells you when the last oberservation has been read;

The expression for M was doifferent in your DATA Step as compared to the one you are using in IML

Below is the new code:

```
data have;
set test end=last;
array temp_array(5,2) _temporary_;
temp_array{_n_, 1} = temp;
temp_array{_n_, 2} = speed;
/*
* if all the data has been read, do the caculation
*/
if last = 1 then do;
m = (temp_array{2,2} - temp_array{1,2}) / (temp_array{2,1} - temp_array{1,1});
l = temp_array{1,1} * m - temp_array{1,2};
putlog m= l=;
output;
end;
keep m l;
run;
```

Please note that DATA Step arrays are not a replacement for the functionality you have in IML.

Bruno

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-01-2016 08:21 AM

Thanks Bruno

It did work and take on board your advise and learn more about array

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-01-2016 12:51 PM

```
If you don't have IML
HAVE
libname sd1 "d:/sd1";
data sd1.have;
input temp speed;
cards4;
2 25
6 30
4 29
5 38
8 20
;;;;
run;
WANT
[1,1]-[2,2]/[2,1]-[3,2]);
-32 = 2 - 30/6 - 29
%utl_submit_r64('
library(haven);
temp_array<-as.matrix(read_sas("d:/sd1/have.sas7bdat"));
temp_array;
m<-(temp_array[1,1]-temp_array[2,2]/temp_array[2,1]-temp_array[3,2]);
m;
');
> library(haven);temp_array<-as.matrix(read_sas("d:/sd1/have.sas7bdat"));
temp_array;m<-(temp_array[1,1]-temp_array[2,2]/temp_array[2,1]-temp_array[3,2]);m;
TEMP SPEED
[1,] 2 25
[2,] 6 30
[3,] 4 29
[4,] 5 38
[5,] 8 20
TEMP
-32
>
```

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-01-2016 07:06 AM

No need for ARRAY. By the way, your IML doesn't look right . data test; input temp speed; datalines; 2 25 6 30 4 29 5 38 8 20 ; run; data want; set test; m=dif(speed)/dif(temp); l=lag(temp)*m-lag(speed); run;

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-01-2016 08:20 AM

Thanks It worked like magic

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-01-2016 10:57 AM

HI Ksharp please can you tell me what is wrong with Proc IML just for my understanding. lag and dif only select one previous value what if I want to cahange the value pick up diffrent one.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

09-01-2016 11:42 AM

Please look at the documentation for the Lag and Dif functions. You can reference Lag1 (default when coding as Lag) Lag2 = second previous, Lag3 = third previous. You therefore have MANY previous records you can address and similar for DIF.

Lag can impact memory resources as copies of each value are kept. Lag100 requires keeping track of 100 values so memory is 100*length of the variable.