{"id":1009,"date":"2017-12-06T19:26:54","date_gmt":"2017-12-07T00:26:54","guid":{"rendered":"http:\/\/www.kaikaichen.com\/?p=1009"},"modified":"2017-12-06T19:33:37","modified_gmt":"2017-12-07T00:33:37","slug":"calculate-idiosyncratic-stock-return-volatility-using-sas","status":"publish","type":"post","link":"https:\/\/www.kaichen.work\/?p=1009","title":{"rendered":"Calculate idiosyncratic stock return volatility"},"content":{"rendered":"<p>I have noted two slightly different definitions of idiosyncratic stock return volatility in:<\/p>\n<ul>\n<li>Campbell, J. Y. and Taksler, G. B. (2003), Equity Volatility and Corporate Bond Yields. <em>The Journal of Finance<\/em>, 58: 2321\u20132350. doi:10.1046\/j.1540-6261.2003.00607.x<\/li>\n<li>Rajgopal, S. and Venkatachalam, M. (2011), Financial reporting quality and idiosyncratic return volatility.\u00a0<em>Journal of Accounting and Economics<\/em>, 51: 1\u201320. doi.org\/10.1016\/j.jacceco.2010.06.001.<\/li>\n<\/ul>\n<p>The code in this post is used to calculate Campbell and Taksler&#8217;s (2003) idiosyncratic stock return volatility, but it can be easily modified for other definitions.<\/p>\n<p>Specifically, this code requires an input dataset that includes two variables: <code>permno<\/code> and <code>enddt<\/code>, where <code>enddt<\/code> is the date of interest. This code will calculate the standard deviation of daily abnormal returns over the 180 calendar days before (and including) <code>enddt<\/code>. Abnormal returns will be calculated using four methods: (1) market-adjusted; (2) standard market model; (3) Fama-French three factors; and (4) Fama-French three factors as well as momentum. This code requires at least 21 return observations (one-month trading days) over that 180-day period for a <code>permno<\/code>\u00a0to calculate its stock return volatility.<\/p>\n<pre class=\"lang:sas decode:true \">libname local \"D:\\Dropbox\";\r\n\r\n* Remote signon and upload input database including permno and date;\r\n%let wrds=wrds-cloud.wharton.upenn.edu 4016;\r\noptions comamid=TCP remote=WRDS;\r\nsignon username=_prompt_;\r\n\r\nrsubmit;\r\n\r\nproc upload data=local.input out=input; run;\r\n\r\n* Get raw returns and FF risk factors;\r\nproc sql;\r\n  create table rets as\r\n  select a.*, b.date, b.ret, c.smb, c.hml, c.umd, d.vwretd as mktret, (b.ret-d.vwretd) as exret\r\n  from input a left join crsp.dsf b\r\n  on a.permno=b.permno and a.enddt-180&lt;b.date&lt;=a.enddt\r\n  left join ff.factors_daily c\r\n  on b.date=c.date\r\n  left join crsp.dsi d\r\n  on b.date=d.date\r\n  order by a.permno, a.enddt, b.date;\r\nquit;\r\n\r\n* Estimate factor exposures;\r\nproc printto log=junk; run;\r\n\r\nproc reg data=rets edf outest=params noprint;\r\n  by permno enddt;\r\n  eq0: model exret=;\r\n  eq1: model ret=mktret;\r\n  eq2: model ret=mktret smb hml;\r\n  eq3: model ret=mktret smb hml umd;\r\nrun;\r\nproc printto; run;\r\n\r\n* Compute abnormal returns for all models for each trading day;\r\ndata abrets; merge rets (in=a)\r\n  params (where=(_model_='eq0')\r\n     keep=permno enddt _model_ _rmse_ _p_ _edf_\r\n     rename=(_rmse_=std0 _p_=p0 _edf_=edf0))\r\n  params (where=(_model_='eq1')\r\n     keep=permno enddt _model_ _rmse_ intercept mktret\r\n     rename=(_rmse_=std1 intercept=alpha1 mktret=beta1))\r\n  params (where=(_model_='eq2')\r\n     keep=permno enddt _model_ _rmse_ intercept mktret smb hml\r\n     rename=(_rmse_=std2 intercept=alpha2 mktret=beta2 smb=sminb2 hml=hminl2))\r\n  params (where=(_model_='eq3')\r\n     keep=permno enddt _model_ _rmse_ intercept mktret smb hml umd\r\n     rename=(_rmse_=std3 intercept=alpha3 mktret=beta3 smb=sminb3 hml=hminl3 umd=umind3));\r\n  by permno enddt;\r\n  var0=std0**2; var1=std1**2;var2=std2**2;var3=std3**2;\r\n  abret0=exret;\r\n  expret1=alpha1+beta1*mktret; abret1=ret-expret1;\r\n  expret2=alpha2+beta2*mktret+sminb2*smb+hminl2*hml; abret2=ret-expret2;\r\n  expret3=alpha3+beta3*mktret+sminb3*smb+hminl3*hml+umind3*umd; abret3=ret-expret3;\r\n  nobs=p0+edf0;  \/*number of observations used in estimation*\/\r\n drop p0 edf0 std0 std1 std2 std3 _model_ exret;\r\n if a and nobs&gt;=21;\r\nrun;\r\n\r\nproc sort data=abrets; by permno enddt date; run;\r\n\r\nproc means data=abrets noprint;\r\n  by permno enddt;\r\n  output out=retvol\r\n  std(abret0)=vol0 std(abret1)=vol1 std(abret2)=vol2 std(abret3)=vol3;\r\nrun;\r\n\r\n* Download output dataset and remote signoff;\r\nproc download data=retvol out=local.retvol; run;\r\n\r\nendrsubmit;\r\nsignoff;\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have noted two slightly different definitions of idiosyncratic stock return volatility in: Campbell, J. Y. and Taksler, G. B. (2003), Equity Volatility and Corporate Bond Yields. The Journal of Finance, 58: 2321\u20132350. doi:10.1046\/j.1540-6261.2003.00607.x Rajgopal, S. and Venkatachalam, M. (2011), &hellip; <a href=\"https:\/\/www.kaichen.work\/?p=1009\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"_links":{"self":[{"href":"https:\/\/www.kaichen.work\/index.php?rest_route=\/wp\/v2\/posts\/1009"}],"collection":[{"href":"https:\/\/www.kaichen.work\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kaichen.work\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kaichen.work\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kaichen.work\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1009"}],"version-history":[{"count":6,"href":"https:\/\/www.kaichen.work\/index.php?rest_route=\/wp\/v2\/posts\/1009\/revisions"}],"predecessor-version":[{"id":1174,"href":"https:\/\/www.kaichen.work\/index.php?rest_route=\/wp\/v2\/posts\/1009\/revisions\/1174"}],"wp:attachment":[{"href":"https:\/\/www.kaichen.work\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1009"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kaichen.work\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1009"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kaichen.work\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1009"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}