Rolling Correlation Window

Joined
7/30/10
Messages
5
Points
11
First time poster here.

Guys I am really struggling with a code that will be able to roll correlation for multiple assets in SAS.

I.e. I want to create rolling correlation (based on asset returns) for each of the stocks in the S&P100. I have the data and know how to compare say stock 1 and stock 2, but any idea of how to do it for all constituents?

I realize this output would be quite massive, but I am just quite stuck on this code.
 
Not up on SAS, but it's just a nested loop of the time-series data. doing the same 1 vs 2 correlation you do, just iteratively, and store it in a matrix or other data structure..You can cut the duplication, depending how you structure it, you really only need to do (n^2-n)/2 correlation calcs.

pseudo:

loop A for security 1->N
{ loop B from 1->N (or 1->N | B <a)>less than A)
{correl(A,B)
save data somewhere
}
}</a)>
 
According to one of the SAS forum pages, this is how to calculate a rolling correlation window based on two assets xi and yi:

data cma ; set cma;
OBS = 1;
if missing(xi) OR missing(yi) then
do;
xi = . ;
yi = . ;
obs = . ;
end;
PRODXIYI = yi * xi;
run;

proc expand DATA = cma OUT = cmaout;
convert yi = YISUM / METHOD = none TRANSFORMOUT = (cmovsum 55);
convert xi = XISUM / METHOD = none TRANSFORMOUT = (cmovsum 55);
convert prodxiyi = PRODXIYISUM / METHOD = none TRANSFORMOUT = (cmovsum 55);
convert obs = N / METHOD = none TRANSFORMOUT = (cmovsum 55);
convert xi = XICSS / METHOD = none TRANSFORMOUT = (cmovcss 55);
convert yi = YICSS / METHOD = none TRANSFORMOUT = (cmovcss 55);
run;

data results; set cmaout;
R = (prodxiyisum - (yisum*xisum)/n) / ( sqrt(xicss)*sqrt(yicss)) ;
run;

So if I need to do it for each of the assets (100 stocks), how do I include the loop. I am not very strong at SAS, maybe someone can help me out. By the way the above code was for a 55-day rolling window.
 
Back
Top Bottom