Avoiding Unnecessary Sorts: Using a CLASS Statement When Possible - SAS Programming

It is usually preferable to use a CLASS statement when running PROC MEANS (or PROC SUMMARY) instead of pre-sorting your data set with a SORT procedure and then using a BY statement.However, if your data set is already sorted in the proper order, and you only need to examine those statistics that you would get when using a CLASS statement and the NWAY option (highest level _TYPE_ groups), a BY statement without a SORT procedure is preferable to CLASS.

A CLASS statement uses more memory than a BY statement, with the amount of memory dependent on the number of CLASS variables as well as the number of levels for each CLASS variable.Running out of memory when using a CLASS statement may necessitate the use of the PROC SORT-BY statement combination.

Assume you have a data set TEST that is not sorted by the variable you wish to use for subgroup analyses,YEAR. Here is the inefficient method of deriving mean values for COST by YEAR.

Example – INEFFICIENT

PROC SORT DATA=TEST;
BY YEAR;
RUN;
PROC MEANS NOPRINT DATA=TEST;
BY YEAR;
VAR COST;
OUTPUT OUT=MEANS MEAN=;
RUN;

Now here is the preferred method:

Example – EFFICIENT

PROC MEANS NOPRINT NWAY DATA=TEST;
CLASS YEAR;
VAR COST;
OUTPUT OUT=MEANS MEAN=;
RUN;

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

SAS Programming Topics