<?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 correlation/covariance for stocks in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculating-correlation-covariance-for-stocks/m-p/425717#M104871</link>
    <description>&lt;P&gt;Just put a BY YEAR statement everywhere:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Stock1;
set sashelp.stocks;
year = year(date);  /* create year variable in data set */
run;

*Sort and create stock data set to work with;
proc sort data=stock1 out=stocks_long;
    by year date;
run;

*transpose to a wide format;
proc transpose data=stocks_long out=stocks_wide prefix=STOCK_;
    by year date;
    id stock;
    var Open;
run;

*Run a correlation on the stocks, include only variables of interest with the STOCK_: from the prefix above;
proc corr data=stocks_wide cov outp=want;
    by year;
    var stock_:;
run;

proc print data=want; run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 08 Jan 2018 10:59:02 GMT</pubDate>
    <dc:creator>Rick_SAS</dc:creator>
    <dc:date>2018-01-08T10:59:02Z</dc:date>
    <item>
      <title>Calculating correlation/covariance for stocks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-correlation-covariance-for-stocks/m-p/425660#M104852</link>
      <description>&lt;P&gt;I have a large dataset that contains daily returns for a many stocks. The dataset has 3 columns: date, stocks, and returns. How can I calculate correlation and covariance between stocks for each quarter or year?&amp;nbsp;&lt;/P&gt;&lt;P&gt;Instead of having a covariance matrix for each period, my desired output would have 5 columns: period ( which can be quarter or year), stock1, stock 2, correlation, and covariance.&lt;/P&gt;&lt;P&gt;Thank you very much for your help!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Jan 2018 01:04:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-correlation-covariance-for-stocks/m-p/425660#M104852</guid>
      <dc:creator>somebody</dc:creator>
      <dc:date>2018-01-08T01:04:04Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating correlation/covariance for stocks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-correlation-covariance-for-stocks/m-p/425661#M104853</link>
      <description>&lt;P&gt;Unfortunately you'll need to restructure your dataset to a wide&amp;nbsp;type format first, i.e.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Date Stock1 Stock2 Stock3 .....&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then you can use PROC CORR to get the correlations and covariances.&amp;nbsp;&lt;BR /&gt;The PROC CORR step is easier.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To reformat your data set you can use PROC TRANSPOSE. You'll like first need to sort your data. See a fully worked example below, it should run fine on your machine. I'll leave the final formatting/customization to you, you can use PROC TRANSPOSE as well to reformat it to the long format you were requesting.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*Sort and create stock data set to work with;
proc sort data=sashelp.stocks out=stocks_long;
    by date;
run;

*transpose to a wide format;
proc transpose data=stocks_long out=stocks_wide prefix=STOCK_;
    by date;
    id stock;
    var Open;
run;

*Run a correlation on the stocks, include only variables of interest with the STOCK_: from the prefix above;
proc corr data=stocks_wide cov outp=want;
    var stock_:;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/98381"&gt;@somebody&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;I have a large dataset that contains daily returns for a many stocks. The dataset has 3 columns: date, stocks, and returns. How can I calculate correlation and covariance between stocks for each quarter or year?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instead of having a covariance matrix for each period, my desired output would have 5 columns: period ( which can be quarter or year), stock1, stock 2, correlation, and covariance.&lt;/P&gt;
&lt;P&gt;Thank you very much for your help!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Jan 2018 01:16:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-correlation-covariance-for-stocks/m-p/425661#M104853</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-01-08T01:16:27Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating correlation/covariance for stocks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-correlation-covariance-for-stocks/m-p/425680#M104858</link>
      <description>&lt;P&gt;thanks. your code pretty much does the job. just one last thing, how can I calculate the CORR and COV between stocks for each year?&lt;/P&gt;</description>
      <pubDate>Mon, 08 Jan 2018 04:02:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-correlation-covariance-for-stocks/m-p/425680#M104858</guid>
      <dc:creator>somebody</dc:creator>
      <dc:date>2018-01-08T04:02:03Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating correlation/covariance for stocks</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-correlation-covariance-for-stocks/m-p/425717#M104871</link>
      <description>&lt;P&gt;Just put a BY YEAR statement everywhere:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Stock1;
set sashelp.stocks;
year = year(date);  /* create year variable in data set */
run;

*Sort and create stock data set to work with;
proc sort data=stock1 out=stocks_long;
    by year date;
run;

*transpose to a wide format;
proc transpose data=stocks_long out=stocks_wide prefix=STOCK_;
    by year date;
    id stock;
    var Open;
run;

*Run a correlation on the stocks, include only variables of interest with the STOCK_: from the prefix above;
proc corr data=stocks_wide cov outp=want;
    by year;
    var stock_:;
run;

proc print data=want; run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 08 Jan 2018 10:59:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-correlation-covariance-for-stocks/m-p/425717#M104871</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2018-01-08T10:59:02Z</dc:date>
    </item>
  </channel>
</rss>

