作者imaltar (..)
标题Re: [程式] SAS 累加
时间Sun Apr 20 15:57:20 2014
※ 引述《tea5 (茶)》之铭言:
: ------------------------------------------------------------------------
: TITLE请着名软体类别
: 按ctrl+v可预览 发文前请把灰色的注解删除
: 可使用ctrl+y删除一整行
: [软体程式类别]:
: SAS
: [程式问题]:
: 资料处理
: [软体熟悉度]:
: 新手(不到1个月)
: [问题叙述]:
: 目前有三个变数
: ID YEAR COST
: 1 2005 100
: 1 2006 100
: 1 2006 200
: 2 2004 100
: 2 2005 100
: 2 2005 100
: 2 2006 300
: 想得到
: ID YEAR COST TOTAL
: 1 2005 100 100
: 1 2006 200 300
: 2 2004 100 100
: 2 2005 100 200
: 2 2006 300 300
: 的结果
: [程式范例]:
: data new;
: set old;
: if first.cost then total=0;
: total=cost+total;
: run;
: 使用first.这个function 好像有点问题
: 麻烦请高手修正,谢谢。
data a;
input ID YEAR COST;
cards;
1 2005 100
1 2006 100
1 2006 200
2 2004 100
2 2005 100
2 2005 100
2 2006 300
;
run;
proc sort data=a;by id year;
data b;set a;
by id year;
retain total;
if first.year then do;total=0;end;
total=cost+total;
if last.year;
run;
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 111.241.43.240
※ 文章网址: http://webptt.com/cn.aspx?n=bbs/Statistics/M.1397980642.A.F99.html
1F:推 tea5:感谢相助! 04/20 20:01