DATA Step, Macro, Functions and more

Proc means - Calculating the share / weight

Accepted Solution Solved
Reply
Contributor
Posts: 65
Accepted Solution

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.


Accepted Solutions
Solution
‎03-29-2018 08:58 AM
Super User
Posts: 9,572

Re: Proc means - Calculating the share / weight

Do it in SQL:

proc sql;
create table want as
select a.*, payment2017 / sum(payment2017) as share
from test a
group by business_line
order by id
;
quit;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
Solution
‎03-29-2018 08:58 AM
Super User
Posts: 9,572

Re: Proc means - Calculating the share / weight

Do it in SQL:

proc sql;
create table want as
select a.*, payment2017 / sum(payment2017) as share
from test a
group by business_line
order by id
;
quit;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 65

Re: Proc means - Calculating the share / weight

Posted in reply to KurtBremser

I always forget about SQL

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 67 views
  • 1 like
  • 2 in conversation