Money=LandCost * NumberOfBookings;
output tourdetails;
The portion of the DATA step that builds TOURDETAILS is now complete.
Writing Totals to Separate Data Sets
Because observations remain in the program data vector after an OUTPUT statement
executes, you can continue using the observations in programming statements. The rest
of the DATA step creates information for the VENDORDETAILS data set.
Use the FIRST.Vendor variable to determine when SAS is processing the first
observation in each group.
Then set the Sum variables VendorBookings and VendorMoney to 0 in that observation.
VendorBookings accumulates the total number the bookings for each vendor, and
VendorMoney accumulates the total costs. Add the following statements to the DATA
step:
if First.Vendor then
do;
VendorBookings=0;
VendorMoney=0;
end;
VendorBookings + NumberOfBookings;
VendorMoney + Money;
Note: The program uses a DO group. Using DO groups enables the program to evaluate
a condition once and take more than one action as a result. For more information
about DO groups, see “Performing More Than One Action in an IF-THEN
Statement” on page 217.
The last observation in each BY–group contains the totals for that vendor. Therefore, use
the following statement to output the last observation to the data set
VENDORDETAILS:
if Last.Vendor then output vendordetails;
As a final step, use KEEP= and DROP= data set options to remove extraneous variables
from the two data sets so that each data set has just the variables that are wanted.
data tourdetails(drop=VendorBookings VendorMoney)
vendordetails(keep=Vendor VendorBookings VendorMoney);
The Program
The following is the complete program that creates the VENDORDETAILS and
TOURDETAILS data sets:
proc sort data=mylib.tourrevenue out=mylib.sorttour;
by Vendor;
run;
data tourdetails(drop=VendorBookings VendorMoney)
vendordetails(keep=Vendor VendorBookings VendorMoney);
set mylib.sorttour;
by Vendor;
Money=LandCost * NumberOfBookings;
output tourdetails;
Writing to Separate Data Sets 207