Chapter 20
BY-Group Processing in the
DATA Step
Definitions for BY-Group Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
Syntax for BY-Group Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
Understanding BY Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
BY Groups with a Single BY Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
BY Groups with Multiple BY Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
Invoking BY-Group Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430
Determining Whether the Data Requires Preprocessing for
BY-Group Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
Preprocessing Input Data for BY-Group Processing . . . . . . . . . . . . . . . . . . . . . . . 431
Sorting Observations for BY-Group Processing . . . . . . . . . . . . . . . . . . . . . . . . . . 431
Indexing for BY-Group Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432
How the DATA Step Identifies BY Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432
Processing Observations in a BY Group . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432
Using a Name Literal as the BY-Group Variable . . . . . . . . . . . . . . . . . . . . . . . . . . 432
How SAS Determines FIRST.variable and LAST.variable . . . . . . . . . . . . . . . . . . 433
Example 1: Grouping Observations by State, City, ZIP Code, and Street . . . . . . . 433
Example 2: Grouping Observations by City, State, ZIP Code, and Street . . . . . . . 435
Example 3: A Change Affecting FIRST.variable . . . . . . . . . . . . . . . . . . . . . . . . . . 435
Processing BY-Groups in the DATA Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
Processing BY-Groups Conditionally . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437
Data Not in Alphabetic or Numeric Order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438
Data Grouped by Formatted Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
Example 1: Using GROUPFORMAT with Formats . . . . . . . . . . . . . . . . . . . . . . . 439
Example 2: Using GROUPFORMAT with Formats . . . . . . . . . . . . . . . . . . . . . . . 439
Definitions for BY-Group Processing
BY-group processing
is a method of processing observations from one or more SAS data sets that are
grouped or ordered by values of one or more common variables. The most common
use of BY-group processing in the DATA step is to combine two or more SAS data
sets. To do this, you use the BY statement with a SET, MERGE, MODIFY, or
UPDATE statement.
427

Get SAS 9.4 Language Reference, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.