我有一个看起来像这样的数据集:
company Assets Liabilities strategy1 strategy2 strategy3.....strategy22
1 500 500 0 50 50 50
2 200 300 33 30 33 0
我的目标是找到所有策略(策略 1 - 策略 22)的行中的最大值,并基本上按照他们使用的策略对公司进行分类。当一些公司在多种策略下具有相同的最大值时,问题就来了。在这种情况下,我想将公司放入多个桶中。最终的数据集将是这样的:
company Assets Liab. strategy1 strategy2 strategy3.....strategy22 Strategy
1 500 500 0 50 50 50 Strategy2
1 500 500 0 50 50 50 Strategy3
1 500 500 0 50 50 50 Strategy22
等等。 最终目标是能够通过策略对公司的 Assets 、负债等运行一个proc means。到目前为止,我已经能够获得接近我想要的数据集,但在“策略”列中我无法获得它,因此 SAS 并不总是输出具有最大值的第一个策略。
Data want;
set have;
MAX = max(of strategy1-strategy22);
array nums {22} strategy1-strategy22;
do _n_=1 to 21;
count=1;
do _i_ = _n_+1 to 22;
if nums{_n_} = nums{_i_} AND nums{_i_} ne 0 then count + 1;
end;
if count > maxcount then do;
mode = nums{_n_};
maxcount = count;
end;
end;
Run;
Data want2;
set want (where=( maxcount > 1 AND Mode = Max));
by company;
strat=1;
do until (strat gt maxcount);
output;
strat = strat +1;
end;
Run;
基本上,我计算了模式和相同最大值的计数,如果 maxcount > 1 且模式 = max,则我输出相同的观察结果。但是,如果存在多个相同的最大值,我对如何让 SAS 输出不同的策略感到困惑。
最佳答案
这似乎比您需要的要复杂。
data want;
set have;
array strategies[22] strategy1-strategy22;
do strategy = 1 to dim(strategies);
if strategies[strategy] = max(of strategies[*]) then output;
end;
run;
关于SAS 跨行复制最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32378798/