LAGS

If you want to calculate a period of time between one event and another, then you should use the LAG function. This function helps to return values from a queue. The syntax has two elements:

Let's look at lagging the variables:

Data Lag;
Set Decimal (Keep = Round);
Lag1 = Lag1 (Round);
Lag2 = Lag2 (Round);
Run;

This will result in the following output:

By specifying a single lag, we have started the LAG1 variable values from the second observation. The first observation has been set to missing. In the case of LAG2, we have specified two lags. This is the simplest form of lagging. How can we create lags if we have scores of two different classes in the same dataset? This can be done by leveraging BY GROUP processing.

Before going further into discussing lags, it would be beneficial to explore the concept of the FIRST. and LAST. variables, which are automatically created with BY GROUP processing. Prior to using BY GROUP, we should ensure that the dataset has been sorted by the variable being referred to in BY GROUP.

The concept of FIRST. and LAST. can be explored by running the following code:

Data Lag_ByGroup;
Set Class_Scores;
By Class;
Lag1 = Lag1 (Score);
IF FIRST.Class THEN DO;
Lag1 = .;
END;
ELSE DO;
Lag1 = Lag1;
END;
RUN;

This will result in the following output:

We have lagged the SCORE variable by one observation for each instance of the CLASS variable. Moreover, we have used the IF-THEN conditions to set the first of each observation as missing. If you want, you can now find the difference in the observation from its subsequent observation by finding the difference between LAG1 and SCORE. You can create the preceding dataset in multiple ways using different SAS functions.