SAS:计算每日回报的滚动6个月偏差

时间:2018-01-14 20:41:18

标签: sql sas statistics

我正在尝试解决SAS中的以下问题。

我有几只股票的每日回报数据。这就是我的数据(简化):

PERMNO  DATE        RETURN
10078   2010JAN02   0.0500
10104   2010JAN02   -0.0190
10107   2010JAN02   0.0020
10078   2010JAN03   0.0040
10104   2010JAN03   -0.0400
10107   2010JAN03   0.0500
...       ...        ...
10078   2015JAN02   -0.0190
10104   2015JAN02   0.0100
10107   2015JAN02   0.0700
10078   2015JAN05   0.0500
10104   2010JAN03   -0.0190
10107   2010JAN03   0.0020

PERMNO识别股票,DATE识别日期(yyyymmmdd),RETURN是每日股票收益。 我仅针对三只股票(10078,10104,10107)简化了这个例子。

目标:我正在尝试计算给定月份中每个股票的滚动偏度。 我想用每日回报数据的前6个月(即t-6到t-1个月)来计算每个股票的每月偏度度量。因此,对于例如2010年7月,我希望该月的偏度度量基于2010年1月至2010年6月的每日回报。

我希望输出数据包含该月的PERMNO,月份ID和月度偏度度量(基于之前6个月的数据)。这是一张图片来说明我想要的输出:

PERMNO  DATE        6MONTH_SKEWNESS
10078   2010JUL30   0.7257
10104   2010JUL30   -0.7056
10107   2010JUL30   -0.6781
10078   2010AUG31   0.9999
10104   2010AUG31   -0.6719
10107   2010AUG31   -0.7056
...    ...            ...
10078   2015JUL30   -0.1651
10104   2015JUL30   0.1056
10107   2015JUL30   0.6181
10078   2015AUG31   -0.8886
10104   2015AUG31   0.6119
10107   2015AUG31   0.1056

我已广泛搜索网页并亲自尝试过,但我觉得这个问题真的很困难。提前感谢能够以任何方式提供帮助的任何人。

更新:这是我正在使用的修改后的代码。非常感谢user667489。

/*Join on the previous 6 months of data for each stock*/
proc sql;
create view rolling as
  select 
    a.PERMNO, /* PERMNO is the variable that identifies a stock */
    a.DATE as MONYR_A label="", /* DATE is a date variable for each daily return */
    b.DATE as MONYR_B label="",
    b.RET /* RET is the variable containing daily returns */
  from Dsex3 a left join Dsex3 b /* Dsex3 is the dataset that contains the identifiers for stocks and their daily returns */  
    on      a.PERMNO = b.PERMNO 
        and 1 <= intck('month',b.date,a.date) <= 6
  group by a.PERMNO, MONYR_A
  having count(*) >= 6 /*Only calculate skewness if we have at least 6 months of data*/
  order by a.PERMNO desc,MONYR_A,MONYR_B;
quit;


/*Calculate the skewness for the previous 6 months worth of data*/
ods listing close;
ods output moments=skewness_summary(
  keep = PERMNO MONYR_A label1 nvalue1
  where=(label1='Skewness')
  rename = (nvalue1 = skewness_6mth)
);
proc univariate data = rolling;
  var RET;
  by descending PERMNO MONYR_A;
run;
ods listing;

1 个答案:

答案 0 :(得分:0)

如果你没有SAS / ETS,你可以尝试这样的事情:

/*Generate some dummy data from the sashelp area*/
proc summary data = sashelp.prdsal2 nway;
  var ACTUAL;
  class COUNTRY STATE PRODUCT MONYR;
  output out = prdsal2(drop = _:) sum=;
run;

/*Join on the previous 6 months of data for each product*/
proc sql;
create view rolling as
  select 
    a.COUNTRY, 
    a.STATE, 
    a.PRODUCT, 
    a.MONYR as MONYR_A label="", 
    b.MONYR as MONYR_B label="", 
    b.ACTUAL
  from prdsal2 a left join prdsal2 b   
    on      a.COUNTRY = b.COUNTRY 
        and a.STATE   = b.STATE 
        and a.PRODUCT = b.PRODUCT
        and 1 <= intck('month',b.MONYR,a.MONYR) <= 6
  group by a.COUNTRY,a.STATE,a.PRODUCT,MONYR_A
  having count(*) >= 6 /*Only calculate skewness if we have at least 6 months of data*/
  order by a.COUNTRY desc,a.STATE,a.PRODUCT,MONYR_A,MONYR_B;
quit;

/*Calculate the skewness for the previous 6 months worth of data*/
ods listing close;
ods output moments=skewness_summary(
  keep = COUNTRY STATE PRODUCT MONYR_A label1 nvalue1 
  where=(label1='Skewness')
  rename = (nvalue1 = skewness_6mth)
);
proc univariate data = rolling;
  var ACTUAL;
  by descending COUNTRY STATE PRODUCT MONYR_A;
run;
ods listing;

我发现SAS有一个实际的skewness函数可以直接计算变量列表,所以这里也是一个数据步骤方法:

/*Data step approach, using the skewness function*/
data skewness_summary2;
do _n_ = 1 by 1 until(last.MONYR_A);
  set rolling(keep = COUNTRY STATE PRODUCT MONYR_A ACTUAL);
  by descending COUNTRY STATE PRODUCT MONYR_A;
  skewness_6mth = skewness(ACTUAL,lag1(ACTUAL),lag2(ACTUAL),lag3(ACTUAL),lag4(ACTUAL),lag5(ACTUAL));
  if _n_ >= 6 then output;
end;
run;

数据步骤方法似乎比使用proc单变量快一点,可能是因为proc也在计算所有其他时刻。