DATA step failing to calculate first value

I have a very simple bit of code below that I setup to calculate percentage change between two periods.

The calculations appear fine except for the first valid record.

The data set is arranged so that there are multiple stock tickers in time series. The data is sorted by ticker and first_of_month date.

I check to see if the ticker is equal to the prior record ticker. If it is, then I calculate the percentage change in price. If it is not, that indicates one ticker series has ended and another begun so I do not calculate the change as there is no prior period for the new ticker. For debugging purposes, I fill in the price_change field with 666

As you can see from the output snippet, the first record appripriately gets a 666 but then the second record fails to calcualte the price_change at all. I have included other debugging variables that indicate while the code sees the prior record with the same ticker, it fails to see the lagged price and does not perform the calculation.

This failure only happens on the first two records of the data set. When the ticker changes later on in the data set, everyone performs fine. I also checked this on several different datasets and the behavior is the same - the first ticker misses out on the first opportunity for a price_change calculation but everything else works fine.

Re: DATA step failing to calculate first value

You haven't explained what you are trying to do but I expect that you want to run the LAG functions on every observation instead of conditionally running it only on some of the observations. The LAG() function does not take the value from the previous observation. It takes it from the stack that is build from the values that are pushed down with in runs. Skipping some observations prevents those values from making into the lag stack to be later retrieved. Try this little test.

Re: DATA step failing to calculate first value

You haven't explained what you are trying to do but I expect that you want to run the LAG functions on every observation instead of conditionally running it only on some of the observations. The LAG() function does not take the value from the previous observation. It takes it from the stack that is build from the values that are pushed down with in runs. Skipping some observations prevents those values from making into the lag stack to be later retrieved. Try this little test.

Re: DATA step failing to calculate first value

Thanks guys - I understand it much better now based on your explanations.

As a bonus, I learned about first.var, CALL MISSING and proper use of the BY statement!

Just to make sure I understand this completely, is the stack reset when the next ticker arrives due to the BY statement? That's what I want and the code seems to behave that wya but thought I should verify.