The first. and last. automatic variables generated via the BY statement are just indicators about the current observation related to the preceding and following observations. When first.x=1, it means the current obs has a different value for x than the previous obs - otherwise first.x=0.. Similarly the last.x means the current value of x differs from the next obs (i.e. SAS does a look-ahead for you). Now, it is possible for a record to have both first.x=1 and last.x=1 (the current obs is a singleton) or first.x=0 and last.x=0 (current obs is in the middle of a stream of constant x values).
Now when you have BY a b c d. there will be multiple first. and multiple last. automatic variables. The (slightly) non-intuitive behavior of these is that if, in the middle of a series of constant B values, you have a change in A, both first.A and first.B are set to 1, i.e. whenever a first. variable becomes 1, all the first.'s for variables to its right also become 1. Same with a set of last. variables.
So let me revise my first statement: whenever first.x=1, it means that the current x differs from the preceding x or the current value of some earlier variable in a by list differs from its predecessor.
Editted addition: the above refers to use of by statement in a DATA step.
... View more