Proc means - 计算份额/权重

Proc means - Calculating the share / weight

我正在使用 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_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;

感谢您的帮助。

用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;

Proc FREQ 将计算百分比。您可以除以输出的 PERCENT 列以获得分数,或在下游处理百分比。

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

proc freq data=have noprint;
 by business_line;
 table id*payment2017 / out=want all;
 weight payment2017 ;
run;