/***************************************************************************************************************** SAS file name: array_lag File location: _________________________________________________________________________________________________________________ Purpose: To demonstrate how to store lagged values in temporary arrays in SAS Author: Peter Clemmensen Creation Date: 01/05/2019 This program supports the example page "Use Arrays to Store Lagged Values in SAS" on SASnrd.com *****************************************************************************************************************/ /* Example data */ data have; input id var @@; datalines; 1 1 1 2 1 3 1 4 1 5 2 1 2 . 2 3 2 . 2 5 3 1 3 2 3 4 3 . 3 5 ; /* Store lagged values in a temporary array */ data want; array lag[0:2] _temporary_; /* 1 */ call missing(of lag[*], obs); /* 2 */ do obs=1 by 1 until (last.id); /* 3 */ set have; by id; lag[mod(obs, 3)]=var; /* 4 */ count=n(of lag[*]); /* 5 */ avg=mean(of lag[*]); temp = catq('d', '|', of lag[*]); /* 6 */ put temp=; output; end; run;