在SAS中使用do循环计算移动平均值 [英] Calculating moving average using do loop in SAS

查看:355
本文介绍了在SAS中使用do循环计算移动平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到一种使用SAS do循环计算移动平均值的方法.我有困难.我本质上是想计算4个单位的移动平均值.

I am trying to find a way to calculate a moving average using SAS do loops. I am having difficulty. I essentially want to calculate a 4 unit moving average.

DATA data;
   INPUT a b;
   CARDS;
1 2
3 4 
5 6 
7 8 
9 10
11 12
13 14
15 16
17 18
;
run;    

data test(drop = i);
   set data;
   retain c 0;
   do i = 1 to _n_-4;
      c = (c+a)/4;
   end;
run;

proc print data = test;
run;

推荐答案

一种选择是使用预先合并:

One option is to use the merge-ahead:

DATA have;
 INPUT a b;
CARDS;
1 2
3 4 
5 6 
7 8 
9 10
11 12
13 14
15 16
17 18
;
run;

data want;
merge have have(firstobs=2 rename=a=a_1) have(firstobs=3 rename=a=a_2) have(firstobs=4 rename=a=a_3);
c = mean(of a:);
run;

每当合并的数据集前进一个时,就将数据合并到自身中-因此第二个以2开头,第三个以3开头,依此类推.这样一来,所有4个"a"就全部显示了.

Merge the data to itself, each time the merged dataset advancing one - so the 2nd starts with 2, third starts with 3, etc. That gives you all 4 'a' on one line.

这篇关于在SAS中使用do循环计算移动平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆