data tem;
set tem;
_tem=max(of v1-v3);
array arr v1-v3;
do over arr;
if arr=_tem then V4=_tem||'_'||vname(arr);
end;
drop _tem;
run;
proc print;run;作者: shiyiming 时间: 2003-10-22 18:18 标题: 思路真巧妙啊! vname用的真好,佩服,佩服啊!作者: shiyiming 时间: 2003-10-23 09:27
高人!谢谢!作者: shiyiming 时间: 2004-3-6 01:08
[quote="willon":1d709]data tem;
input V1 V2 V3;
cards;
1 2 3
1 4 3
5 2 4
;run;
data tem;
set tem;
_tem=max(of v1-v3);
array arr v1-v3;
do over arr;
if arr=_tem then V4=_tem||'_'||vname(arr);
end;
drop _tem;
run;
proc print;run;[/quote:1d709]
逻辑很干净利落。
小小建议:其实_tem=max(of v1-v3) 和后面的逻辑导致了重复扫描数组,如果只是求最值,不妨直接求,只需扫描一遍数组:
data tem(keep=v1-v4);
set tem;
array arr v1-v3;
winner = 1;
win_val = arr(1);
do i=2 to dim(arr);
if win_val<arr(i) then do;
winner = i;
win_val= arr(i);
end;
end;
v4 = win_val||'_'||vname(arr(winner));
run;