DATA Step, Macro, Functions and more

Temporary arrays

Accepted Solution Solved
Reply
Super Contributor
Posts: 271
Accepted Solution

Temporary arrays

I have seen most of temporary array use with initial values. I wonder how can be made use of temporary array without initial values. 

data example13 (drop=i);
set sbp2;
array sbp[6];
array above[6];
array threshhold[6] _temporary_ (140 140 140 120 120 120);
do i = 1 to 6;
 if (not missing(sbp[i]))
 then above [i] = sbp[i] > threshhold[i];
 end;
run;

 


Accepted Solutions
Solution
‎12-22-2015 12:17 PM
Super User
Posts: 19,770

Re: Temporary arrays

Posted in reply to SAS_inquisitive

You can use temporary arrays to calculate rolling/moving statistics.

I also like using it for look ups/loops, when the lookup data is another data set, so loading the temporary array from a different data set to perform lookups.

 

Here's an example of rolling statistics:

http://support.sas.com/kb/41/380.html

 

 

/* create monthly sales data from January 2008 to December 2010 */
data test;
do mo_period = '01jan2008'd to '31dec2010'd;
  sales = round(ranuni(1234567) * 1000, .01);
  mo_period = intnx('month', mo_period, 0, 'END');
  output;
end;
format mo_period monyy7. sales comma10.2;
run;

/*specify the number of periods in the rolling sum and average*/
%let roll_num = 12;

data new;
set test;
/*create array with specific number of elements*/
/*passing _temporary_ arrays as arguments to functions is not supported*/
/*until SAS 9.2*/
/*if the array is a _TEMPORARY_ array, the elements are automatically retained*/
array summed[&roll_num] _temporary_;

/*alternatively, a non-temporary array can be used but must be retained:*/
/*array summed[&roll_num];*/
/*retain summed;*/

/*E represents the element of the array to assign a sales value*/
/*Increment it by one unless it is equal to &roll_num, at which point*/
/*start over and assign it a value of 1. This causes the oldest period to*/
/*be replaced by the newest period once &roll_num periods have been read.*/
if E = &roll_num then E = 1;
else E + 1;
/*assign value to proper element of the array*/
summed[E] = sales;
/*start summing once &roll_num values have been read from the data set*/
if _N_ >= &roll_num then do;
  roll_sum = sum(of summed[*]);
  roll_avg = mean(of summed[*]);
end;
format roll_sum roll_avg comma10.2;
run;

proc print; run;

View solution in original post


All Replies
Solution
‎12-22-2015 12:17 PM
Super User
Posts: 19,770

Re: Temporary arrays

Posted in reply to SAS_inquisitive

You can use temporary arrays to calculate rolling/moving statistics.

I also like using it for look ups/loops, when the lookup data is another data set, so loading the temporary array from a different data set to perform lookups.

 

Here's an example of rolling statistics:

http://support.sas.com/kb/41/380.html

 

 

/* create monthly sales data from January 2008 to December 2010 */
data test;
do mo_period = '01jan2008'd to '31dec2010'd;
  sales = round(ranuni(1234567) * 1000, .01);
  mo_period = intnx('month', mo_period, 0, 'END');
  output;
end;
format mo_period monyy7. sales comma10.2;
run;

/*specify the number of periods in the rolling sum and average*/
%let roll_num = 12;

data new;
set test;
/*create array with specific number of elements*/
/*passing _temporary_ arrays as arguments to functions is not supported*/
/*until SAS 9.2*/
/*if the array is a _TEMPORARY_ array, the elements are automatically retained*/
array summed[&roll_num] _temporary_;

/*alternatively, a non-temporary array can be used but must be retained:*/
/*array summed[&roll_num];*/
/*retain summed;*/

/*E represents the element of the array to assign a sales value*/
/*Increment it by one unless it is equal to &roll_num, at which point*/
/*start over and assign it a value of 1. This causes the oldest period to*/
/*be replaced by the newest period once &roll_num periods have been read.*/
if E = &roll_num then E = 1;
else E + 1;
/*assign value to proper element of the array*/
summed[E] = sales;
/*start summing once &roll_num values have been read from the data set*/
if _N_ >= &roll_num then do;
  roll_sum = sum(of summed[*]);
  roll_avg = mean(of summed[*]);
end;
format roll_sum roll_avg comma10.2;
run;

proc print; run;
Super User
Super User
Posts: 7,039

Re: Temporary arrays

Posted in reply to SAS_inquisitive

You do not need to initialize the temporary array if it is purpose if mainly to store information.

For example you might use a temporary array as accumlators that you will later report out.

Here is a contrived example:

data _null_;
  array agecount (100) _temporary_;
  set sashelp.class end=eof;
  agecount(age)=sum(1,agecount(age));
  if eof then do age=1 to dim(agecount) ;
    count=agecount(age);
    if count > 0 then put age= count=;
  end;
run;

 

Super User
Super User
Posts: 7,942

Re: Temporary arrays

Posted in reply to SAS_inquisitive

I am not sure you understand the temporary.  Basically arrays are just a reference to a group of variables which may or may not have the same prefix.  What you are doing when you create a temporary array is create variables with those values, for the duration of the datastep.  You could achieve the same result with using a normal array statement, and then dropping those values from the datastep at the end.  Hence why values are given to temporary arrays on setup as they don't exist and wont exist after the datastep.  If you want to use existing variables or make them available after datastep, you wouldn't use a temporary array?  In your example, what would be the point of the temporary array if there was no data in it, it would then be equivalent to > .

Super Contributor
Posts: 271

Re: Temporary arrays

Thanks, @Reeza@Tom, and @RW9@RW9 I have good grasp of non temporary array ( creating a group of variables that can be referenced later in the program).  I read some SAS papers about temporary array where it was used to store constant values at the beginnig that can be retained over subsequent iterations.  Since variables are not created in the temporary array, I was curios what can be use of it  without intital values.

Super User
Posts: 19,770

Re: Temporary arrays

Posted in reply to SAS_inquisitive
I don't think the idea of variables not being created is correct. Its more along the lines of variables created, but dropped before output.
Super Contributor
Posts: 271

Re: Temporary arrays

@Reeza

 

I was refering to as this example.  This creates variables arr1, arr2, arr3, and arr4

array  arr[  4 ];

 

I wonder what does it create in PDV for temporary array?

Super User
Posts: 19,770

Re: Temporary arrays

Posted in reply to SAS_inquisitive

Temporary variables are created through the use of certain SAS options and statements, and like automatic variables, are not written to the output data set.

 

 

Some common temporary variables include:

IN=variable: IN= is a data set option that indicates whether a particular data set has contributed to the current observation. The user specifies a variable name with the option that will have the value of 1 if the data set contributed to the observation or 0 if it did not. ÿ END=variable: END= is an option to the SET statement that indicates the end of the input data has been reached. The user defines a variable name that will have the value of 1 if the end of the data has been reached, 0 if it has not. Only one END= can be specified on the SET statement. If multiple data sets are given in the SET statement, the END= variable will be set to 1 only when the last observation of the last data set has been read.

 

User defined variables can be assigned the value of automatic and temporary variables if it is necessary to have their values in the output data set.

 

The program data vector holds all variables, user defined, automatic, and temporary, from the data sets, input sources, or created during the execution of the data step. The program data vector is used to populate the output data sets. All variables in the output data sets are in the program data vector, but not all variables on the program data vector are written to the output data sets.

 

During execution SAS will set the non-retained and non-input variables to missing in the program data vector at the beginning of each iteration of the DATA step. SAS reads data from an input file or from a SAS data set directly into the program data vector, replacing the previously existing values.

 

Liberally stolen from here:

http://support.sas.com/resources/papers/proceedings12/255-2012.pdf

 

 

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 841 views
  • 3 likes
  • 4 in conversation