此问题部分与此question有关。填写滚动相关矩阵的缺失值
我的数据文件可以找到here。我使用2008年1月1日至2013年12月31日的样本期。数据文件没有缺失值。
以下代码使用前一年价值的滚动窗口在2008年1月1日至2013年12月31日的每一天生成滚动相关矩阵。例如,2008年1月1日的AUT
和BEL
之间的相关性使用2007年1月1日至2008年1月1日的一系列值计算,并且对于所有其他配对也是如此。
data work.rolling;
set mm.rolling;
run;
%macro rollingCorrelations(inputDataset=, refDate=);
/*first get a list of unique dates on or after the reference date*/
proc freq data = &inputDataset. noprint;
where date >="&refDate."d;
table date/out = dates(keep = date);
run;
/*for each date calculate what the window range is, here using a year's length*/
data dateRanges(drop = date);
set dates end = endOfFile
nobs= numDates;
format toDate fromDate date9.;
toDate=date;
fromDate = intnx('year', toDate, -1, 's');
call symputx(compress("toDate"!!_n_), put(toDate,date9.));
call symputx(compress("fromDate"!!_n_), put(fromDate, date9.));
/*find how many times(numberOfWindows) we need to iterate through*/
if endOfFile then do;
call symputx("numberOfWindows", numDates);
end;
run;
%do i = 1 %to &numberOfWindows.;
/*create a temporary view which has the filtered data that is passed to PROC CORR*/
data windowedDataview/view = windowedDataview;
set &inputDataset.;
where date between "&&fromDate&i."d and "&&toDate&i."d;
drop date;
run;
/*the output dataset from each PROC CORR run will be
correlation_DDMMMYYY<from date>_DDMMMYY<start date>*/
proc corr data = windowedDataview
outp = correlations_&&fromDate&i.._&&toDate&i. (where=(_type_ = 'CORR'))
noprint;
run;
%end;
/*append all datasets into a single table*/
data all_correlations;
format from to date9.;
set correlations_:
indsname = datasetname
;
from = input(substr(datasetname,19,9),date9.);
to = input(substr(datasetname,29,9), date9.);
run;
%mend rollingCorrelations;
%rollingCorrelations(inputDataset=rolling, refDate=01JAN2008)
输出的摘录可以找到here。
可以看出,第2行到第53行显示了2008年4月1日的相关矩阵。然而,2009年4月1日的相关矩阵出现了问题:ALPHA
有相关系数的缺失值,它的对。这是因为如果查看数据文件,则从2008年4月1日到2009年4月1日的ALPHA
的值都为零,因此导致除以零。这种情况也会发生在其他一些数据值上,例如,HSBC
也具有从08年4月1日到2009年4月1日0的所有值。
要解决此问题,我想知道上述代码如何修改即在发生这种情况的情况下(即在2个特定日期之间所有值都为0),则使用整个采样周期简单计算两对数据值之间的相关性。例如,上缺少09年4月1日ALPHA
和AUT
之间的相关性,因此该相关性应该使用的值从1 2008 JAN到2013年12月31日,而不是使用的值从08年4月1日至09年4月1日
您是否拥有ETS授权? – Joe
@Joe我不确定其实,我该如何检查? – user3184733
@ user3184733要检查您已授权的产品,您可以运行以下过程来检查许可证文件并将产品列表输出到日志中。然后简单地做一个'CTRL + F'搜索'SAS/ETS'。 'PROC SETINIT;运行;' – 2014-02-27 10:23:31