BY Statement

Creates a separate graph for each BY group.
Used by: SGDESIGN, SGPANEL, SGPLOT, SGRENDER, and SGSCATTER procedures

Syntax

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

Required Argument

variable
specifies the variable that the procedure uses to form BY groups. You can specify more than one variable. By default, observations in the data set must either be sorted in ascending order by all the variables that you specify, or be indexed appropriately.

Optional Arguments

DESCENDING
specifies that the data set is sorted in descending order by the specified variable. This option affects only the variable that immediately follows it—you must specify the DESCENDING option before each variable that is sorted in descending order. For example, the following code specifies a BY group that uses two variables that are both sorted in descending order:
by descending variable1 descending variable2;
NOTSORTED
specifies that the observations in the data set that have the same BY values are grouped together, but are not necessarily sorted in alphabetical or numeric order. For example, the observations might be sorted in chronological order using a date format such as DDMMYY.
The NOTSORTED option applies to all of the variables in the BY statement. You can specify the NOTSORTED option anywhere within the BY statement.
The requirement for ordering or indexing observations according to the values of BY variables is suspended when you use the NOTSORTED option. In fact, the procedure does not use an index if you specify the NOTSORTED option. For the NOTSORTED option, the procedure defines a BY group as a set of contiguous observations that have the same values for all BY variables. If observations that have the same value for the BY variables are not contiguous, then the procedure treats each new value that it encounters as the first observation in a new BY group. The procedure then creates a graph for that value.
Restriction:The NOTSORTED option is not supported by the SGPANEL procedure.

Details

Preparing Data for BY-Group Processing

Unless you specify the NOTSORTED or DESCENDING options, observations in the input data set must be in ascending numeric or alphabetic order. To prepare the data set, sort it with the SORT procedure using the same BY statement that you plan to use in the target procedure. Alternatively, you can create an appropriate index on the BY variables. For more information about indexes, see Understanding SAS Indexes in SAS Language Reference: Concepts.
If the procedure encounters an observation that is out of order, an error message is generated.
If you need to group data in some other order, such as chronological order, you can still use BY-group processing. To do so, process the data so that observations are arranged in contiguous groups that have the same BY-variable values and specify the NOTSORTED option in the BY statement.

Controlling BY Lines

By default, the BY statement prints a BY line above each graph that contains the variable name followed by an equal sign and the variable value. For example, if you specify BY SITE in the procedure, the default heading when the value of SITE is London would be SITE=London.
To suppress the BY line, use the NOBYLINE option in an OPTION statement.
To display only the BY value, use the NOBYLINE option and then use the #BYVAL1 substitution in a TITLE statement.

Using the BY Statement with the SGPLOT Procedure

You can use the UNIFORM= option in the PROC SGPLOT statement to produce the same group markers, the same axis scaling, or both for all graphs in a BY group. By default, the group markers and axis scales might vary from graph to graph.

Using the BY Statement with the TITLE and FOOTNOTE Statements

The TITLE and FOOTNOTE statements can automatically include the BY variable name, BY variable values, or BY lines in the text that they produce. To insert BY variable information into the text strings used by these statements, use the #BYVAR, #BYVAL, and #BYLINE substitution options. For more information, see the description for the text-string argument in TITLE and FOOTNOTE Statements .