In reply to Ksharp.

Yes. I am not recommending that you read the data multiple times for this example. In the paragraph about "bookkeeping" I say that there are ways to analyze the BY groups without reading the data multiple times. I link to the UNIQUEBY technique, which is essentially what your code does. The point of the current example is to show that the right hand side of a WHERE clause can be a dynamic variable that changes during the course of the program.