代码之家  ›  专栏  ›  技术社区  ›  78282219

过程平均数-计算份额/权重

  •  1
  • 78282219  · 技术社区  · 8 年前

    我正在使用proc方法计算业务线支付的份额,数据如下所示:

    data Test;
    input ID Business_Line Payment2017;
    Datalines;
    1 1 1000
    2 1 2000
    3 1 3000
    4 1 4000
    5 2 500
    6 2 1500
    7 2 3000
    ;
    run;
    

    我想计算另一列,按组(business\u line)计算付款的百分比份额(权重),如下所示:

       data Test;
    input ID Business_Line Payment2017 share;
    Datalines;
    1 1 1000 0.1
    2 1 2000 0.2
    3 1 3000 0.3
    4 1 4000 0.4
    5 2 500 0.1
    6 2 1500 0.3
    7 2 3000 0.6
    ;
    run;
    

    我迄今为止使用的代码:

    proc means data = test noprint;
    class ID;
    by business_line;
    var Payment2017;
    output out=test2 
    sum = share;
    weight = payment2017/share;
    run;
    

    我也试过了

    proc means data = test noprint;
    class ID;
    by business_line;
    var Payment2017 /weight = payment2017;
    output out=test3 ;
    run;
    

    感谢您的帮助。

    2 回复  |  直到 8 年前
        1
  •  3
  •   Richard    8 年前

    Proc FREQ 将计算百分比。您可以划分 PERCENT 列以获取分数,或使用下游百分比。

    在此示例中 id 十字架 payment2017 以确保所有原始行都是输出的一部分。如果 id号 不存在,并且存在任何重复付款金额, FREQ 将合计付款金额。

    proc freq data=have noprint;
     by business_line;
     table id*payment2017 / out=want all;
     weight payment2017 ;
    run;
    
        2
  •  1
  •   Shenglin Chen    8 年前

    使用proc sql很方便:

    proc sql;
       select *, payment2017/sum(payment2017) as share from test group by business_line;
    quit;
    

    数据步骤:

    data have;
        do until (last.business_line);
        set test;
        by business_line notsorted;
        total+payment2017;
        end;
        do until (last.business_line);
        set test;
        by business_line notsorted;
        share=payment2017/total;
        output;
        end;
        call missing(total);
        drop total;
    run;
    
    推荐文章