Search code examples
sassas-studio

Proc means - Calculating the share / weight


I am using a proc means to calculate the share of the payments made by business line, the data looks like this:

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;

i'm looking to calculate an additional column which, by group (business_line) calculates the percentage share (weight) of the payment as such:

   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;

the code I have used so far:

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

I have also tried

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

appreciate the help.


Solution

  • Proc FREQ will compute percentages. You can divide the PERCENT column of the output to get the fraction, or work with percents downstream.

    In this example id crosses payment2017 in order to ensure all original rows are part of the output. If the id was not present, and there were any replicate payment amounts, FREQ would aggregate the payment amounts.

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