<?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: Correlations using the previous three-year observations in SAS Data Science</title>
    <link>https://communities.sas.com/t5/SAS-Data-Science/Correlations-using-the-previous-three-year-observations/m-p/253083#M3752</link>
    <description>&lt;P&gt;You could calculate the correlations in a data step. This doesn't look very elegant, but is fairly straightforward.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Create test data */
data have;
input CoID Year VAR1 VAR2;
cards;
1 1996 10 10
1 1997 15 11
1 1998 20 12
1 1999 25 11
2 1996 23 25
2 1997 24 24
2 1998 25 23
2 1999 80 23
3 1996 40 30
3 1998 45 31
3 1999 46 36
;

/* Compute correlations for 3 consecutive years */
data want;
set have;
by CoID year;
if first.CoID then y=1;
else y+1;
lag11=lag1(var1);
lag21=lag2(var1);
lag12=lag1(var2);
lag22=lag2(var2);
if y&amp;gt;=3 &amp;amp; lag(year)=year-1 &amp;amp; lag2(year)=year-2 &amp;amp; n(of lag:, var1, var2)=6 then do;
  mean1=mean(var1, lag11, lag21);
  mean2=mean(var2, lag12, lag22);
  std1=std(var1, lag11, lag21);
  std2=std(var2, lag12, lag22);
  if std1 &amp;amp; std2 then corr3y=(( var1-mean1)*( var2-mean2)
                             +(lag11-mean1)*(lag12-mean2)
                             +(lag21-mean1)*(lag22-mean2))/2
                             /(std1*std2);
end;
drop y lag: mean: std:;
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please note that the above code computes the correlations only if the three years are consecutive and all six values involved are non-missing. Feel free to weaken these conditions (and adapt the formula accordingly) if you think this is too restrictive.&lt;/P&gt;</description>
    <pubDate>Sun, 28 Feb 2016 23:07:24 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2016-02-28T23:07:24Z</dc:date>
    <item>
      <title>Correlations using the previous three-year observations</title>
      <link>https://communities.sas.com/t5/SAS-Data-Science/Correlations-using-the-previous-three-year-observations/m-p/253048#M3751</link>
      <description>&lt;P&gt;I have data as followed:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Co. ID &amp;nbsp; &amp;nbsp;Year &amp;nbsp; &amp;nbsp;VAR1 &amp;nbsp; &amp;nbsp;VAR2&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1996 &amp;nbsp; &amp;nbsp; a &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;b&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1997 &amp;nbsp; &amp;nbsp;c &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;d&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1998 &amp;nbsp; &amp;nbsp;e &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;f&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1999 &amp;nbsp; &amp;nbsp;g &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; h&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1996 &amp;nbsp; &amp;nbsp;i &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;j&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1997 &amp;nbsp; &amp;nbsp; k &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; l&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1998 &amp;nbsp; &amp;nbsp; m &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; n&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1999 &amp;nbsp; &amp;nbsp;o &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;p&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to calculate the correlation between var1 and var2 using the previous three-year observations for the company and output to a file, e.g., for year 1998 company 1, I want to use company 1 year 1996-1998 observations of var1 and var2 to calculate the correlation between var1 and var2. I know I can use proc corr like this:&lt;/P&gt;&lt;P&gt;proc sort data=have;&lt;/P&gt;&lt;P&gt;by co. ID year;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;proc corr data=have out=want noprint;&lt;/P&gt;&lt;P&gt;var var1 var2;&lt;/P&gt;&lt;P&gt;by co.ID;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But this will give me the correlation using all the available observations for each company. Please help!!!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 28 Feb 2016 18:07:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Science/Correlations-using-the-previous-three-year-observations/m-p/253048#M3751</guid>
      <dc:creator>OceanDream</dc:creator>
      <dc:date>2016-02-28T18:07:08Z</dc:date>
    </item>
    <item>
      <title>Re: Correlations using the previous three-year observations</title>
      <link>https://communities.sas.com/t5/SAS-Data-Science/Correlations-using-the-previous-three-year-observations/m-p/253083#M3752</link>
      <description>&lt;P&gt;You could calculate the correlations in a data step. This doesn't look very elegant, but is fairly straightforward.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Create test data */
data have;
input CoID Year VAR1 VAR2;
cards;
1 1996 10 10
1 1997 15 11
1 1998 20 12
1 1999 25 11
2 1996 23 25
2 1997 24 24
2 1998 25 23
2 1999 80 23
3 1996 40 30
3 1998 45 31
3 1999 46 36
;

/* Compute correlations for 3 consecutive years */
data want;
set have;
by CoID year;
if first.CoID then y=1;
else y+1;
lag11=lag1(var1);
lag21=lag2(var1);
lag12=lag1(var2);
lag22=lag2(var2);
if y&amp;gt;=3 &amp;amp; lag(year)=year-1 &amp;amp; lag2(year)=year-2 &amp;amp; n(of lag:, var1, var2)=6 then do;
  mean1=mean(var1, lag11, lag21);
  mean2=mean(var2, lag12, lag22);
  std1=std(var1, lag11, lag21);
  std2=std(var2, lag12, lag22);
  if std1 &amp;amp; std2 then corr3y=(( var1-mean1)*( var2-mean2)
                             +(lag11-mean1)*(lag12-mean2)
                             +(lag21-mean1)*(lag22-mean2))/2
                             /(std1*std2);
end;
drop y lag: mean: std:;
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please note that the above code computes the correlations only if the three years are consecutive and all six values involved are non-missing. Feel free to weaken these conditions (and adapt the formula accordingly) if you think this is too restrictive.&lt;/P&gt;</description>
      <pubDate>Sun, 28 Feb 2016 23:07:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Science/Correlations-using-the-previous-three-year-observations/m-p/253083#M3752</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-02-28T23:07:24Z</dc:date>
    </item>
    <item>
      <title>Re: Correlations using the previous three-year observations</title>
      <link>https://communities.sas.com/t5/SAS-Data-Science/Correlations-using-the-previous-three-year-observations/m-p/253117#M3753</link>
      <description>&lt;P&gt;Assuming there is no gap between two obs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input CoID    Year    VAR1    VAR2 ;
cards;
1             1996     2            5
1              1997    4            8
1              1998    21            32
1              1999    42           6
2               1996    7            4
2              1997     8           14
2              1998     9         21
2               1999    12          8
;
run;

%let window_size=3;
data temp;
 set have;
 by CoID;
 array x{&amp;amp;window_size} _temporary_;
 array y{&amp;amp;window_size} _temporary_;
 if first.CoID then do;n=0;call missing(of x{*} y{*});end;
 n+1;
 x{mod(n,&amp;amp;window_size)+1}=var1;
 y{mod(n,&amp;amp;window_size)+1}=var2;
 if n ge &amp;amp;window_size then do;
  do i=1 to &amp;amp;window_size;
   var1=x{i};
   var2=y{i};
   output;
  end;
 end;
run;
proc corr data=temp out=want noprint;
var var1 var2;
by CoID Year    ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 29 Feb 2016 02:49:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Science/Correlations-using-the-previous-three-year-observations/m-p/253117#M3753</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-02-29T02:49:08Z</dc:date>
    </item>
  </channel>
</rss>

