Sample 24665: Use the LAG function to conditionally carry information down a data set
Illustrate 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 you use conditional */
/* and unconditional logic in your program. Because the LAG function stores */
/* values on the queue only when it is called, you must call LAG unconditionally */
/* (outside the IF condition) to get the correct answers. (SAS Language Reference, */
/* Dictionary --> Functions and CALL Routines --> LAG function, Example 2) */
title 'Store Every Other Lagged Value';
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, 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 |