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?
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...
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
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
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.
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.
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
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
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
Thanks Bruno
It did work and take on board your advise and learn more about array
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
>
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;
Thanks It worked like magic
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.
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.