SAS 跨行复制最大值

标签 sas

我有一个看起来像这样的数据集:

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/

相关文章:

date - SAS 按日期和时间合并

sas - 在 SAS 中使用多个变量进行转置

sas - sas中何时使用%do %end vs do end循环

sas - 在 PROC SQL (SAS) 中声明变量

sas - 如何获取 SAS 中元数据对象的详细信息

sql - 获取组中的第 n 行 - SAS/SQL

java - ANTLR "no viable alternative at input"解析 SAS 代码时出错 If Then Else

sas - 使用 SAS 检测文件夹中的最新修改日期

regex - 使用正则表达式将字符串拆分为多个变量 SAS

r - 分组观察数