<?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: calculating moving covariance/variance + with missing values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/calculating-moving-covariance-variance-with-missing-values/m-p/700128#M214240</link>
    <description>&lt;P&gt;What do you mean by 'deal with' missing values?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, do you have multiple groups/IDs in your data, and should they be handled as well?&lt;/P&gt;</description>
    <pubDate>Thu, 19 Nov 2020 07:24:53 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2020-11-19T07:24:53Z</dc:date>
    <item>
      <title>calculating moving covariance/variance + with missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculating-moving-covariance-variance-with-missing-values/m-p/700127#M214239</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WANT; 
   input YEAR $ return $ market return;
   datalines;                     
1990 0.11 0.05
1991 0.02 0.02
1992 0.03 0.05
1993 0.04 0.03
1994 0.05 0.08
1995 0.06 0.09
1996 0.07 .
1997 0.08 0.09
1998 0.07 0.07
1999 0.14 0.20 
2000 0.10 0.11&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The above is a simple example of my dataset.&lt;/P&gt;&lt;P&gt;I am trying to calculate moving covariance(between return and market return) and moving variance of market returns.&lt;/P&gt;&lt;P&gt;As you may guess, I at the end want to have beta but the beta I need to have is not a coefficient from regression. The paper I follow did this way so I need to calculate cov(Rm, Ri) and Var(Rm).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I first thought to utilize RPOC EXPAND and yes, this at least gives me the result of variances by obtaining moving standard deviations from MOVSTD and I square them. Nonetheless, there is no command calculating COVARIANCE, quite sadly.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do anyone of you know how to calculate covariances given with the data like the above? Also, how can I deal with missing values when I use your own codes?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Moving covariance is tricky and what I have found so far is moving covariance for time-series only.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I appreciate your helps a lot!!!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Nov 2020 07:21:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculating-moving-covariance-variance-with-missing-values/m-p/700127#M214239</guid>
      <dc:creator>JKCho</dc:creator>
      <dc:date>2020-11-19T07:21:09Z</dc:date>
    </item>
    <item>
      <title>Re: calculating moving covariance/variance + with missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculating-moving-covariance-variance-with-missing-values/m-p/700128#M214240</link>
      <description>&lt;P&gt;What do you mean by 'deal with' missing values?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, do you have multiple groups/IDs in your data, and should they be handled as well?&lt;/P&gt;</description>
      <pubDate>Thu, 19 Nov 2020 07:24:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculating-moving-covariance-variance-with-missing-values/m-p/700128#M214240</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-11-19T07:24:53Z</dc:date>
    </item>
    <item>
      <title>Re: calculating moving covariance/variance + with missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculating-moving-covariance-variance-with-missing-values/m-p/700139#M214244</link>
      <description>&lt;P&gt;For the data you provided I think that something like below may be an option:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data HAVE; 
   input YEAR $ return market_return;
datalines;                     
1990 0.11 0.05
1991 0.02 0.02
1992 0.03 0.05
1993 0.04 0.03
1994 0.05 0.08
1995 0.06 0.09
1996 0.07 .
1997 0.08 0.09
1998 0.07 0.07
1999 0.14 0.20 
2000 0.10 0.11
;
run;

%let window = 3; /* one less than what you need */

data want;
  if &amp;amp;window.&amp;lt;2 then stop; /* prevent to short window */
  set have curobs=curobs;
  /* in case you need it in groups uncomment below 2 lines */
  /*
    by SOME_GROUPING_VARIABLE;
    if first.SOME_GROUPING_VARIABLE then call missing(of MR[*], of R[*]);
  */

  array R[0:&amp;amp;window.]  _temporary_;
  array MR[0:&amp;amp;window.] _temporary_;

  R[mod(curobs,&amp;amp;window)] = return; 
  MR[mod(curobs,&amp;amp;window)] = market_return;

  /* variance */
  if nmiss(of MR[*]) &amp;lt; &amp;amp;window. then MRvariance = var(of MR[*]);

  /* covariance */
  if nmiss(of MR[*]) &amp;lt; &amp;amp;window. and nmiss(of R[*]) &amp;lt; &amp;amp;window. then
    do;
      MRavg = mean(of MR[*]);
       Ravg = mean(of  R[*]);

       s = 0; n = 0;
       do i = 0 to &amp;amp;window.;
         if not nmiss(MR[i],R[i]) then
           do;
             s + (MR[i]-MRavg)*(R[i]-Ravg);
             n + 1;
           end;

         if n&amp;lt;2 then MR_R_covariance =.;
                else MR_R_covariance = s/(n-1);
       end;
    end;
run;
proc print data = want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;all the best&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Thu, 19 Nov 2020 08:45:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculating-moving-covariance-variance-with-missing-values/m-p/700139#M214244</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-11-19T08:45:46Z</dc:date>
    </item>
    <item>
      <title>Re: calculating moving covariance/variance + with missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculating-moving-covariance-variance-with-missing-values/m-p/700172#M214264</link>
      <description>&lt;P&gt;In your earlier thread about moving averages, I pointed to&amp;nbsp;&lt;A href="https://support.sas.com/kb/25/027.html" target="_blank" rel="noopener"&gt;https://support.sas.com/kb/25/027.html&lt;/A&gt;&amp;nbsp;which allows you to compute moving averages in a data set. The same logic allows you to compute moving covariances (except instead of the part where you compute a moving average, you have to program it to compute the moving covariance).&lt;/P&gt;</description>
      <pubDate>Thu, 19 Nov 2020 11:52:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculating-moving-covariance-variance-with-missing-values/m-p/700172#M214264</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-11-19T11:52:07Z</dc:date>
    </item>
    <item>
      <title>Re: calculating moving covariance/variance + with missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculating-moving-covariance-variance-with-missing-values/m-p/700323#M214314</link>
      <description>As I said,&lt;BR /&gt;&lt;BR /&gt;I first thought to utilize RPOC EXPAND and yes, this at least gives me the result of variances by obtaining moving standard deviations from MOVSTD and I square them. Nonetheless, there is no command calculating COVARIANCE, quite sadly.&lt;BR /&gt;&lt;BR /&gt;I was asking about obtaining moving covariance, not movage. You know it is not the same when there is no available PROC EXPAND command that is why I had to make this thread. clear?</description>
      <pubDate>Thu, 19 Nov 2020 20:33:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculating-moving-covariance-variance-with-missing-values/m-p/700323#M214314</guid>
      <dc:creator>JKCho</dc:creator>
      <dc:date>2020-11-19T20:33:30Z</dc:date>
    </item>
    <item>
      <title>Re: calculating moving covariance/variance + with missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculating-moving-covariance-variance-with-missing-values/m-p/700362#M214332</link>
      <description>&lt;P&gt;You can look up the formula for covariance, and then replace the calculation of the mean in the DATA step example with the calculation of a covariance.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Nov 2020 21:58:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculating-moving-covariance-variance-with-missing-values/m-p/700362#M214332</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-11-19T21:58:45Z</dc:date>
    </item>
    <item>
      <title>Re: calculating moving covariance/variance + with missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculating-moving-covariance-variance-with-missing-values/m-p/700363#M214333</link>
      <description>&lt;P&gt;There's also Example 2 in &lt;A href="https://support.sas.com/resources/papers/proceedings/pdfs/sgf2008/093-2008.pdf" target="_self"&gt;this paper&lt;/A&gt; which produces a moving correlation coefficient with PROC EXPAND; if you can calculate moving correlation then you can use the same ideas to calculate a moving covariance.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Nov 2020 22:05:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculating-moving-covariance-variance-with-missing-values/m-p/700363#M214333</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-11-19T22:05:43Z</dc:date>
    </item>
    <item>
      <title>Re: calculating moving covariance/variance + with missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculating-moving-covariance-variance-with-missing-values/m-p/700396#M214337</link>
      <description>&lt;P&gt;It's true theatproc expand can only generate moving statistics on univariate series, which excludes moving covariances.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BUT ... you can&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;create a 3rd variable&amp;nbsp; &amp;nbsp;(product=return*market_return) in a new data set&lt;/LI&gt;
&lt;LI&gt;Use proc expand on that new dataset to get the moving sums of return, market_return, and product&lt;/LI&gt;
&lt;LI&gt;Take advantage of the fact that covariance can be calculated from those sums using the bottom line in this image&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 256px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/51852i3375A2096876A720/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;In other words, for a windows of size 6&amp;nbsp; you would have&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; one sixth of the sum of product,&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; minus&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; one thirty-sixth of the sum of return times the sum of market_return.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As to missing values, if they are rare, I'd just have 5 data points in my 6-year windows&amp;nbsp; (i.e. in your data, all the windows that normally would include 1996).&amp;nbsp; Just be sure to drop ALL the variables for 1996, not just market_return.&lt;/P&gt;</description>
      <pubDate>Fri, 20 Nov 2020 04:58:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculating-moving-covariance-variance-with-missing-values/m-p/700396#M214337</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-11-20T04:58:36Z</dc:date>
    </item>
    <item>
      <title>Re: calculating moving covariance/variance + with missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculating-moving-covariance-variance-with-missing-values/m-p/700398#M214340</link>
      <description>Hello mkeintz,&lt;BR /&gt;&lt;BR /&gt;Yeah, I did a similar approach you suggested. There is no covariance so I did calculate correlation and multiplied it by the standard deviation of returns and that of market returns.&lt;BR /&gt;&lt;BR /&gt;As to missing values, dropping missing values are what I usually do but if I do so, this is somewhat against a pure definition of what I want to obtain, 24-month period covariance or so. What I have thought is to calculate standard deviations of 24 observations or less if there are missing values while the windows are still 24-month. SO... yeah, same. I just leave missing values since they are rare... like you said: I'd just have 5 data points in my 6-year windows.&lt;BR /&gt;&lt;BR /&gt;Thank you for your tip and stay safe!&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Fri, 20 Nov 2020 05:43:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculating-moving-covariance-variance-with-missing-values/m-p/700398#M214340</guid>
      <dc:creator>JKCho</dc:creator>
      <dc:date>2020-11-20T05:43:07Z</dc:date>
    </item>
    <item>
      <title>Re: calculating moving covariance/variance + with missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculating-moving-covariance-variance-with-missing-values/m-p/700399#M214341</link>
      <description>&lt;P&gt;Hi Paige,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I did found the paper you suggested once but did not think to revise the codes for my case.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I did revise some and share this with other users who may face similar difficulties.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc expand DATA = result OUT = result2;
id date;
 convert VWRETD = YISUM / METHOD = none TRANSFORMOUT = (movsum 60 trimleft 36);
 convert ret = XISUM / METHOD = none TRANSFORMOUT = (movsum 60 trimleft 36);
 convert prodxiyi = PRODXIYISUM / METHOD = none TRANSFORMOUT = (movsum 60 trimleft 36);
 convert obs = N / METHOD = none TRANSFORMOUT = (movsum 60 trimleft 36);
 convert ret = XICSS / METHOD = none TRANSFORMOUT = (movcss 60 trimleft 36);
 convert VWRETD = YICSS / METHOD = none TRANSFORMOUT = (movcss 60 trimleft 36);
 convert VWRETD = STDrm / METHOD = none TRANSFORMOUT = (movstd 60 trimleft 36);
 convert ret = STDret / METHOD = none TRANSFORMOUT = (movstd 60 trimleft 36);
by cusip;
run;
data result3; set result2;
 R = (prodxiyisum - (yisum*xisum)/n) / ( sqrt(xicss)*sqrt(yicss)) ;
 cov=R*STDrm*STDret;
 var=(STDrm)**2;
 beta=cov/var;
 yyyy=year(date);
 if n="." then delete;
 if 1990&amp;lt;=yyyy&amp;lt;=2019;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 20 Nov 2020 05:50:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculating-moving-covariance-variance-with-missing-values/m-p/700399#M214341</guid>
      <dc:creator>JKCho</dc:creator>
      <dc:date>2020-11-20T05:50:19Z</dc:date>
    </item>
    <item>
      <title>Re: calculating moving covariance/variance + with missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculating-moving-covariance-variance-with-missing-values/m-p/700400#M214342</link>
      <description>Thank you for bringing the paper back to me!</description>
      <pubDate>Fri, 20 Nov 2020 05:50:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculating-moving-covariance-variance-with-missing-values/m-p/700400#M214342</guid>
      <dc:creator>JKCho</dc:creator>
      <dc:date>2020-11-20T05:50:59Z</dc:date>
    </item>
  </channel>
</rss>

