I encourage you to learn the SAS programming language, as much through help from this list, but also with your own desk-checking and DOC-review. You don't need LAG and if you were to add a PUTLOG _ALL_ ; statement at various points in your DATA step coding logic, you would learn/understand why not. Your SAS code that assigns PROD_NUMBER based on FIRST.<break_varname> tests is sufficient -- meaning no LAG required. And you do not need to test each BY variable -- only the "significant" variable in the list that would influence your DO/END or otherwise some SAS assignment logic -- in your case that is the PROD_NUMBER assignment, either as a first-occurrence value of 1 or to increment. Please take the opportunity and review the available SAS DOC and examples. No doubt you will be given the answer at some point, if you continue to throw out code-pieces not thoroughly explored / tested on our own accord. Also, the location of the assignment statement that uses LAG function has no bearing on the location of the BY statement at all.
... View more