input x $ y $ 9-17 z $ 19-26;
datalines;
apple banana coconut
apple banana coconut
apple blueberry citron
apricot blueberry citron
;
data _null_;
set testfile;
by x y z;
if _N_=1 then put 'Grouped by X Y Z';
put _N_= x= first.x= last.x= first.y= last.y= first.z= last.z= ;
run;
data _null_;
set testfile;
by y x z;
if _N_=1 then put 'Grouped by Y X Z';
put _N_= x= first.x= last.x= first.y= last.y= first.z= last.z= ;
run;
Log 20.1 Partial SAS Log Showing the Results of Processing with BY Variables
Grouped by X Y Z
_N_=1 x=Apple FIRST.x=1 LAST.x=0 FIRST.y=1 LAST.y=0 FIRST.z=1 LAST.z=0
_N_=2 x=Apple FIRST.x=0 LAST.x=0 FIRST.y=0 LAST.y=1 FIRST.z=0 LAST.z=1
_N_=3 x=Apple FIRST.x=0 LAST.x=1 FIRST.y=1 LAST.y=1 FIRST.z=1 LAST.z=1
_N_=4 x=Apricot FIRST.x=1 LAST.x=1 FIRST.y=1 LAST.y=1 FIRST.z=1 LAST.z=1
Grouped by Y X Z
_N_=1 x=Apple FIRST.x=1 LAST.x=0 FIRST.y=1 LAST.y=0 FIRST.z=1 LAST.z=0
_N_=2 x=Apple FIRST.x=0 LAST.x=1 FIRST.y=0 LAST.y=1 FIRST.z=0 LAST.z=1
_N_=3 x=Apple FIRST.x=1 LAST.x=1 FIRST.y=1 LAST.y=0 FIRST.z=1 LAST.z=1
_N_=4 x=Apricot FIRST.x=1 LAST.x=1 FIRST.y=0 LAST.y=1 FIRST.z=1
LAST.z=1
Processing BY-Groups in the DATA Step
Overview
The most common use of BY-group processing in the DATA step is to use SET,
MERGE, MODIFY, or UPDATE with the BY statement to combine two or more SAS
data sets.. (If you use a SET, MERGE, or UPDATE statement with the BY statement,
your observations must be grouped or ordered.) When processing these statements, SAS
reads one observation at a time into the program data vector. With BY-group processing,
SAS selects the observations from the data sets according to the values of the BY
variable or variables. After processing all the observations from one BY group, SAS
expects the next observation to be from the next BY group.
The BY statement modifies the action of the SET, MERGE, MODIFY, or UPDATE
statement by controlling when the values in the program data vector are set to missing.
During BY-group processing, SAS retains the values of variables until it has copied the
last observation that it finds for that BY group in any of the data sets. Without the BY
458 Chapter 20 • BY-Group Processing in the DATA Step