楼主: FlyUFalcon
5633 10

一个很简单 的moving/rolling standard deviation 的问题 [推广有奖]

  • 6关注
  • 43粉丝

已卖:3份资源

教授

18%

还不是VIP/贵宾

-

威望
0
论坛币
2652 个
通用积分
18.0956
学术水平
54 点
热心指数
74 点
信用等级
53 点
经验
34243 点
帖子
1042
精华
0
在线时间
1267 小时
注册时间
2011-8-16
最后登录
2025-10-25

楼主
FlyUFalcon 在职认证  发表于 2015-6-27 04:12:38 |AI写论文
20论坛币
大家好,

我要在sas 里面算 (-2,+2 )天内数值的标准差, 我大概的数据格式如下

rolling.PNG

这里的难点在于每一天有不同数量的 数值, 因此我不能使用简单的moving standard 的算法, 例如:

PROC EXPAND DATA=TESTTEST OUT=MOVINGAVERAGE;CONVERT VAL=AVG / TRANSFORMOUT=(MOVSTD 5);
RUN;

请问大家有没有好的方法。 在此谢谢了



关键词:Deviation Standard rolling Moving ATION standard moving

沙发
yongyitian 发表于 2015-6-27 04:12:39
FlyUFalcon 发表于 2015-6-27 22:21
十分感谢你的回答。
sorry 我忘记说了。我现在第一列有个 _ric 栏(类似于ID )。
  1. /* try this */
  2. proc sort data=have;
  3.     by _ric date;
  4. run;
  5. data have1;
  6.     set have;
  7.     by _ric date;
  8.     retain gdate;
  9.     if first.date then gdate+1;
  10. run;
  11. proc sql noprint;
  12.     create table want (drop=gdate) as
  13.     select distinct a._ric, a.gdate, a.date, a.value,  
  14.             std(b.value) as std_value  format=5.1,
  15.            mean(b.value) as mean_value format=5.1
  16.     from have1 a, have1 b
  17.     where a.gdate - 2 <= b.gdate <= a.gdate + 2
  18.     group by a._ric, a.gdate
  19.     order by a._ric, a.gdate;
  20. quit;
复制代码
已有 1 人评分论坛币 收起 理由
jerker + 5 热心帮助其他会员

总评分: 论坛币 + 5   查看全部评分

藤椅
yongyitian 发表于 2015-6-27 07:44:37
  1. data have;
  2.    do date = '01Jan2015'd to '31Jan2015'd;
  3.       value = ceil(ranuni(12345)*20); output;
  4.       value = ceil(ranuni(12345)*20); output;
  5.         end;
  6.         format date ddmmyy10.;
  7. run;

  8. proc sql;
  9.     create table want as
  10.     select distinct  a.date, a.value, std(b.value) as std_value,
  11.                                      mean(b.value) as mean_value format=5.1
  12.         from have a, have b
  13.         where a.date-2 <= b.date <= a.date+2
  14.         group by a.date
  15.         order by a.date;
  16. quit;
复制代码
已有 3 人评分论坛币 学术水平 热心指数 收起 理由
jerker + 5 精彩帖子
67890 + 1 + 1 精彩帖子
FlyUFalcon + 4 精彩帖子

总评分: 论坛币 + 5  学术水平 + 5  热心指数 + 1   查看全部评分

板凳
FlyUFalcon 在职认证  发表于 2015-6-27 20:53:39
yongyitian 发表于 2015-6-27 07:44
谢谢你的答案。

是这个样子的, 我的code 和你的很类似。 但是 我的原数据中的日期不是连续的, 只有工作日。因此day-2,day+2 这里应该调整一下。 我只想把过去的两个工作日纳入我的计算。 请问有没有好的方法?谢谢了   

报纸
yongyitian 发表于 2015-6-27 21:59:14
FlyUFalcon 发表于 2015-6-27 20:53
谢谢你的答案。

是这个样子的, 我的code 和你的很类似。 但是 我的原数据中的日期不是连续的, 只有 ...
  1. data have;
  2. input date mmddyy10. value;
  3. format date yymmdd10.;
  4. datalines;
  5. 01/01/2015   18
  6. 01/01/2015   15
  7. 01/01/2015   5
  8. 02/01/2015   66
  9. 02/01/2015   7
  10. 03/01/2015   7
  11. 04/01/2015   19
  12. 04/01/2015   7
  13. 04/01/2015   11
  14. 04/01/2015   17
  15. 05/01/2015   3
  16. 06/01/2015   7
  17. ; run;

  18. proc sort data=have;
  19.     by date;
  20. run;
  21. data have1;
  22.     set have;
  23.     by date;
  24.     retain gdate;
  25.     if first.date then gdate+1;
  26. run;
  27. proc sql noprint;
  28.     create table want (drop=gdate) as
  29.     select distinct a.gdate, a.date, a.value,  
  30.             std(b.value) as std_value  format=5.1,
  31.            mean(b.value) as mean_value format=5.1
  32.     from have1 a, have1 b
  33.     where a.gdate - 2 <= b.gdate <= a.gdate + 2
  34.     group by a.gdate
  35.     order by a.gdate;
  36. quit;
复制代码

地板
FlyUFalcon 在职认证  发表于 2015-6-27 22:21:28
yongyitian 发表于 2015-6-27 21:59
十分感谢你的回答。
sorry 我忘记说了。我现在第一列有个 _ric 栏(类似于ID )。

我现在只想在每个_ric 之内做(-2,+2)的运算。 如果使用您的code,_ric 之间的运算会有交叉。



请问有更好的方法吗? 十分感谢


rolling.PNG

7
ydb8848 发表于 2015-6-28 08:31:18
学习了。。。。呵呵

8
ryuuzt 发表于 2015-6-28 09:33:28
5天的标准差有意义吗?

9
FlyUFalcon 在职认证  发表于 2015-6-28 21:45:11
ryuuzt 发表于 2015-6-28 09:33
5天的标准差有意义吗?
为什么没有意义?

10
xinzhiyuanwmj 发表于 2015-7-15 13:33:38

proc sort data=have;
    by id date;
run;
data have1;
    set have;
    by id date;
       
        if first.id then do;
                                                if first.date then do;
                                                                                                gdate=0;
                                                                                                gdate+1;
                                                                                                retain gdate;
                                                                                        end;
   
                                         end;
   else do;
                           if first.date then gdate+1;
                        else do ;retain gdate;end;
                   end;


run;

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-27 04:35