<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Calculate stock buy and hold with both firm return and market benchmark in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculate-stock-buy-and-hold-with-both-firm-return-and-market/m-p/523825#M142373</link>
    <description>&lt;P&gt;I do not have SAS on this computer, so I cannot test, but I think something like this should work:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set bhar;
  by ticker;
  array loglret (0:12) 8 _temporary_;
  array logbench(0:12) 8 _temporary_;
  if first.ticker then
    call missing(of loglret(*),of logbench(*));
  _N_=mod(_N_,13);
  loglret(_N_)=log(1+lret);
  logbench(_N_)=log(1+lbenchret);
  n_months=n(of loglret(*)); /* number of months used in calculation */
  bhar=exp(sum(of loglret(*))-sum(of logbench(*));
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The idea is that the arrays are overwritten if there are more than 13 months for a given ticker.&lt;/P&gt;</description>
    <pubDate>Sat, 29 Dec 2018 15:56:52 GMT</pubDate>
    <dc:creator>s_lassen</dc:creator>
    <dc:date>2018-12-29T15:56:52Z</dc:date>
    <item>
      <title>Calculate stock buy and hold with both firm return and market benchmark</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-stock-buy-and-hold-with-both-firm-return-and-market/m-p/523748#M142336</link>
      <description>&lt;P&gt;Dear all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have data like demonstrated in the code. "lret" is the stock return of a particular firm. "lbenchret" is the return of the market accordingly. This data is just the short version for demonstration. In the full sample, I have 36 months for more than 100 firms with all proper "lret" and "lbenchret".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data bhar;
input ticker $ Counthmonth EvntMonth AccMonth :yymmn6. lret lbenchret ;
datalines;
AA 0 201208 201208 0.0140682449 0.0195706004
AA 1 201208 201209 0.0338819793 0.0239470567
AA 2 201208 201210 -0.029220052 -0.019987832
AA 3 201208 201211 -0.018846242 0.0028426588
AA 4 201208 201212 0.031600109 0.0070434466
AA 5 201208 201301 0.0182653297 0.0491977573
AA 6 201208 201302 -0.033355557 0.0109998789
AA 7 201208 201303 0 0.0353553677
AA 8 201208 201304 -0.002350231 0.0179241582
AA 9 201208 201305 0.003523198 0.0205501713
AA 10 201208 201306 -0.083381591 -0.015112958
AA 11 201208 201307 0.0164873283 0.0482777567
AA 12 201208 201308 -0.028063068 -0.031798258
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I want to calculate the buy-and-hold return (BHAR) for 12 months/24 months/36 months of&amp;nbsp;all firms base on lret and lbenchret. The formula is as follow:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;BHAR(i)&lt;/STRONG&gt; = &lt;STRONG&gt;{&lt;/STRONG&gt; [(1+lret) of month 0] *&amp;nbsp;&lt;SPAN&gt;[(1+lret) of month 1] *&amp;nbsp;[(1+lret) of month 2] *&amp;nbsp;[(1+lret) of month 3] *&amp;nbsp; ... *&amp;nbsp;[(1+lret) of month 12] &lt;STRONG&gt;}&lt;/STRONG&gt; &lt;FONT size="5"&gt;&lt;STRONG&gt;-&lt;/STRONG&gt;&lt;/FONT&gt;&amp;nbsp;&lt;STRONG&gt;{&lt;/STRONG&gt; [(1+lbenchret) of month 0] *&amp;nbsp;[(1+lbenchret) of month 1] *&amp;nbsp;[(1+lbenchret) of month 2] *&amp;nbsp;[(1+lbenchret) of month 3] *&amp;nbsp; ... *&amp;nbsp;[(1+lbenchret) of month 12] &lt;STRONG&gt;}&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;where: i = firm i, lret = return of firm i in particular month, lbenchret = return of market for firm i in particular month.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;How can I create a SAS code to do so?&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Thank you so much!&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 29 Dec 2018 19:19:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-stock-buy-and-hold-with-both-firm-return-and-market/m-p/523748#M142336</guid>
      <dc:creator>LucyDang</dc:creator>
      <dc:date>2018-12-29T19:19:33Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate stock buy and hold with both firm return and market benchmark</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-stock-buy-and-hold-with-both-firm-return-and-market/m-p/523825#M142373</link>
      <description>&lt;P&gt;I do not have SAS on this computer, so I cannot test, but I think something like this should work:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set bhar;
  by ticker;
  array loglret (0:12) 8 _temporary_;
  array logbench(0:12) 8 _temporary_;
  if first.ticker then
    call missing(of loglret(*),of logbench(*));
  _N_=mod(_N_,13);
  loglret(_N_)=log(1+lret);
  logbench(_N_)=log(1+lbenchret);
  n_months=n(of loglret(*)); /* number of months used in calculation */
  bhar=exp(sum(of loglret(*))-sum(of logbench(*));
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The idea is that the arrays are overwritten if there are more than 13 months for a given ticker.&lt;/P&gt;</description>
      <pubDate>Sat, 29 Dec 2018 15:56:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-stock-buy-and-hold-with-both-firm-return-and-market/m-p/523825#M142373</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2018-12-29T15:56:52Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate stock buy and hold with both firm return and market benchmark</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-stock-buy-and-hold-with-both-firm-return-and-market/m-p/523871#M142393</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data bhar;
input ticker $ Counthmonth EvntMonth AccMonth :yymmn6. lret lbenchret ;
datalines;
AA 0 201208 201208 0.0140682449 0.0195706004
AA 1 201208 201209 0.0338819793 0.0239470567
AA 2 201208 201210 -0.029220052 -0.019987832
AA 3 201208 201211 -0.018846242 0.0028426588
AA 4 201208 201212 0.031600109 0.0070434466
AA 5 201208 201301 0.0182653297 0.0491977573
AA 6 201208 201302 -0.033355557 0.0109998789
AA 7 201208 201303 0 0.0353553677
AA 8 201208 201304 -0.002350231 0.0179241582
AA 9 201208 201305 0.003523198 0.0205501713
AA 10 201208 201306 -0.083381591 -0.015112958
AA 11 201208 201307 0.0164873283 0.0482777567
AA 12 201208 201308 -0.028063068 -0.031798258
;
run;
data want;
 set bhar;
 by ticker;
 retain cum_lret cum_lben 1;
 if first.ticker then do;cum_lret=1;cum_lben=1;end;;
 lret=sum(lret,1);
 lbenchret=sum(lbenchret,1);
 cum_lret=cum_lret*lret;
 cum_lben=cum_lben*lbenchret;
 if last.ticker then BHAR=cum_lret-cum_lben;
 run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 30 Dec 2018 11:18:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-stock-buy-and-hold-with-both-firm-return-and-market/m-p/523871#M142393</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-12-30T11:18:12Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate stock buy and hold with both firm return and market benchmark</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-stock-buy-and-hold-with-both-firm-return-and-market/m-p/523884#M142401</link>
      <description>&lt;OL&gt;
&lt;LI&gt;You're using the term 12-month BHAR, but your formula is for 13-month BHAR.&amp;nbsp; Which is the one you want?&lt;/LI&gt;
&lt;LI&gt;If you're using the same benchmark for all firms (and for the same time range), you could create the benchmark 12, 24, and 36 month returns once, store them in an array and just lookup their values as needed.&amp;nbsp; Then you only need to calculate the individual firms&amp;nbsp; rolling window returns.&amp;nbsp;&amp;nbsp; Here's code that develops the complete benchmark cumulative "returns"&amp;nbsp; (actually cumulative 1+returns) during the first ticker, and uses those values for all other tickers:&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data bhar;
input ticker $ Counthmonth EvntMonth AccMonth :yymmn6. lret lbenchret ;
datalines;
AA 0 201208 201208 0.0140682449 0.0195706004
AA 1 201208 201209 0.0338819793 0.0239470567
AA 2 201208 201210 -0.029220052 -0.019987832
AA 3 201208 201211 -0.018846242 0.0028426588
AA 4 201208 201212 0.031600109 0.0070434466
AA 5 201208 201301 0.0182653297 0.0491977573
AA 6 201208 201302 -0.033355557 0.0109998789
AA 7 201208 201303 0 0.0353553677
AA 8 201208 201304 -0.002350231 0.0179241582
AA 9 201208 201305 0.003523198 0.0205501713
AA 10 201208 201306 -0.083381591 -0.015112958
AA 11 201208 201307 0.0164873283 0.0482777567
AA 12 201208 201308 -0.028063068 -0.031798258
;
run;


data want (drop=_:);
  array cumbnchmrk {-36:36} _temporary_ (73*1);
  array cumfirm    {-36:36} _temporary_ (73*1);
  set bhar;
  by ticker;
  retain _bnchmrkdone 0;
  if _bnchmrkdone=0 then cumbnchmrk{counthmonth}=  cumbnchmrk{counthmonth-1}*(1+lbenchret);
  if last.ticker then _bnchmrkdone=1;

  cumfirm{counthmonth}=cumfirm{counthmonth-1}*(1+lret);
  if counthmonth&amp;gt;=12 then bhar12 =  cumfirm{counthmonth}/cumfirm{counthmonth-12}
                                  - cumbnchmrk{counthmonth}/cumbnchmrk{counthmonth-12};

  if counthmonth&amp;gt;=24 then bhar24 =  cumfirm{counthmonth}/cumfirm{counthmonth-24}
                                  - cumbnchmrk{counthmonth}/cumbnchmrk{counthmonth-24};

  if counthmonth&amp;gt;=36 then bhar36 =  cumfirm{counthmonth}/cumfirm{counthmonth-36}
                                  - cumbnchmrk{counthmonth}/cumbnchmrk{counthmonth-36};
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This develops "true" 12 (not 13), 24, and 36-month rolling returns, but only for complete windows (i.e. no "short" windows).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 30 Dec 2018 18:00:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-stock-buy-and-hold-with-both-firm-return-and-market/m-p/523884#M142401</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-12-30T18:00:20Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate stock buy and hold with both firm return and market benchmark</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-stock-buy-and-hold-with-both-firm-return-and-market/m-p/523913#M142414</link>
      <description>&lt;P&gt;Dear mkeintz&lt;SPAN class="login-bold"&gt;, I want to calculate 12 months. Month "0" is the event month. That's why it's 13 months there in the data. Since your code has "countmonth-1", I think that I do not need to delete the event month before applying your code, right? &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="login-bold"&gt;Btw, what does "73*1" mean?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="login-bold"&gt;How about in case that there are 2 events on 2 different days for the same month for the same company with the same ticker?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="login-bold"&gt;I applied your code to my whole data, but it did not work &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 31 Dec 2018 08:03:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-stock-buy-and-hold-with-both-firm-return-and-market/m-p/523913#M142414</guid>
      <dc:creator>LucyDang</dc:creator>
      <dc:date>2018-12-31T08:03:52Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate stock buy and hold with both firm return and market benchmark</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-stock-buy-and-hold-with-both-firm-return-and-market/m-p/523916#M142416</link>
      <description>&lt;P&gt;Dear Ksharp, what if I have two different&amp;nbsp;event days but for the same 1 firm with the same ticker? How should I revise your code?&lt;/P&gt;</description>
      <pubDate>Mon, 31 Dec 2018 07:43:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-stock-buy-and-hold-with-both-firm-return-and-market/m-p/523916#M142416</guid>
      <dc:creator>LucyDang</dc:creator>
      <dc:date>2018-12-31T07:43:21Z</dc:date>
    </item>
  </channel>
</rss>

