BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
mkeintz
PROC Star

@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).

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Fer_
Fluorite | Level 6

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). 

Patrick
Opal | Level 21

@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).

 

Patrick_0-1673742189032.png

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;

 

Fer_
Fluorite | Level 6

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. 

Patrick
Opal | Level 21

@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;

Patrick_0-1673745672409.png

 

 

 

 

Fer_
Fluorite | Level 6

Thank you so much for the codes. I really appreciate your help. @mkeintz 

and thanks to you for the explanation. @Patrick 

 

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 20 replies
  • 2180 views
  • 6 likes
  • 4 in conversation