If YEAR designates fiscal year, then you want to accumulate up through the 3rd record following the last month record for a given year, as in:
data bhret;
set have;
by gvkey year;
bhret+log(1+ret);
if lag3(last.year);
bhret=exp(bhret)-1;
output;
call missing bhret;
run;
Note this assumes that there are no missing monthly returns.
But maybe (you're using Compustat data right?) you have a variable FYR indicating the month number for the end of fiscal year (i.e. if FYR=6 then fiscal year ends in June). If so, then
data bhret;
set have;
by gvkey;
bhret+log(1+ret);
if lag3(month)=fyr;
bhret=exp(bhret)-1;
output;
call missing bhret;
run;
This also assume no missing months.
A couple notes:
The "bhret+log(1+ret);" is a "summing statement", which tells sas to retain the resulting value in variable bhret. I.e. don't automatically reset bhret to missing.
The "if lag3(...)" statement is a subsetting if, keeping only those records in which the 3rd preceding record satisfy the specified condition.
One a record is output, the programmer now needs to reset the bhret to missing, so every record has only results built on record following the previous annual return.
... View more