SAS Programming Professionals,
Did you know that you can maintain the relative order of observations in a BY group during a sort? You can do so by using the EQUALS option on the PROC SORT statement.
This concept is easier to illustrate than it is to describe, so let’s consider the SASHELP.CLASS data set that is already sorted by NAME.
We would like to have the data set sorted by SEX, but keep the NAMEs in alphabetical order within SEX. So, we would code:
proc sort data=sashelp.class out=class equals;
…which nets the following:
Note that the resulting SAS data set is sorted by SEX and that the relative order of NAME (in alphabetical order) has been preserved. The EQUALS option is handy for when you already have an established relative order within observations that you want to maintain after sorting by the variables in your BY statement.
Best of luck in all your SAS endeavors!
(aka Michael A. Raithel)
Excerpt from the book: Did You Know That? Essential Hacks for Clever SAS Programmers
I plan to post each and every one of the hacks in the book to social media on a weekly basis. Please pass them along to colleagues who you know would benefit.