全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
2987 4
2016-06-18
悬赏 10 个论坛币 已解决
data test3;
input Acct_ID 1. Month 3. c 5.;
datalines;
1 1 10
1 2 20
1 3 30
1 4 40
2 1 10
2 2 20
2 3 30
3 1 10
3 2 20
4 1 10
;
run;
目标是把超过3个月的ACCT_ID 取最后三个月的平均。存在时间不满三个月的,取最后一个月的数值。
最终答案应该是
Acct_ID     Month        D
1                4              30
2                3              20
3                2              20
4                1              10
data test4;
set test3;
D=c;
by Acct_ID Month;
if Acct_ID = lag2(Acct_ID) then

最佳答案

wang1839 查看完整内容

data test4; set test3; by Acct_ID Month; lag1=lag(c);lag2=lag2(c); if last.Acct_ID then do; if month>=3 then do; d=mean(c,lag1,lag2); end; else d=c; output; end; run;
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2016-6-18 17:13:39

data test4;
set test3;
by Acct_ID Month;
lag1=lag(c);lag2=lag2(c);
if last.Acct_ID then do;
if month>=3 then do;
d=mean(c,lag1,lag2);
end;
else d=c;
output;
end;
run;
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2016-6-19 03:36:21
自己定起来~
我自己写的CODE是这样的。
data test4;
set test3;
RR_0_30=c;
by Acct_ID Month;
if Acct_ID = lag2(Acct_ID) then
        RR_0_30= (LAG2(c)+LAG(c)+c)/3;
IF last.Acct_ID;
drop c;
run;
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2016-6-21 01:19:31
proc sort data=test3;
by acct_id month;
run;
data test3;
set test3;
lag_c=lag(c);
lag2_c=lag2(c);
if lag2_c ^=. then RR_0_30=(c+lag_c+lag2_c);
else RR_0_30=c;
if last.acct_id;
run;
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2016-6-21 01:28:00
分析哥 发表于 2016-6-21 01:19
proc sort data=test3;
by acct_id month;
run;
哦 忘了 set 后面还要加个by
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群