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


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.

Additional Resources

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

How to Normalize Data in SAS
How to Remove Duplicates in SAS
How to Replace Missing Values with Zero in SAS

Leave a Reply

Your email address will not be published.