Interesting stuff, thanks! I’d like to add a reference to look-ahead techniques where two do-loops read a single sorted dataset stopping at each level of the by-groups. Here is a paper on the topic: http://support.sas.com/resources/papers/proceedings12/052-2012.pdf This is a technique I learned early in my sales career and have used many times!