<?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: generate correlation matrix from column of correlations in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/generate-correlation-matrix-from-column-of-correlations/m-p/834371#M329887</link>
    <description>&lt;P&gt;For your consideration, an example with a small data set created in a datastep.&lt;/P&gt;
&lt;PRE&gt;data have;
   input varx $ with $ corr;
datalines;
var1 var2  0.096
var1 var3  -0.062
var2 var3  -0.41
;

data need;
   set have;
   length temp $ 10;
   output;
   temp=var;
   var=with;
   with=temp;
   output;
   drop temp;
run;

proc tabulate data=need;
   class varx with;
   var corr;
   table varx='',
         with=''*corr=''*max=''
         /misstext='1'
   ;
run;&lt;/PRE&gt;
&lt;P&gt;First, please provide data in the form of a data step, such as shown above, or at least as text. We cannot code against pictures of values and I'm very unwilling to retype much.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Familiarity with reporting procedures like Tabulate says that if I have Class variable holding a row label and another with a column label I can get statistics of a numeric variable. So that tells me, since you have the same values of the row and column labels that you need a data set with all the values in the row label with a corresponding column. So the Need data step duplicates the data with the row/column positions switched to allow the "mirror".&lt;/P&gt;
&lt;P&gt;The Proc Tabulate option Misstext allows me to show what I want when the cell would be missing such as when the row and column labels are the same. The Tabulate approach could likely use other statistics but I chose max. Note that the =' ' bits are to suppress the variable names from appearing in the output.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you actually want the word "corr" that's another issue.&lt;/P&gt;</description>
    <pubDate>Tue, 20 Sep 2022 19:25:02 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2022-09-20T19:25:02Z</dc:date>
    <item>
      <title>generate correlation matrix from column of correlations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/generate-correlation-matrix-from-column-of-correlations/m-p/834341#M329880</link>
      <description>&lt;P&gt;Hi, I'm not sure there's an easy answer to this one, but let's see.&amp;nbsp; I'm trying to generate a correlation matrix like that one that comes from proc corr outp=, with the full (mirrored) correlations of the variables and 1's on the diagonal:&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="awesome_opossum_1-1663696654700.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/75406i10FDD426EB3F88AC/image-size/medium?v=v2&amp;amp;px=400" role="button" title="awesome_opossum_1-1663696654700.png" alt="awesome_opossum_1-1663696654700.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, I'm trying to get there from a column list as such:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="awesome_opossum_0-1663696513074.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/75405iDD85E6D27391A6C4/image-size/medium?v=v2&amp;amp;px=400" role="button" title="awesome_opossum_0-1663696513074.png" alt="awesome_opossum_0-1663696513074.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can't wrap my head around a relatively simple or painless way to do this.&amp;nbsp; Does anyone have advice?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Sep 2022 17:59:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/generate-correlation-matrix-from-column-of-correlations/m-p/834341#M329880</guid>
      <dc:creator>awesome_opossum</dc:creator>
      <dc:date>2022-09-20T17:59:01Z</dc:date>
    </item>
    <item>
      <title>Re: generate correlation matrix from column of correlations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/generate-correlation-matrix-from-column-of-correlations/m-p/834371#M329887</link>
      <description>&lt;P&gt;For your consideration, an example with a small data set created in a datastep.&lt;/P&gt;
&lt;PRE&gt;data have;
   input varx $ with $ corr;
datalines;
var1 var2  0.096
var1 var3  -0.062
var2 var3  -0.41
;

data need;
   set have;
   length temp $ 10;
   output;
   temp=var;
   var=with;
   with=temp;
   output;
   drop temp;
run;

proc tabulate data=need;
   class varx with;
   var corr;
   table varx='',
         with=''*corr=''*max=''
         /misstext='1'
   ;
run;&lt;/PRE&gt;
&lt;P&gt;First, please provide data in the form of a data step, such as shown above, or at least as text. We cannot code against pictures of values and I'm very unwilling to retype much.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Familiarity with reporting procedures like Tabulate says that if I have Class variable holding a row label and another with a column label I can get statistics of a numeric variable. So that tells me, since you have the same values of the row and column labels that you need a data set with all the values in the row label with a corresponding column. So the Need data step duplicates the data with the row/column positions switched to allow the "mirror".&lt;/P&gt;
&lt;P&gt;The Proc Tabulate option Misstext allows me to show what I want when the cell would be missing such as when the row and column labels are the same. The Tabulate approach could likely use other statistics but I chose max. Note that the =' ' bits are to suppress the variable names from appearing in the output.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you actually want the word "corr" that's another issue.&lt;/P&gt;</description>
      <pubDate>Tue, 20 Sep 2022 19:25:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/generate-correlation-matrix-from-column-of-correlations/m-p/834371#M329887</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-09-20T19:25:02Z</dc:date>
    </item>
    <item>
      <title>Re: generate correlation matrix from column of correlations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/generate-correlation-matrix-from-column-of-correlations/m-p/834694#M329960</link>
      <description>&lt;P&gt;Sounds like you want to start with correlations in "long form" and convert to a symmetric correlation matrix in "wide form". If you have access to SAS/IML software, you can&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;figure out the number of variables from the number of rows in the data set (solve the equation p*(p-1)/2 = N, where N is the number of rows)&lt;/LI&gt;
&lt;LI&gt;allocate a p x p matrix&lt;/LI&gt;
&lt;LI&gt;Find the names of the variables&lt;/LI&gt;
&lt;LI&gt;Fill in the upper- and lower-triangular portions of the matrix from the CORR values&lt;/LI&gt;
&lt;LI&gt;Optionally, write the information as a TYPE=CORR data set&lt;/LI&gt;
&lt;/UL&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Have;
length Var WithVar $32;
input Var WithVar Corr;
datalines;
var1 var2  0.09
var1 var3 -0.62
var1 var4  0.1
var2 var3 -0.41
var2 var4 -0.003
var3 var4 0.07
;

proc iml;
use Have;
read all var {'Var' 'WithVar' 'Corr'};
close;

/* how many vars? Solve the equation 
   p*(p-1) = N for p, where N is the number of obs
*/
N = nrow(Corr);
p = (sqrt(8*N+1)+1)/2;
varNames = j(1, p, BlankStr(32));

/* fill up the varNames */
varNames[1] = Var[1];
varNames[2:p] = WithVar[1:(p-1)];

/* convert the numbers into a symmetric array */
C = j(p,p,0);       /* create p x p matrix  */
upperIdx = loc(row(C)&amp;lt;col(C));  /* assign lower-triangula elements */
C[upperIdx] = Corr;
C = C + C`;                     /* assign upper-triangular elements */
diagIdx = loc(row(C)=col(C));
C[diagIdx] = 1;                 /* put 1s on the diagonal */

print C[r=VarNames c=VarNames];

/* optional: write to data set in TYPE=CORR format */
_TYPE_ = j(p, 1, "CORR");
_NAME_ = colvec(varNames);
/* The following statement is for 9.4M6 and later.
   For earlier releases, use two CREATE statements: 
   One for the _TYPE_ and _NAME_ vars and the other for the corr matrix */
create Want from _TYPE_ _NAME_ C[c=({'_TYPE_' '_NAME_'}||varNames)];
append from _TYPE_ _NAME_ C;
close;
quit;

proc print data=Want; run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 23 Sep 2022 13:40:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/generate-correlation-matrix-from-column-of-correlations/m-p/834694#M329960</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2022-09-23T13:40:52Z</dc:date>
    </item>
  </channel>
</rss>

