Sas 填充滚动相关矩阵的缺失值

Sas 填充滚动相关矩阵的缺失值,sas,Sas,这个问题部分与此有关 可以找到我的数据文件。我使用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 rollingCorrelatio

这个问题部分与此有关

可以找到我的数据文件。我使用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)
data-work.rolling;
设置为毫米滚动;
跑
%宏rollingCorrelations(inputDataset=,refDate=);
/*首先获取参考日期当天或之后的唯一日期列表*/
proc freq data=&inputDataset。无印;
其中日期>=“&refDate.”d;
表日期/截止日期=日期(保留日期=日期);
跑
/*对于每个日期,使用一年的长度计算窗口范围*/
数据日期范围(drop=日期);
设置日期结束=结束日期
nobs=numDates;
格式为从日期9开始的日期。;
toDate=日期;
fromDate=intnx('year',toDate,-1,'s');
调用symputx(compress(“toDate”!!,put(toDate,date9.));
调用symputx(compress(“fromDate”!!_n_),put(fromDate,date9.);
/*找到我们需要迭代的次数(numberOfWindows)*/
如果是endOfFile,那么就这样做;
调用symputx(“numberOfWindows”,numDates);
结束;
跑
%i=1%到&numberOfWindows。;
/*创建一个临时视图,其中包含传递给PROC CORR的筛选数据*/
数据窗口数据视图/视图=窗口数据视图;
设置并输入数据集。;
其中日期介于“&&fromDate&i.”d和“&&toDate&i.”d之间;
终止日期;
跑
/*每次PROC CORR运行的输出数据集将
相关性_DDMMMYYY_DDMMMYY*/
proc corr data=窗口数据视图
输出=相关性&&fromDate&i..&&toDate&i。(其中=(_type_='CORR'))
无印;
跑
%结束;
/*将所有数据集追加到单个表中*/
所有相关数据;
格式从到日期9。;
将相关性设置为:
indsname=datasetname
;
from=输入(substr(datasetname,19,9),date9.);
to=输入(substr(datasetname,29,9),date9.);
跑
%修正滚动相关性;
%滚动相关性(inputDataset=rolling,refDate=2008年1月1日)
可以找到输出的摘录

如图所示,第2行至第53行显示了2008年4月1日的相关矩阵。但是,2009年4月1日的相关矩阵出现了一个问题:
ALPHA
及其对的相关系数缺少值。这是因为如果查看数据文件,从2008年4月1日到2009年4月1日的
ALPHA
的值都是零,因此导致被零除。这种情况也发生在一些其他数据值上,例如,
HSBC
在2008年4月1日至2009年4月1日期间的所有值都为0


为了解决这个问题,我想知道如何修改上述代码,以便在出现这种情况的情况下(即,在两个特定日期之间的所有值均为0),可以使用整个采样周期简单地计算两对数据值之间的相关性。例如,2009年4月1日,
ALPHA
AUT
之间的相关性缺失,因此,应使用2008年1月1日至2013年12月31日的值来计算此相关性,而不是在运行上述宏并获得
所有相关性
数据集后使用2008年4月1日至2009年4月1日的值,这次您需要使用所有数据运行另一个
PROC CORR
,即

/*first filter the data to be between "01JAN2008"d and "31DEC2013"d*/
data work.all_data_01JAN2008_31DEC2013;
set mm.rolling;
where date between "01JAN2008"d and "31DEC2013"d;
drop date ;
run;
然后将上述数据集传递给
PROC CORR

proc corr data =  work.all_data_01JAN2008_31DEC2013
outp = correlations_01JAN2008_31DEC2013
 (where=(_type_ = 'CORR'))

        noprint;
run;
data correlations_01JAN2008_31DEC2013;
length id 8;
set correlations_01JAN2008_31DEC2013;
/*add a column identifier to make sure the order of the correlation matrix is preserved when joined with other tables*/
id = _n_;
run;
您将得到一个数据集,该数据集在
\u name\u
列中是唯一的。 然后,您必须将
相关性
连接到
所有相关性
,如果
所有相关性
中缺少一个值,则将
相关性
中的相应值插入其位置。为此,我们可以使用
procsql
&函数

PROC SQL;
CREATE TABLE MISSING_VALUES_IMPUTED AS 
SELECT
A.FROM
,A.TO
,b.id
,a._name_
,coalesce(a.AUT,b.AUT) as AUT
,coalesce(a.BEL,b.BEL) as BEL
,coalesce(a.DEN,b.DEN) as DEN
,coalesce(a.FRA,b.FRA) as FRA
,coalesce(a.GER,b.GER) as GER
,coalesce(a.GRE,b.GRE) as GRE
,coalesce(a.IRE,b.IRE) as IRE
,coalesce(a.ITA,b.ITA) as ITA
,coalesce(a.NOR,b.NOR) as NOR
,coalesce(a.POR,b.POR) as POR
,coalesce(a.SPA,b.SPA) as SPA
,coalesce(a.SWE,b.SWE) as SWE
,coalesce(a.NL,b.NL) as NL
,coalesce(a.ERS,b.ERS) as ERS
,coalesce(a.RZB,b.RZB) as RZB
,coalesce(a.DEX,b.DEX) as DEX
,coalesce(a.KBD,b.KBD) as KBD
,coalesce(a.DAB,b.DAB) as DAB
,coalesce(a.BNP,b.BNP) as BNP
,coalesce(a.CRDA,b.CRDA) as CRDA
,coalesce(a.KN,b.KN) as KN
,coalesce(a.SGE,b.SGE) as SGE
,coalesce(a.CBK,b.CBK) as CBK
,coalesce(a.DBK,b.DBK) as DBK
,coalesce(a.IKB,b.IKB) as IKB
,coalesce(a.ALPHA,b.ALPHA) as ALPHA
,coalesce(a.ALBK,b.ALBK) as ALBK
,coalesce(a.IPM,b.IPM) as IPM
,coalesce(a.BKIR,b.BKIR) as BKIR
,coalesce(a.BMPS,b.BMPS) as BMPS
,coalesce(a.PMI,b.PMI) as PMI
,coalesce(a.PLO,b.PLO) as PLO
,coalesce(a.BINS,b.BINS) as BINS
,coalesce(a.MB,b.MB) as MB
,coalesce(a.UC,b.UC) as UC
,coalesce(a.BCP,b.BCP) as BCP
,coalesce(a.BES,b.BES) as BES
,coalesce(a.BBV,b.BBV) as BBV
,coalesce(a.SCHSPS,b.SCHSPS) as SCHSPS
,coalesce(a.NDA,b.NDA) as NDA
,coalesce(a.SEA,b.SEA) as SEA
,coalesce(a.SVK,b.SVK) as SVK
,coalesce(a.SPAR,b.SPAR) as SPAR
,coalesce(a.CSGN,b.CSGN) as CSGN
,coalesce(a.UBSN,b.UBSN) as UBSN
,coalesce(a.ING,b.ING) as ING
,coalesce(a.SNS,b.SNS) as SNS
,coalesce(a.BARC,b.BARC) as BARC
,coalesce(a.HBOS,b.HBOS) as HBOS
,coalesce(a.HSBC,b.HSBC) as HSBC
,coalesce(a.LLOY,b.LLOY) as LLOY
,coalesce(a.STANBS,b.STANBS) as STANBS
from all_correlations as a
inner join correlations_01JAN2008_31DEC2013 as b
on a._name_ = b._name_
order by
A.FROM
,A.TO
,b.id
;
quit;
/*verify that no missing values are left. NMISS column should be 0 from all variables*/
proc means data = MISSING_VALUES_IMPUTED n nmiss;
run;

您有ETS许可证吗?@Joe我不确定,实际上,我如何检查?@user3184733要检查您已许可的产品,您可以运行以下过程,检查许可证文件并将产品列表输出到日志。然后只需执行
CTRL+F
即可搜索
SAS/ETS
<代码>进程SETINIT;运行感谢您的大力帮助。简洁易懂的答案!