Figure 13.7 Vendor Totals in the VENDORDETAILS Data Set
Using a Value in a Later Observation
A further requirement of Tradewinds Travel is a separate SAS data set that contains the
tour that generated the most revenue. (The revenue total equals the price of the tour
multiplied by the number of bookings.) One method of creating the new data set might
be to follow these three steps:
1. Calculate the revenue in a DATA step.
2. Sort the data set in descending order by the revenue.
3. Use another DATA step with the OBS= data set option to write that observation.
A more efficient method compares the revenue from all observations in a single DATA
step. SAS can retain a value from the current observation to use in future observations.
When the processing of the DATA step reaches the next observation, the held value
represents information from the previous observation.
The RETAIN statement causes a variable that is created in the DATA step to retain its
value from the current observation into the next observation. The variable is not set to
missing at the beginning of each iteration of the DATA step. RETAIN is a declarative
statement, not an executable statement. This statement has the following form:
RETAIN variable-1 < . . . variable-n>;
To compare the Revenue value in one observation to the Revenue value in the next
observation, create a retained variable named HoldRevenue and assign the value of the
current Revenue variable to it. In the next observation, the HoldRevenue variable
contains the Revenue value from the previous observation, and its value can be
compared to that of Revenue in the current observation.
To see how the RETAIN statement works, look at the next example. The following
DATA step writes observations to data set TEMP before SAS assigns the current revenue
Revenue=LandCost * NumberOfBookings;
Using a Value in a Later Observation 209