Published on :
Reporting SASHELP

Grouping Rows in a Report with PROC PRINT

This code is also available in: Deutsch Español Français
Awaiting validation
This script illustrates how to prepare data for a grouped report by first sorting it with PROC SORT by the variable of interest (Age). It then presents two display methods with PROC PRINT: using the BY statement alone to create separate sections, and using the BY and ID statements together to structure the display by replacing the OBS column with the group variable.
Data Analysis

Type : SASHELP


Usage of the standard SASHELP.CLASS dataset.

1 Code Block
PROC SORT Data
Explanation :
Sorting the SASHELP.CLASS dataset by the 'Age' variable. The result is stored in a temporary table 'class_sort'. This step is a prerequisite for using the BY statement in subsequent procedures.
Copied!
1 
2PROC SORT
3DATA=sashelp.class out=class_sort;
4BY Age;
5RUN;
6 
2 Code Block
PROC PRINT
Explanation :
Generation of a report displaying data from 'class_sort'. The 'by Age' statement divides the report into distinct sections for each unique age value.
Copied!
1title "Listing of SASHELP.CLASS Grouped By Age (BY statement)";
2PROC PRINT DATA=class_sort;
3 BY Age;
4RUN;
3 Code Block
PROC PRINT
Explanation :
Generation of a variant of the previous report. Adding the 'id Age' statement modifies the presentation by removing the observation number column (Obs) and using the 'Age' variable to identify the rows.
Copied!
1title "Listing of SASHELP.CLASS Grouped By Age (BY and ID statements)";
2PROC PRINT DATA=class_sort;
3 BY Age;
4 id Age;
5RUN;
6title;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.