由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Statistics版 - 请问如果用SAS 解决这个问题
相关主题
一个SAS问题(transpose?)help!! help!! SAS help!! Urgent!!
请教proc sql处理missing value的问题SAS BASE 官方sample 第三题 求解析 谢谢!
包子问题请教( sas)怎样用R定位变量的位置
请问SAS中如何通过组内一个变量的值控制整组变量新手请教大家一个sas 问题,多谢指教
How to change sas dataset column order很挑战的data transformation problem help
弱问一个SAS里面求adjusted means的问题请教一个SAS问题
在SAS里如何用array或者macro改进这段程序?求指教!怎样储存我想要的proc means 的结果?
help about initial value in retain statement请教关于SAS表格输出的问题
相关话题的讨论汇总
话题: proc话题: var4话题: max话题: run话题: var1
进入Statistics版参与讨论
1 (共1页)
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;
1 (共1页)
进入Statistics版参与讨论
相关主题
请教关于SAS表格输出的问题How to change sas dataset column order
Please help with a SAS macro弱问一个SAS里面求adjusted means的问题
Proc Surveyselect中, 当 reps >=2 时, 样本重复在SAS里如何用array或者macro改进这段程序?求指教!
请教proc transpose 问题help about initial value in retain statement
一个SAS问题(transpose?)help!! help!! SAS help!! Urgent!!
请教proc sql处理missing value的问题SAS BASE 官方sample 第三题 求解析 谢谢!
包子问题请教( sas)怎样用R定位变量的位置
请问SAS中如何通过组内一个变量的值控制整组变量新手请教大家一个sas 问题,多谢指教
相关话题的讨论汇总
话题: proc话题: var4话题: max话题: run话题: var1