Posts

Showing posts with the label Retaining Values without Lag Functions

LAG Function: How to obtain information from previous observation(s)

Often times SAS® programmers need to retain the value of a variable in the current observation to the next observation. The LAG function  can be very helpful here. A LAGn (n=1-100) function returns the value of the nth previous execution of the function. It is easy to assume that the LAGn functions return values of the nth previous observation. Using the LAG function to obtain information from previous observation(s) **********************************************************; /* Sample 1: Create a single lag of one variable */ data one; input x; lagonce= lag (x); datalines ; 1 2 3 4 5 ; proc print data=one; title 'Sample1: Single lag of one variable' ; run ; ***************************************************************; /* Sample 2: Create multiple lags of one variable */ data two; input x; lag1 = lag (x); lag2= lag2 (x); datalines ; 1 2 3 4 5 ; proc print data=two; title 'Sample 2: Multiple lags of one variable' ; run ; ...