DATA Step, Macro, Functions and more

Two dimentional arrays and use them in calculation

Reply
Contributor dpa
Contributor
Posts: 35

Two dimentional arrays and use them in calculation

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?

 

Super User
Posts: 19,770

Re: Two dimentional arrays and use them in calculation

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

Contributor dpa
Contributor
Posts: 35

Re: Two dimentional arrays and use them in calculation

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

 

Contributor dpa
Contributor
Posts: 35

Re: Two dimentional arrays and use them in calculation

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

 

Super User
Posts: 19,770

Re: Two dimentional arrays and use them in calculation

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. 

Contributor dpa
Contributor
Posts: 35

Re: Two dimentional arrays and use them in calculation

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.

SAS Super FREQ
Posts: 708

Re: Two dimentional arrays and use them in calculation

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

Contributor dpa
Contributor
Posts: 35

Re: Two dimentional arrays and use them in calculation

Posted in reply to Bruno_SAS

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

 

SAS Super FREQ
Posts: 708

Re: Two dimentional arrays and use them in calculation

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

Contributor dpa
Contributor
Posts: 35

Re: Two dimentional arrays and use them in calculation

Posted in reply to Bruno_SAS

Thanks Bruno

 

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

Valued Guide
Posts: 505

Re: Two dimentional arrays and use them in calculation

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
>
Super User
Posts: 10,018

Re: Two dimentional arrays and use them in calculation

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;


Contributor dpa
Contributor
Posts: 35

Re: Two dimentional arrays and use them in calculation

Thanks It worked like magicSmiley Happy

Contributor dpa
Contributor
Posts: 35

Re: Two dimentional arrays and use them in calculation

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.

Super User
Posts: 11,343

Re: Two dimentional arrays and use them in calculation

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.

Ask a Question
Discussion stats
  • 15 replies
  • 569 views
  • 1 like
  • 6 in conversation