I'm not familiar with the constructs, but from a quick search I gather you want to if the current row contains the first or last occurrence of a particular variable's value in a data set sorted by that variable. I think that would be:
if (:var != Lag(:var), ...); // first
if (:var != Lag(:var, -1), ...); // last
Wouldn't the equivalent in a matrix language just be indexing to the first value or the last value?