Chapter Contents


Statements with the Same Function in Multiple Procedures


Orders the output according to the BY groups.

See also: Creating Titles That Contain BY-Group Information

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

Required Arguments

specifies the variable that the procedure uses to form BY groups. You can specify more than one variable. If you do not use the NOTSORTED option in the BY statement, the observations in the data set must either be sorted by all the variables that you specify, or they must be indexed appropriately. Variables in a BY statement are called BY variables.


specifies that the observations are sorted in descending order by the variable that immediately follows the word DESCENDING in the BY statement.

specifies that observations are not necessarily sorted in alphabetic or numeric order. The observations are grouped in another way, for example, chronological order.

The requirement for ordering or indexing observations according to the values of BY variables is suspended for BY-group processing when you use the NOTSORTED option. In fact, the procedure does not use an index if you specify NOTSORTED. The procedure defines a BY group as a set of contiguous observations that have the same values for all BY variables. If observations with the same values for the BY variables are not contiguous, the procedure treats each contiguous set as a separate BY group.

Note:   You cannot use the NOTSORTED option in a PROC SORT step.  [cautionend]

Note:   You cannot use the GROUPFORMAT option, which is available in the BY statement in a DATA step, in a BY statement in any PROC step.  [cautionend]

BY-Group Processing
Procedures create output for each BY group. For example, the elementary statistics procedures and the scoring procedures perform separate analyses for each BY group. The reporting procedures produce a report for each BY group.

Note:   All base procedures except PROC PRINT process BY groups completely independently. PROC PRINT can report the number of observations in each BY group as well as the number of observations in all BY groups. Similarly, PROC PRINT can sum numeric variables in each BY group and across all BY groups.  [cautionend]

You can use only one BY statement in each PROC step. When you use a BY statement, the procedure expects an input data set that is sorted by the order of the BY variables or one that has an appropriate index. If your input data set does not meet these criteria, an error occurs. Either sort it with the SORT procedure or create an appropriate index on the BY variables.

Depending on the order of your data, you may need to use the NOTSORTED or DESCENDING option in the BY statement in the PROC step.

For more information on

Procedures That Support the BY Statement

CHART REPORT (nonwindowing environment only)
COMPARE SORT (required)

Note:   In the SORT procedure, the BY statement specifies how to sort the data. With the other procedures, the BY statement specifies how the data are currently sorted.  [cautionend]

This example uses a BY statement in a PROC PRINT step. There is output for each value of the BY variable, Year. The DEBATE data set is created in Example: Temporarily Dissociating a Format from a Variable .

options nodate pageno=1 linesize=64 
proc print data=debate noobs;
   by year;
   title 'Printing of Team Members';
   title2 'by Year';
[HTML Output]  [Listing Output]

Chapter Contents



Top of Page

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