From: Nickname unavailable on
Can someone please help me calculate a rolling correlation coefficient
stat=
in SAS? I need to calculate the correlation rolling every 5 years. So
for =
the years 1990-93 there would be no correlation. For 1994, the
correlation =
would be based on data from 1990-94, for 1995 it would be from 1991-95
and =
so on. Any help would be appreciated. Thanks

I have the following data. the correlation is between x and y

id year x y
1 1990 0.1 0.2
1 1991 0.4 0.3
1 1992 0.5 0.6
..
..
..
1 2007 0.5 0,4
2 1990 0.6 0.6
2 1991 0.7 0.8
2 1992 0.7 0.6
From: Eli Y. Kling on
Here is one Classic, resource wastful approach;

data Test;
set sashelp.class;
do year=2001 to 2010;
output;
end;
run;

%Macro EliLoop;
%Do FromYear=2001 %to 2005;
%Let ToYear=%sysevalf(&FromYear.+5);
proc corr data=test noprint outp=outp(where=(_type_="CORR" and
_Name_="Height") rename=(Weight=Pearson));
where &FromYear.<=year<=&ToYear.;
var Height Weight;
run;
data outp;
length Set $20.;
set outp;
Set="&FromYear.<=year<=&ToYear.";
keep Set Pearson;
run;
proc append base=RollingCorrelations data=outp force;run;
%End;
%Mend;
proc delete data=RollingCorrelations;run;
%EliLoop;

From: Muthia Kachirayan on
Hi,

This is a rolling question !

The solution below works without BY Grouping.

It is built on using the least memory and would be faster than any of the
Procs.


*** Rolling Correlation coefficient;
%let wsize=5;
data need(keep = x y r);
array xx(&wsize) (&wsize * 0);
array yy(&wsize) (&wsize * 0);
retain xx : yy :;
x2remove = xx(mod(_n_ - 1, &wsize) + 1);
y2remove = yy(mod(_n_ - 1, &wsize) + 1);
set sashelp.class(keep = weight height rename = (weight = y height = x)) ;
xx(mod(_n_ - 1, &wsize) + 1) = x;
yy(mod(_n_ - 1, &wsize) + 1) = y;
sumx + x - x2remove ;
sumxsq + x**2 - x2remove**2;
sumy + y - y2remove;
sumysq + y**2 - y2remove**2;
sumxy + x * y - x2remove * y2remove;
if _n_ >= &wsize then
r = (sumxy - sumx * sumy / &wsize)/sqrt((sumxsq - sumx ** 2 / &wsize) *
(sumysq - sumy ** 2 / &wsize));
run;

Muthia Kachirayan
On Mon, Feb 1, 2010 at 7:33 PM, Nickname unavailable <
stinaikar_2000(a)yahoo.com> wrote:

> Can someone please help me calculate a rolling correlation coefficient
> stat=
> in SAS? I need to calculate the correlation rolling every 5 years. So
> for =
> the years 1990-93 there would be no correlation. For 1994, the
> correlation =
> would be based on data from 1990-94, for 1995 it would be from 1991-95
> and =
> so on. Any help would be appreciated. Thanks
>
> I have the following data. the correlation is between x and y
>
> id year x y
> 1 1990 0.1 0.2
> 1 1991 0.4 0.3
> 1 1992 0.5 0.6
> .
> .
> .
> 1 2007 0.5 0,4
> 2 1990 0.6 0.6
> 2 1991 0.7 0.8
> 2 1992 0.7 0.6
>