Hi,
I would like to compute annual buy and hold return using monthly return like the following.
annual ruturn = (1+January return)(1+February return)(1+March return)-----(1+December return) - 1
The data structure I have is as follows:
gvkey year month monthly return
00001 2000 1 0.002
00001 2000 2 0.001
00001 2000 3 0.003
Do you have a simple SAS code I can refer using retain statement (without using macro)?
Thank you for your help
This should do what you want but the value is only going to be for the entire year if the last value in each year corresponds to Dec.
I assumed that since you have a key value that you wanted this for each key and that the data is sorted by gvkey year and month.
If you only want the final value add at the end of the datastep: If Last.year then output;
data want;
set have;
by gvkey year;
retain AnReturn;
if first.year then AnReturn= (1+ MonthReturn);
else AnReturn= AnReturn*(1+MonthReturn);
run;
However I suspect you are going to run into precision problems (under flow). you may need to consider an approach with logs or shift ot integer arithmetic and shift back.
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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.