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 -->
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;
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;
我也试试