How to Use the LAG Function in SAS (With Examples)

The LAG function in SAS is a useful tool for analyzing data and creating variables in a dataset. The LAG function allows users to retrieve the value of a variable from the previous observation in a dataset. This can be used to compare values across observations, detect trends, or create new variables. Examples of using the LAG function include comparing sales from one month to the previous month, tracking changes in an index, or creating a lagged variable of another variable in the dataset.


You can use the LAG function in SAS to retrieve lagged values of some variable.

This function uses the following basic syntax:

lag1_value = lag(value);

By default, lag finds the previous value of some variable.

However, you can use lag2, lag3, lagn, etc. to calculate the 2-lagged, 3-lagged, n-lagged, etc. values of some variable.

The following examples show how to use the lag function in practice.

Example 1: Calculated Lagged Values for Some Variable

Suppose we have the following dataset in SAS that shows the total sales made by some store on consecutive days:

/*create dataset*/
data original_data;
    input day $ sales;
    datalines;
1 14
2 19
3 22
4 20
5 16
6 26
7 40
8 43
9 29
10 30
11 35
12 33
;
run;

/*view dataset*/
proc print data=my_data;

The following code shows how to calculate the value for sales lagged by 1, 2, and 3 days:

/*create new dataset that shows lagged values of sales*/
data new_data;
    set original_data;
    lag1_sales = lag(sales);
    lag2_sales = lag2(sales);
    lag3_sales = lag3(sales);
run;

/*view new dataset*/
proc print data=new_data;

sas lag function example

The three new columns (lag1_sales, lag2_sales, lag3_sales) show the sales lagged by one, two, and three days, respectively.

Example 2: Calculated Lagged Values by Group

Suppose we have the following dataset in SAS that shows the total sales made by two stores during consecutive days:

/*create dataset*/
data original_data;
    input store $ sales;
    datalines;
A 14
A 19
A 22
A 20
A 16
A 26
B 40
B 43
B 29
B 30
B 35
B 33
;
run;

/*view dataset*/
proc print data=original_data;

We can use the following code to calculate the 1-day lagged sales values by store:

/*create new dataset that shows lagged values of sales by store*/
data new_data;
	set original_data;
	by store;
	lag1_sales = lag(sales);
	if first.store then lag1_sales = .;
run;

/*view new dataset*/
proc print data=new_data;

SAS lag by group

The values in the lag1_sales column show the 1-day lagged sales values for each store.

Notice that the value for lag1_sales in row 7 is empty because the 1-day lagged value for that row represents a sales value for a different store.

The following tutorials explain how to perform other common tasks in SAS:

x