Suggest you share whatever code piece you may have tried that is not working?
And, suggesting also that you try adding this code line so you can tell when you have FIRST. and/or LAST. (where "nn" can be unique and you can insert multiples at various DATA step locations to see effect):
PUTLOG '>DIAG-nn>' / _ALL_;
For your interest, I believe that BY GROUP processing, while having the proper sorted file and also having the BY statement variable optimized, then you can identify unique conditions / characteristics for your data and set a flag accordingly.
It will work-out as long as you find a suitable combination and sequence for your BY variables, then test those conditions while using the PUTLOG example.
Scott Barry
SBBWorks, Inc.
Suggested Google advanced search argument, this topic / post:
by group processing site:sas.com