2014-02-26 3 views
0

이 질문은 부분적으로이 question과 관련이 있습니다.롤링 상관 행렬 누락 값 채우기

내 데이터 파일은 here입니다. 2008 년 1 월 1 일부터 2013 년 12 월 31 일까지 샘플 기간을 사용합니다. 데이터 파일에는 누락 된 값이 없습니다.

다음 코드는 이전 1 년 분량의 롤링 윈도우를 사용하여 2008 년 1 월 1 일부터 2013 년 12 월 31 일까지 매일 롤링 상관 행렬을 생성합니다. 예를 들어 2008 년 1 월 1 일에 AUTBEL 사이의 상관 관계는 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의 값이 모두 0이므로 0으로 나누기 때문입니다. 이 상황은 몇 가지 다른 데이터 값에서도 발생합니다. 예를 들어, HSBC은 4 월 08 일부터 1 월 9 일까지 0으로 모든 값을 갖습니다.

위의 코드를 어떻게 수정할 수 있는지 궁금합니다. 이 상황이 발생하는 경우 (즉, 모든 값이 2 개의 특정 날짜 사이에 0 인 경우), 두 쌍의 데이터 값 간의 상관 관계는 전체 샘플 기간을 사용하여 간단히 계산됩니다. 예를 들어 ALPHAAUT 사이의 상관 관계가 4 월 1 일에 누락되어 있으므로이 상관 관계는 1 Apr 08에서 1 Apr 09까지의 값을 사용하는 대신 1 월 08 일부터 1 월 13 일까지의 값을 사용하여 계산해야합니다.

+0

ETS 라이선스가 있습니까? – Joe

+0

@Joe 나는 실제로 확인하지 않습니다. 어떻게 확인하나요? – user3184733

+0

@ user3184733 라이센스가있는 제품을 확인하려면 라이센스 파일을 확인하고 제품 목록을 로그에 출력하는 다음 절차를 실행할 수 있습니다. 그런 다음 'SAS/ETS'를 검색하기 위해 CTRL + F를 누르십시오. '프로 시틴 (PROC SETINIT); RUN; ' –

답변

1

위의 매크로를 실행하고 all_correlations 데이터 집합을 가지고하면, 당신은

/*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이 시간을 실행해야합니다 :

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; 

_name_ 열에서 고유 한 데이터 집합을 얻습니다. 그런 다음 all_correlations에 값이 누락되면 correlations_01JAN2008_31DEC2013의 값이 해당 위치에 삽입되는 방식으로 correlations_01JAN2008_31DEC2013all_correlations에 가입시켜야합니다. 이를 위해 PROC SQL & COALESCE 기능을 사용할 수 있습니다.

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; 
+0

엄청난 도움에 감사드립니다. 간결하고 쉬운 답변을 따르십시오! – user3184733