Avoiding Unnecessary Sorts: Performing a Two-level Sort Instead of a One-level and a Two-level Sort - SAS Programming

Sorting data sets is resource intensive and should be avoided whenever possible, especially when you are dealing with large data sets.Beginning with Release 6.07, the SAS System is clever enough to keep track of the sort order of a SAS data set and will not perform a requested sort it is not necessary (i.e., if the data set is already properly sorted).

This last set of examples provides several hints that can reduce or eliminate the need to sort your data sets. Suppose you need your data set sorted by YEAR for some procedures, and by YEAR and MONTH for others. Here is the inefficient code to do exactly that:

Example– INEFFICIENT

PROC SORT DATA=TEST;
BY YEAR;
RUN;
PROC anyprocs;
BY YEAR;
(SAS Statements)
RUN;
PROC SORT DATA=TEST;
BY YEAR MONTH;
RUN;
PROC otherprocs;
BY YEAR MONTH;
(SAS Statements)
RUN;

Since you know that you need the data set TEST in YEAR order for some procedures and in YEAR MONTH order for others,you should run PROC SORT only once with YEAR and MONTH as the BY variables. Get it all done at once and avoid the unnecessary second sort. Here is the code:

Example– EFFICIENT

PROC SORT DATA=TEST;
BY YEAR MONTH;
RUN;
PROC anyprocs;
BY YEAR;
(SAS Statements)
RUN;
PROC otherprocs;
BY YEAR MONTH;
(SAS Statements)
RUN;

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

SAS Programming Topics