Example 8.13 Obtaining the Previous Value of a Variable within a BY Group

Goal

Create variables that have the values of a variable from previous observations. Process the data set in BY groups so that the process of obtaining lagged values starts over with each new BY group.

This example uses the same input data set as Example 8.14.

Example Features

Featured StepDATA step
Featured Step Options and StatementsLAGn function
Related TechniqueDATA step, BY and RETAIN statements, FIRST. and LAST. automatic variables
A Closer LookMore on the DATA Step in This Example

Input Data Set

Data set PRODUCTION records the number of units of an item made on several days at four plant locations.

                  PRODUCTION
                                      units_
  Obs      plant         proddate      made 1 Aux Plant 03/02/2009 ...

Get Combining and Modifying SAS® Data Sets: Examples Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.