Sample 24665: Use the LAG function to conditionally carry information down a data set
It is important to understand how the LAG function works in order to achieve the desired results.
The value of the LAG function is returned to a queue. Each time the LAG function is executed, the value at the top of its queue is removed and returned, and all remaining values are shifted upward. The new value returned by the function is placed at the bottom of the queue. This process occurs only when the function is executed. A conditionally executed LAG function only stores and return a value from the observation that meets the condition. That means the LAG function returns the value from the last observation that met the conditional logic, which might not be the previous observation.
The sample code on the Full Code tab illustrates the effects of executing the LAG function conditionally.
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 example shows the difference in output when the LAG function is executed on every observation versus using conditional logic
to execute the function. It is recommended that the results of the LAG function be conditionally used instead of conditionally
executing the LAG function.
The goal of this code is to return the value from the previous observation when the observation number is an even number.
/* this code uses the MOD function to determine when the observation number is an even number */
data test;
input x @@;
/* If X is an even number, assign the value of X to a lag queue */
if mod(x,2)=0 then a=lag(x);
/* Execute the lag queue each iteration, and assign the value into B */
b=lag(x);
/* Conditionally assign from the lag queue */
if mod(x,2)=0 then c=b;
label a='(WRONG) a' c='(RIGHT) c';
datalines;
1 2 3 4 5 6 7 8
;
proc print label data=test;
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.
Store Every Other Lagged Value
(WRONG) (RIGHT)
Obs x a b c
1 1 . . .
2 2 . 1 1
3 3 . 2 .
4 4 2 3 3
5 5 . 4 .
6 6 4 5 5
7 7 . 6 .
8 8 6 7 7
Use the LAG function to create lagged values of a variable when certain conditions are met.
Type: | Sample |
Topic: | SAS Reference ==> DATA Step SAS Reference ==> Functions ==> Special
|
Date Modified: | 2005-12-16 03:02:56 |
Date Created: | 2004-09-30 14:09:02 |
Operating System and Release Information
SAS System | Base SAS | All | n/a | n/a |