MEANS Procedure

Example 3: Using the BY Statement with Class Variables

Features:

PROC MEANS statement option: statistic keywords

BY statement

CLASS statement

Other features:

SORT procedure

Data set: GRADE

Details

This example does the following:
  • separates the analysis for the combination of class variables within BY values
  • shows the sort order requirement for the BY statement
  • calculates the minimum, maximum, and median

Program

options nodate pageno=1 linesize=80 pagesize=60;
proc sort data=Grade out=GradeBySection;
   by section;
run;
proc means data=GradeBySection min max median;
   by Section;
   var Score;
   class Status Year;
   title1 'Final Exam Scores for Student Status and Year of Graduation';
   title2 ' Within Each Section';
run;

Program Description

Set the SAS system options. The NODATE option suppresses the display of the date and time in the output. PAGENO= specifies the starting page number. LINESIZE= specifies the output line length, and PAGESIZE= specifies the number of lines on an output page.
options nodate pageno=1 linesize=80 pagesize=60;
Sort the GRADE data set. PROC SORT sorts the observations by the variable Section. Sorting is required in order to use Section as a BY variable in the PROC MEANS step.
proc sort data=Grade out=GradeBySection;
   by section;
run;
Specify the analyses. The statistic keywords specify the statistics and their order in the output.
proc means data=GradeBySection min max median;
Divide the data set into BY groups. The BY statement produces a separate analysis for each value of Section.
   by Section;
Specify the analysis variable. The VAR statement specifies that PROC MEANS calculate statistics on the Score variable.
   var Score;
Specify subgroups for the analysis. The CLASS statement separates the analysis by the values of Status and Year. Because there is no TYPES statement in this program, analyses are performed for each subgroup, within each BY group.
   class Status Year;
Specify the titles.
   title1 'Final Exam Scores for Student Status and Year of Graduation';
   title2 ' Within Each Section';
run;

Output

Final Exam Scores
Final Exam Grades for Student Status and Year of GraduationWithin Each Section