sas - 根据日期计算移动总和/平均值
问题描述
我的数据具有以下结构
data have;
infile datalines delimiter=' ';
input customerid : 8.
date : date9.
opens : 3.
;
datalines;
2123780 11APR2017 0
2123780 13APR2017 0
2123780 16APR2017 1
2123780 18APR2017 0
2123780 19APR2017 2
2123780 20APR2017 0
2123780 21APR2017 0
2123780 23APR2017 0
2123780 25APR2017 0
2123780 26APR2017 0
2123780 28APR2017 0
2123780 29APR2017 3
2123780 01MAY2017 3
2123780 03MAY2017 2
2123780 04MAY2017 5
2123780 05MAY2017 1
2123780 07MAY2017 2
2123780 09MAY2017 2
2123780 11MAY2017 3
2123780 13MAY2017 3
2123780 14MAY2017 0
2123780 16MAY2017 2
2123780 17MAY2017 2
;
run;
我想要实现的是opens
变量(以及更多)的移动总数、平均值、标准差等,其中包含当前观察之前的最后 7、14、30 天等内的值,通过customerid
. 如您所见,观察结果是不规则的。有时之间有很大的差距,有时在同一天有几个。因此,我不能使用PROC EXPAND
(如果我错了,请纠正我)。此外,我不想将我的日期压缩为每周一次的观察,而是保持原样。
我想出的解决方案是一段丑陋的LAG()
-coding 和 if 子句。一个变量和 7 天的示例:
%macro loop;
data want(drop= lag_kdnr_num -- lag_mahnung min7 -- min365 minimum);
set have;
week_opens=0;
%do i=1 %to 500;
lag_customerID=lag&i.(customerID);
date_7=lag&i.(date)+7;
lag_opens=lag&i(opens);
if ((customerID=lag_customerID) and (dsate < date_7)) then
do;
week_opens=sum(week_opens + lag_opens);
end;
%end;
min7=minimum + 7;
if date < min7 then
do;
week_opens=.;
end;
run;
%MEND;
%loop;
这给了我这个:
data want2;
infile datalines delimiter=' ';
input customerid : 8.
date : date9.
opens : 3.
week_opens : 3.
;
datalines;
2123780 11APR2017 0 .
2123780 13APR2017 0 .
2123780 16APR2017 1 .
2123780 18APR2017 0 1
2123780 19APR2017 2 1
2123780 20APR2017 0 3
2123780 21APR2017 0 3
2123780 23APR2017 0 2
2123780 25APR2017 0 2
2123780 26APR2017 0 0
2123780 28APR2017 0 0
2123780 29APR2017 3 0
2123780 01MAY2017 3 3
2123780 03MAY2017 2 6
2123780 04MAY2017 5 8
2123780 05MAY2017 1 13
2123780 07MAY2017 2 11
2123780 09MAY2017 2 10
2123780 11MAY2017 3 5
2123780 13MAY2017 3 7
2123780 14MAY2017 0 8
2123780 16MAY2017 2 6
2123780 17MAY2017 2 8
;
run;
由于数据量巨大,这真的很慢,并且会创建很多未使用的变量,我最后放弃了这些变量。是否有更快、更优雅的方法来获得此结果,例如通过临时数组或 SAS/ETS?先感谢您!
最后一句话:我想在生存分析中使用这些信息作为协变量。
解决方案
推荐阅读
- ssl - 如何使用服务器端配置(Nginx)强制浏览器获取新的 SSL 证书而不是旧的证书?
- java - 在 Observable 中超时时发出项目
- php - PHP - 需要帮助完成 Google Sheets API 请求
- c - 函数类似于 pipe(3) 但返回 FILE *
- javascript - 如何最好地将脚本从 PageSpeed Insights 中排除?
- powershell - 创建脚本以从 AD 获取用户特定的详细信息
- css - 如何垂直居中这个元素?
- python-2.7 - 如何在 sqlite3 中使用从列表中获取的列名创建一个表?
- javascript - 如何在javascript中检查页面的加载时间?
- android - 如何在android的中心设置Tabbar位置?