@Fer_ wrote:
Thanks for the code, unfortunately as my dataset is too large, I just could provide a small sample of it. I have a question about the code. My dataset starts from 1990 to 2021, but by running the code the window start is from 1991, which means that for every date the code notice to 252 days before that date. but I want to see 252 days ahead of each date. BHAR for day1= (1+ret day1)(1+ret of day2)....(1+ret day252)- ((1+vwretd day1)(1+vwretd day2)...(1+vwretd day252)).
I am thankful for your help.
If you take a look at the code, as revised, you will see that I provided a WINDOWS_START date, so the BHAR values are look-ahead values. So, examine the WINDOWS_START value, instead of the YEAR value (which will indicate the end of the window).
Thanks for reply, for example for data in 1991, window start is 1990 and year 1991. And my dataset start from 1991 not 1990 by running the code. It seems to me that for 1991, the code starts to see lag 252 days before the date which means that the window start is 1990 (it sees the lag of 1991 not ahead).
@Fer_ wrote:
Thanks for reply, for example for data in 1991, window start is 1990 and year 1991. And my dataset start from 1991 not 1990 by running the code. It seems to me that for 1991, the code starts to see lag 252 days before the date which means that the window start is 1990 (it sees the lag of 1991 not ahead).
The solution provided is really smart and efficient coding. It took me a bit to fully appreciate it and I was grateful to @mkeintz that he kept the code to the relevant minimum and didn't overload it with additional logic like some look-ahead.
If you spend the time to fully appreciate what has been given to you then you'll also see variable window_start. If you want to add the aggregated values at the beginning of your window then just use this variable to merge/join the values from Want to your Have table (on have.cusip=want.cusip and have.year=want.window_start).
Sample data can be mock-up data. It just needs to be representative of your real data.
Some code like below would suffice to create such sample data. If there could be some missing dates in your time series or similar then that's what you would have to mock-up as well for the sample to be representative.
data have;
do cusip='11001','11999';
do year='01jun2020'd to '20feb2021'd;
ret=rand('uniform',-0.09,0.09);
vwretd=rand('uniform',-0.09,0.09);
output;
end;
end;
format year yymmddn8.;
run;
Thanks for the code you sent.
For BHAR 252 days, for 2021 which is the last year in my dataset, I should not have any values after running the codes as I do not have data for 252 days ahead for them. But when I run the code, BHAR have value until the end of my dataset (20211231). That is why I asked question about the codes.
@Fer_ wrote:
Thanks for the code you sent.
For BHAR 252 days, for 2021 which is the last year in my dataset, I should not have any values after running the codes as I do not have data for 252 days ahead for them. But when I run the code, BHAR have value until the end of my dataset (20211231). That is why I asked question about the codes.
WINDOW_START allows you to join the aggregated values to the beginning of the window.
data have;
do cusip='11001','11999';
do year='01jun2020'd to '20feb2021'd;
ret=rand('uniform',-0.09,0.09);
vwretd=rand('uniform',-0.09,0.09);
output;
end;
end;
format year yymmddn8.;
run;
data inter (drop=_:);
set have;
by cusip notsorted;
if first.cusip then call missing(_obs,_sum_log_cusip_ret_plus1,_sum_log_index_ret_plus1);
_obs+1;
_sum_log_cusip_ret_plus1 + log(1+ret) - ifn(_obs>252,lag252(log(1+ret)),0) ;
_sum_log_index_ret_plus1 + log(1+vwretd) - ifn(_obs>252,lag252(log(1+vwretd)),0);
window_start = lag251(year) ;
format window_start yymmddn8.;
if _obs>=252 ;
bhar_252 = exp(_sum_log_cusip_ret_plus1) - exp(_sum_log_index_ret_plus1) ;
run;
data want;
merge have(rename=(year=date)) inter(keep=cusip window_start bhar_252 year rename=(window_start=date year=window_end));
by cusip date;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.