首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >不规则时间序列的Proc展开

不规则时间序列的Proc展开
EN

Stack Overflow用户
提问于 2021-10-27 10:04:53
回答 2查看 323关注 0票数 0

如何使用PROC展开计算有中断的时间序列的移动平均值?我正在寻找一种更有效的方法来完成这一计算,因为由于服务器的限制,大型数据集上的数据步骤和连接需要很长时间才能执行。

数据:

代码语言:javascript
运行
复制
data have(drop=i);
call streaminit(1);
do i = 1 to 20;
    period = i;
    if i > 10 then period = i + 5;
    if i >17 then period = i + 6;
    x = round(rand('uniform')*10,1.);
    output;
end;
run;

│ period │ x  │
├────────┼────┤
│ 1      │ 9  │
│ 2      │ 10 │
│ 3      │ 5  │
│ 4      │ 9  │
│ 5      │ 7  │
│ 11     │ 9  │
│ 12     │ 9  │
│ 13     │ 5  │
│ 15     │ 8  │
│ 16     │ 9  │

注意,周期变量中有两个断点: 5-11和13-15。

以下是预期结果(3个月移动平均数):

代码语言:javascript
运行
复制
Proc sql;
create table want as
select a.period, a.x
      ,mean(b.x) as x_avg format=10.2
from have as a
left join have as b
    on a.period -3 < b.period <= a.period
group by 1,2;
Quit;

│ period │ x  │ x_avg │
├────────┼────┼───────┤
│ 1      │ 9  │ 9.00  │
│ 2      │ 10 │ 9.50  │
│ 3      │ 5  │ 8.00  │
│ 4      │ 9  │ 8.00  │
│ 5      │ 7  │ 7.00  │
│ 11     │ 9  │ 9.00  │
│ 12     │ 9  │ 9.00  │
│ 13     │ 5  │ 7.67  │
│ 15     │ 8  │ 6.50  │
│ 16     │ 9  │ 8.50  │
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-10-27 14:48:17

使用proc timeseries在每个间隙之间添加缺失值,然后通过proc expandmethod=none运行它。我们将每天考虑这个间隔,因为它一次递增一个值。筛选出最后的数据集,使其不丢失x的值。

代码语言:javascript
运行
复制
proc timeseries data = have
                out  = have_ts;
    id period interval=day setmissing=missing;
    var x;
run;

proc expand data = have_ts
            out  = want(where=(NOT missing(x)) );
    id period;
    convert x = x_avg / method=none transform=(moveave 3);
run;

您需要用period8.格式化为proc datasets,因为proc timeseries需要将其视为日期。

代码语言:javascript
运行
复制
proc datasets lib=work nolist;
    modify want;
        format x 8.;
quit;
票数 1
EN

Stack Overflow用户

发布于 2021-10-27 14:46:28

您可以通过稍加修改使SQL更快。

代码语言:javascript
运行
复制
proc sql noprint;
  create table want2 as
  select a.period, a.x ,mean(b1.x,b2.x,a.x) as x_avg format=10.2
  from have as a
  left join have as b1 on a.period -2 = b1.period
  left join have as b2 on a.period -1 = b2.period
  order by a.period;
quit;

更快的数据步骤。

代码语言:javascript
运行
复制
data want3;
  set have;

  period_l2 = lag2(period);
  period_l1 = lag(period);
  x_l2 = ifn(period_l2=period-2,lag2(x),ifn(period_l1=period-2,lag(x),.));
  x_l1 = ifn(period_l1=period-1,lag(x),.);
  x_avg = mean(x_l2,x_l1,x);
run;

如果系列的长度不再是3,请使用数组和mean(of _array_[*])来帮助自己。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69736644

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档