** Let’s say you have daily (trading-date) data with variables DATE and PRC in data set HAVE, and you want, for each day, NEXT_PRC, the price on the following trading day.
But while there is a LAG function in SAS, there is no LEAD function. You have to “read ahead” using a different technique, like this:
data want;
set have end=end_of_have;
if end_of_have then next_prc=.;
else set have (firstobs=2 keep=prc rename=(prc=next_prc));
run;
The technique is to use two SET statements. One does not affect the other - they are separate “streams” of data. The main difference is that the second SET statement has a “firstobs=2” data set parameter - which tells SAS to start that stream at the second observation. That is, to “read ahead”. So while the first set reads observation I, the second reads observation I+1.
The second set statement is told to read in only the PRC variable (“keep=prc”), but of course if it keeps that name, PRC from the second SET would overwrite the PRC from the first. So it is also told to rename prc to NEXT_PRC. Result: 3 variables: DATE, PRC and NEXT_PRC.
Now what is this “end_of_have” stuff? The “end=end_of_have” option on the first SET statement says to make a (temporary) dummy variable which is a 1 when the record-in-hand is the last record in HAVE (otherwise it’s a zero). That’s neccessary because when the first SET HAVE reads the last case, the second SET HAVE would attempt to read beyond the last case and SAS would instantly stop, without writing out a record.
That is, if HAVE had 100 records, WANT would have only 99. So the “else if” above means the second SET runs only when the first has not hit the end, and therefore will not prematurelhy end the data step. As a result WANT will have 100 observations.
The only other note to make is that when the last case is in hand, you want NEXT_PRC to be a missing value. Hence the “if end_of_have then next_prc=.;”. Without this, the last case would have a NEXT_PRC value carried over from the “penultimate” record.
In my next entry I’ll deal with leads in the presence of BY groups.