logo_learn_stats

Find out how to Significance the LAG Serve as in SAS (With Examples)

Posted on
banner 336x280

You’ll be able to usefulness the LAG serve as in SAS to retrieve lagged values of a few variable.

This serve as makes use of refer to plain syntax:

banner 468x60
lag1_value = lag(price);

Via default, lag reveals the former price of a few variable.

Then again, you’ll usefulness lag2, lag3, lagn, and so on. to calculate the 2-lagged, 3-lagged, n-lagged, and so on. values of a few variable.

Please see examples display the way to usefulness the lag serve as in apply.

Instance 1: Calculated Lagged Values for Some Variable

Assume we’ve refer to dataset in SAS that displays the overall gross sales made via some gather on consecutive days:

/*develop dataset*/
knowledge original_data;
    enter generation $ gross 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 knowledge=my_data;

Please see code displays the way to calculate the price for gross sales lagged via 1, 2, and three days:

/*develop unutilized dataset that displays lagged values of gross sales*/
knowledge new_data;
    i'm ready original_data;
    lag1_sales = lag(gross sales);
    lag2_sales = lag2(gross sales);
    lag3_sales = lag3(gross sales);
run;

/*view unutilized dataset*/
proc print knowledge=new_data;

sas lag function example

The 3 unutilized columns (lag1_sales, lag2_sales, lag3_sales) display the gross sales lagged via one, two, and 3 days, respectively.

Instance 2: Calculated Lagged Values via Crew

Assume we’ve refer to dataset in SAS that displays the overall gross sales made via two retail outlets all through consecutive days:

/*develop dataset*/
knowledge original_data;
    enter gather $ gross 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 knowledge=original_data;

We will usefulness refer to code to calculate the 1-day lagged gross sales values via gather:

/*develop unutilized dataset that displays lagged values of gross sales via gather*/
knowledge new_data;
	i'm ready original_data;
	via gather;
	lag1_sales = lag(gross sales);
	if first.gather upcoming lag1_sales = .;
run;

/*view unutilized dataset*/
proc print knowledge=new_data;

SAS lag by group

The values within the lag1_sales column display the 1-day lagged gross sales values for every gather.

Understand that the price for lag1_sales in row 7 is emptied for the reason that 1-day lagged price for that row represents a gross sales price for a special gather.

Alternative Assets

Please see tutorials give an explanation for the way to carry out alternative usual duties in SAS:

Find out how to Normalize Knowledge in SAS
Find out how to Take away Duplicates in SAS
Find out how to Change Lacking Values with 0 in SAS

banner 336x280

Leave a Reply

Your email address will not be published. Required fields are marked *