<?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 Create correlation for specific pair of variable and value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-correlation-for-specific-pair-of-variable-and-value/m-p/341854#M78311</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I want to calculate correlation for each pair of (variable and value) specified in a seperate file.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I show the code for 1 combination so you know what I am trying to do.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So could you please help me with that?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Many thanks,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have; 
input date a b c d;
datalines;
1 4 4 5 5
2 4 4 5 9
3 4 5 5 0
4 3 6 8 9
5 3 5 0 0
6 4 5 1 2
7 6 5 0 1
;run;

data have_var; 
input a_name $ a_value;
datalines;
a 4
b 5
c 9
d 9
;run;

** RUN FOR THE FIRST 2 CONDITIONS-------------;
data w1; set have;
if a=4 OR b=5; run;

proc corr data=w1 outp=cor1;
var a b; run;

data cor1; set cor1;
keep var1 var2 corr;
lname=lag(_name_);
if _N_=5;
var1=_name_; 
var2=lname;
corr=a; run;
*---------------------------------------------;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 17 Mar 2017 04:13:20 GMT</pubDate>
    <dc:creator>hhchenfx</dc:creator>
    <dc:date>2017-03-17T04:13:20Z</dc:date>
    <item>
      <title>Create correlation for specific pair of variable and value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-correlation-for-specific-pair-of-variable-and-value/m-p/341854#M78311</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I want to calculate correlation for each pair of (variable and value) specified in a seperate file.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I show the code for 1 combination so you know what I am trying to do.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So could you please help me with that?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Many thanks,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have; 
input date a b c d;
datalines;
1 4 4 5 5
2 4 4 5 9
3 4 5 5 0
4 3 6 8 9
5 3 5 0 0
6 4 5 1 2
7 6 5 0 1
;run;

data have_var; 
input a_name $ a_value;
datalines;
a 4
b 5
c 9
d 9
;run;

** RUN FOR THE FIRST 2 CONDITIONS-------------;
data w1; set have;
if a=4 OR b=5; run;

proc corr data=w1 outp=cor1;
var a b; run;

data cor1; set cor1;
keep var1 var2 corr;
lname=lag(_name_);
if _N_=5;
var1=_name_; 
var2=lname;
corr=a; run;
*---------------------------------------------;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Mar 2017 04:13:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-correlation-for-specific-pair-of-variable-and-value/m-p/341854#M78311</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2017-03-17T04:13:20Z</dc:date>
    </item>
    <item>
      <title>Re: Create correlation for specific pair of variable and value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-correlation-for-specific-pair-of-variable-and-value/m-p/341867#M78316</link>
      <description>&lt;P&gt;How are you defining the conditions in the second data set?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Mar 2017 05:40:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-correlation-for-specific-pair-of-variable-and-value/m-p/341867#M78316</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-03-17T05:40:13Z</dc:date>
    </item>
    <item>
      <title>Re: Create correlation for specific pair of variable and value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-correlation-for-specific-pair-of-variable-and-value/m-p/341931#M78340</link>
      <description>&lt;PRE&gt;
It is easy for IML.





data have; 
input date a b c d;
datalines;
1 4 4 5 5
2 4 4 5 9
3 4 5 5 0
4 3 6 8 9
5 3 5 0 0
6 4 5 1 2
7 6 5 0 1
;run;

data have_var; 
input a_name $ a_value;
datalines;
a 4
b 5
c 9
d 9
;run;

proc iml;
use have(drop=date);
read all var _all_ into have[c=vname];
close;

use have_var;
read all var{a_name a_value};
close;

n=nrow(a_name);
do i=1 to n-1;
 do j=i+1 to n;
   temp=have[,a_name[i]]||have[,a_name[j]];
   idx=loc(temp[,1]=a_value[i] | temp[,2]=a_value[j]);
   corr=corr(temp[idx,]);
   var1=var1//a_name[i];
   var2=var2//a_name[j];
   cor=cor//corr[2];
 end;
end;

create want var {var1 var2 cor};
append;
close;
quit;

proc print noobs;run;

&lt;/PRE&gt;</description>
      <pubDate>Fri, 17 Mar 2017 11:22:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-correlation-for-specific-pair-of-variable-and-value/m-p/341931#M78340</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-03-17T11:22:29Z</dc:date>
    </item>
    <item>
      <title>Re: Create correlation for specific pair of variable and value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-correlation-for-specific-pair-of-variable-and-value/m-p/341955#M78347</link>
      <description>&lt;P&gt;Thank you so much.&lt;/P&gt;
&lt;P&gt;HC&lt;/P&gt;</description>
      <pubDate>Fri, 17 Mar 2017 12:59:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-correlation-for-specific-pair-of-variable-and-value/m-p/341955#M78347</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2017-03-17T12:59:42Z</dc:date>
    </item>
    <item>
      <title>Re: Create correlation for specific pair of variable and value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-correlation-for-specific-pair-of-variable-and-value/m-p/342691#M78577</link>
      <description>Hi Ksharp,&lt;BR /&gt;Can you please help me to include the information of Total_N (number of record) and N_non_missing (number of record that has both value to be used to calculate correlation)?&lt;BR /&gt;I am totally blank with that method.&lt;BR /&gt;Thanks a lot.&lt;BR /&gt;HHC</description>
      <pubDate>Mon, 20 Mar 2017 17:02:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-correlation-for-specific-pair-of-variable-and-value/m-p/342691#M78577</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2017-03-20T17:02:14Z</dc:date>
    </item>
    <item>
      <title>Re: Create correlation for specific pair of variable and value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-correlation-for-specific-pair-of-variable-and-value/m-p/342814#M78612</link>
      <description>&lt;PRE&gt;
OK. Here is.



data have; 
input date a b c d;
datalines;
1 4 4 5 5
2 . 5 5 9
3 4 5 5 0
4 3 6 8 9
5 3 5 0 0
6 4 5 1 2
7 6 5 0 1
;run;

data have_var; 
input a_name $ a_value;
datalines;
a 4
b 5
c 9
d 9
;run;

proc iml;
use have(drop=date);
read all var _all_ into have[c=vnames];
close;

use have_var;
read all var{a_name a_value};
close;

n=nrow(a_name);
do i=1 to n-1;
 do j=i+1 to n;
   temp=have[,a_name[i]]||have[,a_name[j]];
   idx=loc(temp[,1]=a_value[i] | temp[,2]=a_value[j]);
   want=temp[idx,];
   corr=corr(want);
   var1=var1//a_name[i];
   var2=var2//a_name[j];
   cor=cor//corr[2];
   Total_N=Total_N//nrow(want);
   N_non_missing=N_non_missing//ncol(loc(countmiss(want,'row')=0));
 end;
end;

create want var {var1 var2 cor Total_N N_non_missing};
append;
close;
quit;



Actually , you could use CALL EXECUTE() to get it.
make a macro to hold your first code and;


data _null_;
 set have_var end=last;
 array name{99999} $ _temporary_;
array value{99999} _temporary_;

name{_n_}=a_name;
value{_n_}=a_value;

if last then do;
 do i=1 to _n_-1;
 do j=i+1 to _n_;
    call execute(YOUR MACRO);
 end;
 end;
end;
run;



&lt;/PRE&gt;</description>
      <pubDate>Tue, 21 Mar 2017 02:15:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-correlation-for-specific-pair-of-variable-and-value/m-p/342814#M78612</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-03-21T02:15:47Z</dc:date>
    </item>
  </channel>
</rss>

