<?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 pairwise correlation for multipel pairs over each time period in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculate-pairwise-correlation-for-multipel-pairs-over-each-time/m-p/694032#M211638</link>
    <description>&lt;P&gt;Sorry if my question wasn't clear. In my original question, there is a dataset "want", which describes my goal. I was wondering how to convert the correlation matrix to that dataset.&lt;/P&gt;</description>
    <pubDate>Sat, 24 Oct 2020 19:37:14 GMT</pubDate>
    <dc:creator>xyxu</dc:creator>
    <dc:date>2020-10-24T19:37:14Z</dc:date>
    <item>
      <title>Calculate pairwise correlation for multipel pairs over each time period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-pairwise-correlation-for-multipel-pairs-over-each-time/m-p/693653#M211552</link>
      <description>&lt;P&gt;I have a data set of daily prices for each ID that looks like the following example.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input id $1. date :yymmdd10. price 5.;
	format date yymmdd10.;
	datalines;
A 2000-01-02 3
A 2000-01-03 4
A 2000-01-04 6
A 2000-01-05 4
A 2000-01-06 7
A 2000-01-07 8
A 2000-01-08 9
A 2000-01-09 6
A 2000-01-10 7
A 2000-01-11 5
A 2000-01-12 6
A 2000-01-13 5
B 2000-01-02 4
B 2000-01-03 6
B 2000-01-04 7
B 2000-01-05 8
B 2000-01-06 9
B 2000-01-07 10
B 2000-01-08 12
B 2000-01-09 11
B 2000-01-10 9
B 2000-01-11 8
B 2000-01-12 9
B 2000-01-13 7
C 2000-01-02 3
C 2000-01-03 2
C 2000-01-04 4
C 2000-01-05 3
C 2000-01-06 4
C 2000-01-07 5
C 2000-01-08 4
C 2000-01-09 3
C 2000-01-10 4
C 2000-01-11 6
C 2000-01-12 6
C 2000-01-13 5
;
run;

data have; set have; week = intnx('week', date, 0, 'e'); format week yymmdd10.; run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I want to calculate correlation between every pair of IDs' prices: i.e., correlation between prices of (A,B), (B,C), (A,C), over each week. The result I want looks like the following:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	input id1 $ id2 $ week :yymmdd10. corr 5.;
	format week yymmdd10.;
	datalines;
A B 2000-01-08 0.3
A C 2000-01-08 0.5
B C 2000-01-08 0.4
A B 2000-01-15 0.2
A C 2000-01-15 0.4
B C 2000-01-15 0.5
;
run;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Is there a way to do this efficiently, especially when I have A, B, C, ..., Z and many weeks?&lt;/P&gt;</description>
      <pubDate>Fri, 23 Oct 2020 04:25:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-pairwise-correlation-for-multipel-pairs-over-each-time/m-p/693653#M211552</guid>
      <dc:creator>xyxu</dc:creator>
      <dc:date>2020-10-23T04:25:07Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate pairwise correlation for multipel pairs over each time period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-pairwise-correlation-for-multipel-pairs-over-each-time/m-p/693695#M211563</link>
      <description>&lt;P&gt;Correlations in SAS are defined between variables, not between rows of the data set. So you will have to transpose the data so that you have variables A B and C, and not rows that represent A B and C.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have;
	by date;
run;
proc transpose data=have out=have_t;
	by date;
	var price;
	id id;
run;
data have_t;
	set have_t;
	week = intnx('week', date, 0, 'e');
	format week mmddyy10.;
run;
proc corr data=have_t;
	var a b c;
	by week;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 23 Oct 2020 10:30:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-pairwise-correlation-for-multipel-pairs-over-each-time/m-p/693695#M211563</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-10-23T10:30:14Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate pairwise correlation for multipel pairs over each time period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-pairwise-correlation-for-multipel-pairs-over-each-time/m-p/694027#M211634</link>
      <description>&lt;P&gt;When I do&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc corr data = have_t out = corr (where = (_type_ = 'CORR')) noprint;
	var a b c;
	by week;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I get a correlation matrix. Is there a simple way to conver the matrix to what I want?&lt;/P&gt;</description>
      <pubDate>Sat, 24 Oct 2020 18:48:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-pairwise-correlation-for-multipel-pairs-over-each-time/m-p/694027#M211634</guid>
      <dc:creator>xyxu</dc:creator>
      <dc:date>2020-10-24T18:48:13Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate pairwise correlation for multipel pairs over each time period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-pairwise-correlation-for-multipel-pairs-over-each-time/m-p/694030#M211637</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/99650"&gt;@xyxu&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc corr data = have_t out = corr (where = (_type_ = 'CORR')) noprint;
	var a b c;
	by week;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I get a correlation matrix. Is there a simple way to conver the matrix to what I want?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I don't know what you mean. I don't know what you want, or why the above code doesn't meet your needs.&lt;/P&gt;</description>
      <pubDate>Sat, 24 Oct 2020 19:31:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-pairwise-correlation-for-multipel-pairs-over-each-time/m-p/694030#M211637</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-10-24T19:31:32Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate pairwise correlation for multipel pairs over each time period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-pairwise-correlation-for-multipel-pairs-over-each-time/m-p/694032#M211638</link>
      <description>&lt;P&gt;Sorry if my question wasn't clear. In my original question, there is a dataset "want", which describes my goal. I was wondering how to convert the correlation matrix to that dataset.&lt;/P&gt;</description>
      <pubDate>Sat, 24 Oct 2020 19:37:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-pairwise-correlation-for-multipel-pairs-over-each-time/m-p/694032#M211638</guid>
      <dc:creator>xyxu</dc:creator>
      <dc:date>2020-10-24T19:37:14Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate pairwise correlation for multipel pairs over each time period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-pairwise-correlation-for-multipel-pairs-over-each-time/m-p/694038#M211639</link>
      <description>&lt;P&gt;It has to be that form, and no other form?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let me ask another real-world question ... are there only three variables A B C or does the real-world problem you are working on have many more than three variables?&lt;/P&gt;</description>
      <pubDate>Sat, 24 Oct 2020 20:17:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-pairwise-correlation-for-multipel-pairs-over-each-time/m-p/694038#M211639</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-10-24T20:17:04Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate pairwise correlation for multipel pairs over each time period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-pairwise-correlation-for-multipel-pairs-over-each-time/m-p/694039#M211640</link>
      <description>&lt;P&gt;Yeah I am trying to define a pair-level identifier (e.g., "AB", "AC", "BC") and use the pairwise correlation for later steps. In the real-world problem, there are around 20 IDs.&lt;/P&gt;</description>
      <pubDate>Sat, 24 Oct 2020 20:20:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-pairwise-correlation-for-multipel-pairs-over-each-time/m-p/694039#M211640</guid>
      <dc:creator>xyxu</dc:creator>
      <dc:date>2020-10-24T20:20:15Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate pairwise correlation for multipel pairs over each time period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-pairwise-correlation-for-multipel-pairs-over-each-time/m-p/694040#M211641</link>
      <description>&lt;P&gt;Ok, let's give this a try. You will have to change this code in the third row, so instead of &lt;FONT face="courier new,courier"&gt;a--c&lt;/FONT&gt; you have a valid SAS list of variable names for your problem, whatever they are, and that they are also in alphabetical order for the code to work. Naturally, we can program them to be in alphabetical order if that's not an easy thing for you to do manually. EDIT: ignore the alphabetical order comment, the code works even on variable lists where the variables are NOT in alphabetical order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set corr;
    array x(*) a--c;
    do j=1 to dim(x);
        thisvar=vname(x(j));
        if thisvar&amp;gt;_name_ then do;
             corr=vvaluex(thisvar);
             output;
        end;
    end;
    keep week _name_ thisvar corr;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 25 Oct 2020 12:19:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-pairwise-correlation-for-multipel-pairs-over-each-time/m-p/694040#M211641</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-10-25T12:19:36Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate pairwise correlation for multipel pairs over each time period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-pairwise-correlation-for-multipel-pairs-over-each-time/m-p/694102#M211678</link>
      <description>This works amazingly well, despite that I am not 100% sure why it works.</description>
      <pubDate>Sun, 25 Oct 2020 14:15:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-pairwise-correlation-for-multipel-pairs-over-each-time/m-p/694102#M211678</guid>
      <dc:creator>xyxu</dc:creator>
      <dc:date>2020-10-25T14:15:55Z</dc:date>
    </item>
  </channel>
</rss>

