|Valid:||in a DATA step or a PROC step|
|In a DATA Step|
|In a PROC Step|
|With SAS Data Views|
|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|
<. . .<DESCENDING> <GROUPFORMAT>
|Restriction:||You cannot use the DESCENDING option with indexed data sets because indexes are always stored in ascending order.|
|Featured in:||Specifying Sort Order|
|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|
|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|
|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|
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.
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.
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.
If you are using SAS data views, refer to the appropriate SAS documentation for your database management system before you use the BY statement.
by city zipcode;
by salesrep descending jansales;
by descending bedrooms descending price;
Observations are ordered by the name of the month in which the expenses were accrued:
by month notsorted;
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;
Top of Page
Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.