在使用SAS做投资策略的时候,面对一系列的时间序列数据,我们需要对这些数据求移动平均,移动求和,最近N个的最大值,最小值等等,如果自己用SAS data步编程过程比较繁琐,下面为大家介绍一个神奇但鲜为人知的过程步-proc expand。
基本常用语法:
proc expand data=YourData Out=YourData method=option;
by variable;
convert variable1= variable2/ transformout = (option);
run;
基本语法大致如此,
data=你的数据集,Out为输出的数据集,method指定在数据间的差值方法,默认的方法是,一般来说我们设为method=none;
by variable 表示按变量variable 分组运算。
convert variable1= variable2 表示要将变量1 转换为变量2,至于如何转换 则要看transformout。
transformout = (option); option指定转换的方法和参数。
下面举例说明
例1:LAGS 和 LEADS
proc sort data=sashelp.Stocks;
by Stock;
run;
proc expand data=sashelp.Stocks Out=Stocks method=none;
by Stock;
convert close = lead_close / transformout = (lead 1);
convert close = lag_close / transformout = (lag 1);
run;
例子2 求最近N个观测的最大值
proc expand data=sashelp.Stocks Out=Stocks method=none;
by Stock;
convert close = max_close / transformout = (movmax 5);
convert close = min_close / transformout = (movmin 5);
run;
未完待续
更多精彩 尽在公众号:SAS与量化投资