How SAS Determines FIRST.variable and LAST.variable
When an observation is the first in a BY group, SAS sets the value of FIRST.variable to
1 for the variable whose value changed, as well as for all of the variables that follow in
the BY statement. For all other observations in the BY group, the value of
FIRST.variable is 0. Likewise, if the observation is the last in a BY group, SAS sets the
value of LAST.variable to 1 for the variable whose value changes on the next
observation, as well as for all of the variables that follow in the BY statement. For all
other observations in the BY group, the value of LAST.variable is 0. For the last
observation in a data set, the value of all LAST.variable variables are set to 1.
Note: See “SAS Name Literals” on page 31 for more information about SAS name
literals.
Example 1: Grouping Observations by State, City, ZIP code, and
Street
This example shows how SAS uses the FIRST.variable and LAST.variable to flag the
beginning and end of four BY groups: State, City, ZipCode, and Street. Six temporary
variables are created within the program data vector. These variables can be used during
the DATA step, but they do not become variables in the new data set.
In the figure that follows, observations in the SAS data set are arranged in an order that
can be used with this BY statement:
by State City ZipCode;
SAS creates the following temporary variables: FIRST.State, LAST.State, FIRST.City,
LAST.City, FIRST.ZipCode, and LAST.ZipCode.
options pageno=1 nodate linesize=80 pagesize=60;
data testfile;
input State $ ZipCode $ City $ Street $ 19-33;
datalines;
AZ 85730 Tucson Gleeson Place
FL 33133 Miami Rice Street
FL 33133 Miami Thomas Avenue
FL 33133 Miami Surrey Drive
FL 33146 Miami Nervia Street
FL 33146 Miami Corsica Street
OH 45056 Miami Myrtle Street
;
data test2;
set testfile;
by State City ZipCode;
put _N_= state= first.state= last.state= first.city= last.city=
first.ZipCode= last.ZipCode= ;
run;
NOTE: PROCEDURE PRINTTO used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
79 options pageno=1 nodate linesize=80 pagesize=60;
80 data testfile;
81 input State $ ZipCode $ City $ Street $ 19-33;
How the DATA Step Identifies BY Groups 455