liangxuebing 发表于 2014-3-30 19:06 
搞了两天,我自己解决了,和大家分享一下。先升序排,找到最大值,然后和原表合并,再对原表的价格求降序, ...
The same logic can be built into a data step. See below.
DATA tmp;
input ID startmonth price;
cards;
1 198011 12
1 198012 13
1 198101 20
1 198102 25
1 198103 10
2 201101 20
2 201102 30
2 201103 5
2 201104 8
;
/**assume data is sorted**/
data tmp2;
do i=1 by -1 until(last.id);
set tmp;
by id ;
if first.id then max=.;
if price>=max then max=price;
end;
do i=1 by -1 until(max=price);
set tmp;
by id ;
if first.id then min=1e308;
if price<=min then min=price;
end;
do i=1 by -1 until(last.id);
set tmp;
by id ;
diff=max-min;
output;
end;
drop i;
run;
proc print;run;