<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Weighted average in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Weighted-average/m-p/889297#M351343</link>
    <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I would like to calculate:&lt;/P&gt;
&lt;P&gt;Weighted average of interest on loans (The weight is on loan amount)&lt;/P&gt;
&lt;P&gt;Number of loans&lt;/P&gt;
&lt;P&gt;Number of customers&lt;/P&gt;
&lt;P&gt;Sum of loans&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here are some ways to calculate it with one-way-distribution or two-way-distribution.&lt;/P&gt;
&lt;P&gt;I have some questions please:&lt;/P&gt;
&lt;P&gt;1-Using&amp;nbsp;proc tabulate to calculate One_Way_dist&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How can I add to calculation also -Nr_Loans,Loan_Sum,Nr_Customers&lt;/P&gt;
&lt;P&gt;2-Using&amp;nbsp;proc summary to calculate One_Way_dist&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How can I add to calculation also -Nr_Customers&lt;/P&gt;
&lt;P&gt;3-Using&amp;nbsp;&amp;nbsp;proc Report to calculate One_Way_dist&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why Loan_Sum is wrong calculated??&lt;/P&gt;
&lt;P&gt;How to calculate also Nr_Customers??&lt;/P&gt;
&lt;P&gt;4-Using&amp;nbsp;proc Report to calculate Tw0-Way-dist, how to do it?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
/*********Weighted average**************/
/*********Weighted average**************/
/*********Weighted average**************/
Data have;
Input Customer_ID CAT1 $  CAT2 $  amnt interest;
cards;
1 a T 100 4
2 a T 200 5
3 a F 300 6
4 a F 400 4
5 a T 500 4
6 a T 600 8
7 a F 700 10
8 b F 800 11
9 b F 900 12
10 b T 100 5
11 b T 200 6
12 b T 300 8
13 b T 400 3
14 b T 500 4 
15 b F 600 7
;
Run;


/*********One-Way-Dist-W_Avg,nr_Loans,nr_Customers,Sum_Loans***********/
/*********One-Way-Dist-W_Avg,nr_Loans,nr_Customers,Sum_Loans***********/
/*********One-Way-Dist-W_Avg,nr_Loans,nr_Customers,Sum_Loans***********/
PROC SQL;
select  CAT1,
       count(*) as Nr_Loans format=comma12.,
	   count(distinct Customer_ID) as nr_Customers format=comma12.,
       sum(amnt) as Loan_Sum,
       sum(interest*amnt)/sum(amnt) as w_avg_interest   format=8.4
from  have
group by CAT1
;
QUIT;


/*********One-Way-Dist***********/
/*********One-Way-Dist***********/
/*********One-Way-Dist***********/
/***Question-How to calculate also Nr_Loans,Loan_Sum,Nr_Customers???*****/
proc tabulate data=have format=8.4 MISSING;
class CAT1;
var interest;
weight amnt;
TABLE CAT1='' ALL,interest=''*MEAN='W_Avg_interest'/box='CAT1';
run;


/*********One-Way-Dist-W_Avg,nr_Loans,Loan_Sum***********/
/*********One-Way-Dist-W_Avg,nr_Loans,Loan_Sum***********/
/*********One-Way-Dist-W_Avg,nr_Loans,Loan_Sum***********/
/***Question---How to calculate also Nr_Customers???****/
proc sort data=have;
by CAT1;
Run;

proc summary data=have;
by CAT1;
var interest / weight=amnt;
var amnt;
output out=want(drop=_:)
mean(interest)=w_avg_interest
N(amnt)=nr_obs 
sum(amnt)=loan_sum
;
run;


/*********One-Way-Dist-W_Avg,nr_Loans,Sum_Loans---Wrong results for Sum_Loans!!!***********/
/*********One-Way-Dist-W_Avg,nr_Loans,Sum_Loans---Wrong results for Sum_Loans!!!***********/
/*********One-Way-Dist-W_Avg,nr_Loans,Sum_Loans---Wrong results for Sum_Loans!!!***********/
/***Question---Why Loan_Sum is wrong calcukated????*****/
/***Question---How to calculate also Nr_Customers???****/
proc report data=have;
column CAT1  amnt  Customer_ID  interest;
define CAT1 / group;
define amnt / analysis sum format=comma12. "Loan_Sum";/*Wrong result!!*/
define Customer_ID / analysis N format=comma12. "Nr_Loans";
define interest / analysis mean format=8.4 "w_avg_interest";
weight amnt;
run;


/*********Two-Way-Dist***********/
/*********Two-Way-Dist***********/
/*********Two-Way-Dist***********/
proc tabulate data=have format=8.4 MISSING;
class CAT1  CAT2;
var interest;
weight amnt;
TABLE interest=''*MEAN='' , CAT1='' ALL,CAT2 ALL/box='CAT1';
run;
/**Question----How can I calculate it via proc report???/&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 15 Aug 2023 06:52:44 GMT</pubDate>
    <dc:creator>Ronein</dc:creator>
    <dc:date>2023-08-15T06:52:44Z</dc:date>
    <item>
      <title>Weighted average</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Weighted-average/m-p/889297#M351343</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I would like to calculate:&lt;/P&gt;
&lt;P&gt;Weighted average of interest on loans (The weight is on loan amount)&lt;/P&gt;
&lt;P&gt;Number of loans&lt;/P&gt;
&lt;P&gt;Number of customers&lt;/P&gt;
&lt;P&gt;Sum of loans&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here are some ways to calculate it with one-way-distribution or two-way-distribution.&lt;/P&gt;
&lt;P&gt;I have some questions please:&lt;/P&gt;
&lt;P&gt;1-Using&amp;nbsp;proc tabulate to calculate One_Way_dist&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How can I add to calculation also -Nr_Loans,Loan_Sum,Nr_Customers&lt;/P&gt;
&lt;P&gt;2-Using&amp;nbsp;proc summary to calculate One_Way_dist&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How can I add to calculation also -Nr_Customers&lt;/P&gt;
&lt;P&gt;3-Using&amp;nbsp;&amp;nbsp;proc Report to calculate One_Way_dist&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why Loan_Sum is wrong calculated??&lt;/P&gt;
&lt;P&gt;How to calculate also Nr_Customers??&lt;/P&gt;
&lt;P&gt;4-Using&amp;nbsp;proc Report to calculate Tw0-Way-dist, how to do it?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
/*********Weighted average**************/
/*********Weighted average**************/
/*********Weighted average**************/
Data have;
Input Customer_ID CAT1 $  CAT2 $  amnt interest;
cards;
1 a T 100 4
2 a T 200 5
3 a F 300 6
4 a F 400 4
5 a T 500 4
6 a T 600 8
7 a F 700 10
8 b F 800 11
9 b F 900 12
10 b T 100 5
11 b T 200 6
12 b T 300 8
13 b T 400 3
14 b T 500 4 
15 b F 600 7
;
Run;


/*********One-Way-Dist-W_Avg,nr_Loans,nr_Customers,Sum_Loans***********/
/*********One-Way-Dist-W_Avg,nr_Loans,nr_Customers,Sum_Loans***********/
/*********One-Way-Dist-W_Avg,nr_Loans,nr_Customers,Sum_Loans***********/
PROC SQL;
select  CAT1,
       count(*) as Nr_Loans format=comma12.,
	   count(distinct Customer_ID) as nr_Customers format=comma12.,
       sum(amnt) as Loan_Sum,
       sum(interest*amnt)/sum(amnt) as w_avg_interest   format=8.4
from  have
group by CAT1
;
QUIT;


/*********One-Way-Dist***********/
/*********One-Way-Dist***********/
/*********One-Way-Dist***********/
/***Question-How to calculate also Nr_Loans,Loan_Sum,Nr_Customers???*****/
proc tabulate data=have format=8.4 MISSING;
class CAT1;
var interest;
weight amnt;
TABLE CAT1='' ALL,interest=''*MEAN='W_Avg_interest'/box='CAT1';
run;


/*********One-Way-Dist-W_Avg,nr_Loans,Loan_Sum***********/
/*********One-Way-Dist-W_Avg,nr_Loans,Loan_Sum***********/
/*********One-Way-Dist-W_Avg,nr_Loans,Loan_Sum***********/
/***Question---How to calculate also Nr_Customers???****/
proc sort data=have;
by CAT1;
Run;

proc summary data=have;
by CAT1;
var interest / weight=amnt;
var amnt;
output out=want(drop=_:)
mean(interest)=w_avg_interest
N(amnt)=nr_obs 
sum(amnt)=loan_sum
;
run;


/*********One-Way-Dist-W_Avg,nr_Loans,Sum_Loans---Wrong results for Sum_Loans!!!***********/
/*********One-Way-Dist-W_Avg,nr_Loans,Sum_Loans---Wrong results for Sum_Loans!!!***********/
/*********One-Way-Dist-W_Avg,nr_Loans,Sum_Loans---Wrong results for Sum_Loans!!!***********/
/***Question---Why Loan_Sum is wrong calcukated????*****/
/***Question---How to calculate also Nr_Customers???****/
proc report data=have;
column CAT1  amnt  Customer_ID  interest;
define CAT1 / group;
define amnt / analysis sum format=comma12. "Loan_Sum";/*Wrong result!!*/
define Customer_ID / analysis N format=comma12. "Nr_Loans";
define interest / analysis mean format=8.4 "w_avg_interest";
weight amnt;
run;


/*********Two-Way-Dist***********/
/*********Two-Way-Dist***********/
/*********Two-Way-Dist***********/
proc tabulate data=have format=8.4 MISSING;
class CAT1  CAT2;
var interest;
weight amnt;
TABLE interest=''*MEAN='' , CAT1='' ALL,CAT2 ALL/box='CAT1';
run;
/**Question----How can I calculate it via proc report???/&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Aug 2023 06:52:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Weighted-average/m-p/889297#M351343</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2023-08-15T06:52:44Z</dc:date>
    </item>
    <item>
      <title>Re: Weighted average</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Weighted-average/m-p/889358#M351373</link>
      <description>&lt;P&gt;When you say something is "wrong" as in&lt;/P&gt;
&lt;PRE&gt;/*********One-Way-Dist-W_Avg,nr_Loans,Sum_Loans---Wrong results for Sum_Loans!!!***********/
/*********One-Way-Dist-W_Avg,nr_Loans,Sum_Loans---Wrong results for Sum_Loans!!!***********/
/*********One-Way-Dist-W_Avg,nr_Loans,Sum_Loans---Wrong results for Sum_Loans!!!***********/
/***Question---Why Loan_Sum is wrong calcukated????*****/&lt;/PRE&gt;
&lt;P&gt;You need to provide what YOU think is the correct answer and how you derive it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you ask:&lt;/P&gt;
&lt;PRE&gt;/***Question-How to calculate also Nr_Loans,Loan_Sum,Nr_Customers???*****/
proc tabulate data=have format=8.4 MISSING;
class CAT1;
var interest;
weight amnt;
TABLE CAT1='' ALL,interest=''*MEAN='W_Avg_interest'/box='CAT1';
run;&lt;/PRE&gt;
&lt;P&gt;You need to provide SOMETHING as a Class variable to count for proc tabulate to get an N. Like Customer_id. How does Nr_customers vary from Nr_loans? From the example data I would think it the same. But are there supposed to counts within some group? Need to specify.&lt;/P&gt;
&lt;P&gt;You need a VAR variable to SUM. Same comment related to grouping as well. If you do not want a weighted sum then you need to use a separate call to proc Tabulate (or means or summary or report) that does NOT use the Weight statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So for a "pretty" table you may need to calculate some things separately, combine them and display the combined result.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Aug 2023 14:41:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Weighted-average/m-p/889358#M351373</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-08-15T14:41:49Z</dc:date>
    </item>
  </channel>
</rss>

