Proc表示 - 计算份额/重量

时间:2018-03-29 12:39:02

标签: sas sas-studio

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

欣赏帮助。

2 个答案:

答案 0 :(得分:3)

Proc FREQ将计算百分比。您可以将输出的PERCENT列除以得到分数,或者使用下游的百分数。

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

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

答案 1 :(得分:1)

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