BookmarkSubscribeRSS Feed
dpa
Obsidian | Level 7 dpa
Obsidian | Level 7

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?

 

15 REPLIES 15
Reeza
Super User

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

dpa
Obsidian | Level 7 dpa
Obsidian | Level 7

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

 

dpa
Obsidian | Level 7 dpa
Obsidian | Level 7

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

 

Reeza
Super User

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. 

dpa
Obsidian | Level 7 dpa
Obsidian | Level 7

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.

BrunoMueller
SAS Super FREQ

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

dpa
Obsidian | Level 7 dpa
Obsidian | Level 7

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

 

BrunoMueller
SAS Super FREQ

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

dpa
Obsidian | Level 7 dpa
Obsidian | Level 7

Thanks Bruno

 

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

rogerjdeangelis
Barite | Level 11
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
>
Ksharp
Super User
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;


dpa
Obsidian | Level 7 dpa
Obsidian | Level 7

Thanks It worked like magicSmiley Happy

dpa
Obsidian | Level 7 dpa
Obsidian | Level 7

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.

ballardw
Super User

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.

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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