id x y
001 5 12
001 4 7
001 3 3
002 2 8
002 1 6
002 2 5
002 3 3
就是对每个id,第一次Y要求计算出x的所有和,id的第二行,计算从第二行到此id最后一个记录x 的和,依次类推,谢谢作者: shiyiming 时间: 2008-11-2 22:38 标题: Re: sas求和问题 is it an exercise?
<!-- m --><a class="postlink" href="http://sasor.feoh.net/viewtopic.php?f=1&t=4081">http://sasor.feoh.net/viewtopic.php?f=1&t=4081</a><!-- m -->作者: shiyiming 时间: 2008-11-2 22:39 标题: Re: sas求和问题 data te;
set tmp;
retain s 0 LstID ;
if _n_ = 1 then LstID = id;
if LstID = id then s = x + s;
else s = x;
LstID = id;
run;作者: shiyiming 时间: 2008-11-5 16:05 标题: Re: sas求和问题 楼上的方法计算结果好像倒过来了,我也很想知道如按一楼的要求如何计算,请高人指点。作者: shiyiming 时间: 2008-11-5 16:38 标题: Re: sas求和问题 data s;
input id $3. x;
datalines;
001 5
001 4
001 3
002 2
002 1
002 2
002 3
;
data s;
set s;
if id^=lag(id) then n=1;
else n+1;
run;
proc sort data=s;by id descending n;
run;
data s;
set s;
if id^=lag(id) then y=x;
else y+x;
run;
proc sort data=s;by id n;
run;
data s;
set s;
drop n;
run;作者: shiyiming 时间: 2008-11-5 16:55 标题: Re: sas求和问题 谢谢作者: shiyiming 时间: 2008-11-8 23:37 标题: Re: sas求和问题 data one;
input id x;
n+1;
datalines;
001 5
001 4
001 3
002 2
002 1
002 2
002 3
;
proc sort data=one out=two;
by id descending n;
data three;
set two;
by id;
if first.id then y=0;
y+x;
proc sort data=three;
by n;
run;
我也试试