Chapter Contents

Previous

Next
BY

BY



Controls the operation of a SET, MERGE, MODIFY, or UPDATE statement in the DATA step and sets up special grouping variables

Valid: in a DATA step or a PROC step
Category: File-handling
Type: Declarative


Syntax
Arguments
Details
In a DATA Step
In a PROC Step
BY-Group Processing
With SAS Data Views
Examples
Example 1: Specifying One or More BY Variables
Example 2: Specifying Sort Order
Example 3: BY-Processing with Nonsorted Data
Example 4: Grouping Observations By Using Formatted Values
See Also

Syntax

BY <DESCENDING> <GROUPFORMAT> variable-1
<. . .<DESCENDING> <GROUPFORMAT>
variable-n> <NOTSORTED>;

Arguments

DESCENDING
indicates that the data sets are sorted in descending order by the variable that is specified. DESCENDING means largest to smallest numerically, or reverse alphabetical for character variables.
Restriction: You cannot use the DESCENDING option with indexed data sets because indexes are always stored in ascending order.
Featured in: Specifying Sort Order

GROUPFORMAT
uses the formatted values, instead of the stored values, of the variable when you reference FIRST.variable and LAST.variable in a DATA step.
Restriction: You must sort the observations in a data set based on the value of the BY variables before using GROUPFORMAT in the BY statement.
Restriction: You cannot use the GROUPFORMAT option in a BY statement in a PROC step.
Tip: GROUPFORMAT is useful when you define your own formats to display grouped data.
Tip: Using GROUPFORMAT in the DATA step ensures that BY groups that you use to create a data set match those in PROC steps that report grouped, formatted data.
Interaction: If you also use the NOTSORTED option, you can group the observations in a data set by the formatted value of the BY variables instead of by sorting them.
Comparison: BY-group processing in the DATA step using GROUPFORMAT is the same as BY-group processing with formatted values in SAS procedures.
See Also: Data grouped by formatted values in "BY-Group Processing" in SAS Language Reference: Concepts.
Featured in: Grouping Observations By Using Formatted Values

variable
names each variable by which the data set is sorted or indexed. These variables are referred to as BY variables for the current DATA or PROC step.
Tip: The data set can be sorted or indexed by more than one variable.
Featured in: Specifying One or More BY Variables, Specifying Sort Order, BY-Processing with Nonsorted Data, and Grouping Observations By Using Formatted Values

NOTSORTED
specifies that observations with the same BY value are grouped together but are not necessarily sorted in alphabetical or numeric order.
Restriction: You cannot use NOTSORTED with the MERGE and UPDATE statements.
Tip: The NOTSORTED option can appear anywhere in the BY statement.
Tip: NOTSORTED is useful if you have data that fall into other logical groupings such as chronological order or categories.
Featured in: BY-Processing with Nonsorted Data


Details

In a DATA Step

The BY statement applies only to the SET, MERGE, MODIFY, or UPDATE statement that precedes it in the DATA step, and only one BY statement can accompany each of these statements in a DATA step.

The data sets that are listed in the SET, MERGE, or UPDATE statements must be sorted by the values of the variables that are listed in the BY statement or have an appropriate index. As a default, SAS expects the data sets to be arranged in ascending numeric order or in alphabetical order. The observations can be arranged by

Note:   MODIFY does not require sorted data, but sorting can improve performance.  [cautionend]

In a PROC Step

You can specify the BY statement with some SAS procedures to modify their action. Refer to the individual procedure in SAS Procedures Guide for a discussion of how the BY statement affects processing for SAS procedures.

BY-Group Processing

For a complete explanation of how SAS processes grouped data and of how to prepare your data, see "By-Group Processing" in SAS Language Reference: Concepts.

With SAS Data Views

If you are using SAS data views, refer to the appropriate SAS documentation for your database management system before you use the BY statement.


Examples

Example 1: Specifying One or More BY Variables


Example 2: Specifying Sort Order


Example 3: BY-Processing with Nonsorted Data

Observations are ordered by the name of the month in which the expenses were accrued:

     by month notsorted;

Example 4: Grouping Observations By Using Formatted Values

Use PROC FORMAT to create the user-defined format RANGE:

proc format;
   value range 1-2='LOW' 3-4='MEDIUM' 5-6='HIGH';
run;
Create a data set ordered by formatted values. TEST must already be ordered by the stored values of SCORE. BY GROUPFORMAT causes TEST to be processed in BY groups based on the formatted values of SCORE and creates NEWTEST in this order:
data newtest;
   set test;
   format score range.;
   by groupformat score;
run;
PROC PRINT uses the format RANGE to write the values of SCORE:
proc print data=newtest;
   var name score
   by score;
   format score range.;
run;

See Also

Statements:

MERGE
MODIFY
SET
UPDATE


Chapter Contents

Previous

Next

Top of Page

Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.