Sample 24694: Obtaining the previous value of a variable within a BY group
Bring previous observation's value down to the current
observation (lag), resetting at the BY group.
Note:
A LAGn function stores a value in a queue and returns a value
stored previously in that queue. Each occurrence of a LAGn
function in a program generates its own queue of values.
The queue for LAGn is initialized with n missing values,
where n is the length of the queue (for example, a LAG2 queue
is initialized with two missing values). When LAGn is
executed, the value at the top of the queue is removed and
returned, the remaining values are shifted upwards, and the
new value of the argument is placed at the bottom of the
queue. Hence, missing values are returned for the first n
executions of LAGn, after which the lagged values of the
argument begin to appear.
Storing values at the bottom of the queue and returning
values from the top of the queue occurs ONLY when the
function is EXECUTED. A LAGn function that is executed
conditionally will store and return values only from the
observations for which the condition is satisfied.
Note: This is based upon Example 5.7 from Combining and Modifying SAS Data Sets - Examples.
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
/***************************************************************************/
/* This program generates up to three lagged values. By increasing the */
/* size of the array and the number of assignment statements that use */
/* the LAGn functions, you can generate as many lagged values as needed. */
/***************************************************************************/
/* Create starting data */
data old;
input start end;
datalines;
1 1
1 2
1 3
1 4
1 5
1 6
1 7
2 1
2 2
3 1
3 2
3 3
3 4
3 5
;
data new(drop=i count);
set old;
by start;
/* Create and assign values to three new variables. Use ENDLAG1- */
/* ENDLAG3 to store lagged values of END, from the most recent to the */
/* third preceding value. */
array x(*) endlag1-endlag3;
endlag1=lag1(end);
endlag2=lag2(end);
endlag3=lag3(end);
/* Reset COUNT at the start of each new BY-Group */
if first.start then count=1;
/* On each iteration, set array elements that have */
/* not yet received a lagged value for the current */
/* BY-Group to missing. Increase count by 1. */
do i=count to dim(x);
x(i)=.;
end;
count + 1;
run;
proc print;
run;
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
Obs start end endlag1 endlag2 endlag3
1 1 1 . . .
2 1 2 1 . .
3 1 3 2 1 .
4 1 4 3 2 1
5 1 5 4 3 2
6 1 6 5 4 3
7 1 7 6 5 4
8 2 1 . . .
9 2 2 1 . .
10 3 1 . . .
11 3 2 1 . .
12 3 3 2 1 .
13 3 4 3 2 1
14 3 5 4 3 2
Bring the previous observation's value down to the current
observation (lag), resetting at the BY group.
Type: | Sample |
Topic: | SAS Reference ==> DATA Step SAS Reference ==> Functions ==> Special
|
Date Modified: | 2006-06-19 03:02:39 |
Date Created: | 2004-09-30 14:09:05 |
Operating System and Release Information
SAS System | Base SAS | All | n/a | n/a |