x*******u 发帖数: 500 | 1 my data:
var1 var2 var3 var4
2 4 6 7
4 9 7 6
5 2 1 1
如何得到一个新的variable, 它的值是var1-var4中有最大值的那个variable的名字。
结果应该是
newvar
var4
var2
var1
谢谢 |
k*******a 发帖数: 772 | 2 你可以用array,也可以用proc transpose
如果用transpose你可以:
data b;
set a;
ID=_N_;
run;
proc transpose data=b out=c;
var var1-var4;
by ID;
run;
proc sort data=c;
by ID descending col1;
run;
data d(keep=newvar);
set c;
by ID descending col1;
if first.ID;
newvar=_name_;
run; |
d******9 发帖数: 404 | 3 Pretty good, very useful. Thanks. |
d******9 发帖数: 404 | 4 I used array to do it, same results:
proc format;
value position
1='A'
2='B'
3='C'
4='D';
run;
data E(drop=I) ;
set A;
array X(4) A B C D;
do I=1 to 4;
if X(I)= max(A, B, C, D) then Position=I;
end;
Max_Var=put(position, position.);
run;
However, what if the values have ties? say:
var1 var2 var3 var4
9 9 3 4
9 2 9 8
9 9 9 9
???????? |
l****u 发帖数: 529 | 5 with and w/o ties:
%macro new;
data new(drop=i max);
set yourdata;
max=max( of var1-var4);
length new $8.;
%do i=1 %to 4;
if var&i=max then do;
new="var&i";
output;
end;
%end;
run;
%mend;
%new |
d******9 发帖数: 404 | 6 Good. Macro is also a solution to it.
Now we have 3 methods: Proc Transpose, Array, Macro.
Any other methods?
【在 l****u 的大作中提到】 : with and w/o ties: : %macro new; : data new(drop=i max); : set yourdata; : max=max( of var1-var4); : length new $8.; : %do i=1 %to 4; : if var&i=max then do; : new="var&i"; : output;
|
l*********s 发帖数: 5409 | 7 having baozi bu?
data result;
length name $8;
set A;
array aa{*} var1-var4;
do i=1 to dim(aa);
if aa{i}=max(of var1-var4) then name=vname(comp{i});
end;
drop i;
run;
【在 d******9 的大作中提到】 : Good. Macro is also a solution to it. : Now we have 3 methods: Proc Transpose, Array, Macro. : Any other methods?
|
d******9 发帖数: 404 | 8 Good. It is nice to use the VNAME function.
Why do you use Comp(I)????????
【在 l*********s 的大作中提到】 : having baozi bu? : data result; : length name $8; : set A; : array aa{*} var1-var4; : do i=1 to dim(aa); : if aa{i}=max(of var1-var4) then name=vname(comp{i}); : end; : drop i; : run;
|
l*********s 发帖数: 5409 | 9 sorry, it shall be aa, I changed the array name while posting but forgot to modify the
loop accordingly le.
【在 d******9 的大作中提到】 : Good. It is nice to use the VNAME function. : Why do you use Comp(I)????????
|
o****o 发帖数: 8077 | 10 data _xxx;
input var1 var2 var3 var4;
cards;
2 4 6 7
4 9 7 6
5 2 1 1
7 3 7 3
;
run;
proc transpose data=_xxx out=_xxx2;
run;
proc means data=_xxx2 noprint;
var col1-col4;
output out=_xxx3(keep=v1-v4)
maxid(col1(_name_)
col2(_name_)
col3(_name_)
col4(_NAME_))= v1-v4/autoname;
run;
proc transpose data=_xxx3 out=_xxx3t;
var v1-v4;
run;
data _xxx;
merge _xxx _xxx3t(keep=COL1);
rename col1=varname;
run; |
d******9 发帖数: 404 | 11 oloolo, your method is cool and unusual. |
d******9 发帖数: 404 | 12 I just work out another method to do it by using Proc Transpose + Proc SQL +
Macro. It works fine although more complicated.
data A;
input A B C D;
cards;
2 4 6 7
4 9 7 6
5 2 1 1
2 9 3 5
3 5 6 0
1 8 16 4
;
run;
proc transpose data=A out=B;
var _numeric_;
run;
%macro max;
%local I;
proc sql;
select count(*) into: N
from A;
quit;
%let N=&N;
%do I=1 %to &N;
proc sql;
create table max_col&I as
select "&I" as Obs, max(col&I) as max, _Name_ as Var
from B
having max(col&I)=col&I;
/*having calculated max=col&I;*/
quit;
proc append base=all data=max_col&I force;
run;
%end;
%mend;
%max; |