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!
proc sort data=sashelp.class out=class_sort;
by Age;
run;
1
2
PROC SORT
3
DATA=sashelp.class out=class_sort;
4
BY Age;
5
RUN;
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!
title "Listing of SASHELP.CLASS Grouped By Age (BY statement)";
proc print data=class_sort;
by Age;
run;
1
title "Listing of SASHELP.CLASS Grouped By Age (BY statement)";
2
PROC PRINTDATA=class_sort;
3
BY Age;
4
RUN;
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!
title "Listing of SASHELP.CLASS Grouped By Age (BY and ID statements)";
proc print data=class_sort;
by Age;
id Age;
run;
title;
1
title "Listing of SASHELP.CLASS Grouped By Age (BY and ID statements)";
2
PROC PRINTDATA=class_sort;
3
BY Age;
4
id Age;
5
RUN;
6
title;
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.
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.