O'Reilly logo

SAS 9.4 Language Reference, 6th Edition by SAS Institute

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

BY variable
names a variable or variables by which the data set is sorted or indexed. All data sets
must be ordered or indexed on the values of the BY variable if you use the SET,
MERGE, or UPDATE statements. If you use MODIFY, data does not need to be
ordered. However, your program might run more efficiently with ordered data. All
data sets that are being combined must include one or more BY variables. The
position of the BY variable in the observations does not matter.
BY value
is the value or formatted value of the BY variable.
BY group
includes all observations with the same BY value. If you use more than one variable
in a BY statement, a BY group is a group of observations with the same combination
of values for these variables. Each BY group has a unique combination of values for
the variables.
FIRST.variable and LAST.variable
are variables that SAS creates for each BY variable. SAS sets FIRST.variable when
it is processing the first observation in a BY group, and sets LAST.variable when it
is processing the last observation in a BY group. These assignments enable you to
take different actions, based on whether processing is starting for a new BY group or
ending for a BY group. For more information, see “How the DATA Step Identifies
BY Groups” on page 454.
For more information about BY-Group processing, see Chapter 21, “Reading,
Combining, and Modifying SAS Data Sets,” on page 465. See also Combining and
Modifying SAS Data Sets: Examples.
Syntax for BY-Group Processing
Use one of the following forms for BY-group processing:
BY variable(s);
BY <DESCENDING> variable(s) <NOTSORTED> <GROUPFORMAT>;
where
DESCENDING
indicates that the data sets are sorted in descending order (largest to smallest) by the
variable that is specified. If you have more that one variable in the BY group,
DESCENDING applies only to the variable that immediately follows it.
variable
names each variable by which the data set is sorted or indexed.
Note: All data sets must be ordered or indexed on the values of the BY variable if
you process them using the SET, MERGE, or UPDATE statements. If you use the
MODIFY statement, your data does not need to be ordered. However, your
program might run more efficiently with ordered data. All data sets that are being
combined must include the BY variable or variables. The position of the BY
variable in the observations does not matter.
NOTSORTED
specifies that observations with the same BY value are grouped together but are not
necessarily stored in alphabetical or numeric order.
450 Chapter 20 BY-Group Processing in the DATA Step

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required