<?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 Re: Weighted average by group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Weighted-average-by-group/m-p/698085#M213456</link>
    <description>&lt;P&gt;Please have a look at the documentation of the weight-statement in proc summary.&lt;/P&gt;</description>
    <pubDate>Wed, 11 Nov 2020 06:50:30 GMT</pubDate>
    <dc:creator>andreas_lds</dc:creator>
    <dc:date>2020-11-11T06:50:30Z</dc:date>
    <item>
      <title>Weighted average by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Weighted-average-by-group/m-p/698080#M213453</link>
      <description>&lt;P&gt;hello&lt;/P&gt;
&lt;P&gt;I have loans information.&lt;/P&gt;
&lt;P&gt;Each row contain information about loan that a customer received.&lt;/P&gt;
&lt;P&gt;I want to calculate for each team the following 3 statistics:&lt;/P&gt;
&lt;P&gt;1-Number of loans&lt;/P&gt;
&lt;P&gt;2-Sum of loans&lt;/P&gt;
&lt;P&gt;3-Weighted average of interest rate&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have tried to use proc summary but result of sum of loans is not correct.&lt;/P&gt;
&lt;P&gt;What is the way to get correct results via proc summary?&lt;/P&gt;
&lt;P&gt;What is the way to perform it via proc tabulate?&lt;/P&gt;
&lt;P&gt;What is the way to perform it via proc report?&lt;/P&gt;
&lt;P&gt;I want also to add a summary row that contain information for all teams together.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Customer_ID Team $ Loan_Sum  interest_rate;
cards;
111 A 100 2
333 A 200 3
888 A 300 4
222 A 400 5
111 B 300 6
777 B 400 2
333 B 100 3
;
run;


data have2;
set have;
Loan_Sum2=Loan_Sum;
Run;

proc summary data=have2;
by Team;
var interest_rate Loan_Sum2;
weight Loan_Sum;
output out=want(drop=_:)
mean(interest_rate)=weighted_avg_interest_rate
N(Loan_Sum2)=Nr_Obs 
sum(Loan_Sum)=Loan_Sum ;
Run;

&lt;/CODE&gt;&lt;/PRE&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;</description>
      <pubDate>Wed, 11 Nov 2020 06:22:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Weighted-average-by-group/m-p/698080#M213453</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2020-11-11T06:22:25Z</dc:date>
    </item>
    <item>
      <title>Re: Weighted average by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Weighted-average-by-group/m-p/698085#M213456</link>
      <description>&lt;P&gt;Please have a look at the documentation of the weight-statement in proc summary.&lt;/P&gt;</description>
      <pubDate>Wed, 11 Nov 2020 06:50:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Weighted-average-by-group/m-p/698085#M213456</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-11-11T06:50:30Z</dc:date>
    </item>
    <item>
      <title>Re: Weighted average by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Weighted-average-by-group/m-p/698089#M213458</link>
      <description>&lt;P&gt;The weight average is working well but the sum not&lt;/P&gt;</description>
      <pubDate>Wed, 11 Nov 2020 07:09:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Weighted-average-by-group/m-p/698089#M213458</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2020-11-11T07:09:57Z</dc:date>
    </item>
    <item>
      <title>Re: Weighted average by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Weighted-average-by-group/m-p/698093#M213460</link>
      <description>&lt;P&gt;Both proc summary and proc report are not working well&amp;nbsp; for sum calculation.&lt;/P&gt;
&lt;P&gt;May anyone help please?&lt;/P&gt;
&lt;P&gt;This is the target of this forum I think, Thanks Dave&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Customer_ID Team $ Loan_Sum  interest_rate;
cards;
111 A 100 2
333 A 200 3
888 A 300 4
222 A 400 5
111 B 300 6
777 B 400 2
333 B 100 3
;
run;
/*Way1-proc sql*/
PROC SQL;
	select  Team,
            count(*) as Nr_Loans,
            sum(Loan_Sum) as Loan_Sum,
            sum(interest_rate*Loan_Sum)/sum(Loan_Sum) as Weighted_Avg_interest_rate 
	from  have
	group by Team
;
QUIT;
/*Way2-proc report*/
proc report data=have;
column Team  Loan_Sum=x  Loan_Sum  Customer_ID  interest_rate;
  define Team / group;
  define x / analysis sum format=comma12. "Loan_Sum";/*Wrong result!!*/
  define Customer_ID / analysis N format=comma12. "Nr_Loans";
  define interest_rate / analysis mean format=comma12.3 "Weighted_Avg_interest_rate";
  weight Loan_Sum;
run;
/*Way3-proc summary*/
data have2;
set have;
sum_halv2=sum_halv;
Run;
proc summary data=have2;
by Team;
var interest_rate;
weight Loan_Sum;
output out=want(drop=_:)
N(Customer_ID)=Nr_Loans
sum(Loan_Sum)=Loan_Sum /*Wrong result!!*/
mean(interest_rate)=Weighted_Avg_interest_rate
;
Run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 11 Nov 2020 07:50:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Weighted-average-by-group/m-p/698093#M213460</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2020-11-11T07:50:50Z</dc:date>
    </item>
    <item>
      <title>Re: Weighted average by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Weighted-average-by-group/m-p/698094#M213461</link>
      <description>&lt;P&gt;Once again, I have to make a clear statement for Maxim 1:&lt;/P&gt;
&lt;P&gt;READ THE DOCUMENTATION.&lt;/P&gt;
&lt;P&gt;I mean it.&lt;/P&gt;
&lt;P&gt;Really.&lt;/P&gt;
&lt;P&gt;Yes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You want to use PROC SUMMARY/MEANS, with which you are not very familiar, so your first step is to study the documentation.&lt;/P&gt;
&lt;P&gt;The &lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=proc&amp;amp;docsetTarget=p12n9rrav4byvzn1b9wj26dcoq9l.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;VAR&lt;/A&gt; statement in these procedures has these important messages for you:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;H2 id="n1os7wan372100n1fv06efxnuher" class="xisDoc-title"&gt;Syntax&lt;/H2&gt;
&lt;DIV id="p129x3tgwqv438n1o0zmrj39owgg" class="xisDoc-syntaxSimple"&gt;
&lt;DIV class="xisDoc-syntaxLevel"&gt;&lt;SPAN class="xisDoc-keyword"&gt;VAR&lt;/SPAN&gt; &lt;EM class="xisDoc-userSuppliedValue"&gt;&lt;A tabindex="0" title="“variable(s)”" href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=proc&amp;amp;docsetTarget=p12n9rrav4byvzn1b9wj26dcoq9l.htm&amp;amp;locale=de#p0w33kk0j31pp4n1p55csnh8j7np" data-docset-id="proc" data-docset-version="9.4" data-original-href="p12n9rrav4byvzn1b9wj26dcoq9l.htm#p0w33kk0j31pp4n1p55csnh8j7np"&gt;variable(s)&lt;/A&gt;&lt;/EM&gt; &lt;SPAN class="xisDoc-optional"&gt;&amp;lt;&lt;A tabindex="0" title="“WEIGHT=weight-variable ”" href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=proc&amp;amp;docsetTarget=p12n9rrav4byvzn1b9wj26dcoq9l.htm&amp;amp;locale=de#n1lq4pmrmowru0n1utyzry7ez1fz" data-docset-id="proc" data-docset-version="9.4" data-original-href="p12n9rrav4byvzn1b9wj26dcoq9l.htm#n1lq4pmrmowru0n1utyzry7ez1fz"&gt;/ WEIGHT=&lt;EM class="xisDoc-userSuppliedValue"&gt;weight-variable&lt;/EM&gt;&lt;/A&gt;&amp;gt;&lt;/SPAN&gt;;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/BLOCKQUOTE&gt;
&lt;DIV id="p129x3tgwqv438n1o0zmrj39owgg" class="xisDoc-syntaxSimple"&gt;
&lt;DIV class="xisDoc-syntaxLevel"&gt;So you can define a weight variable without a WEIGHT statement, and make it specific for a variable.&lt;/DIV&gt;
&lt;DIV class="xisDoc-syntaxLevel"&gt;And this:&lt;/DIV&gt;
&lt;BLOCKQUOTE&gt;
&lt;DIV class="xisDoc-syntaxLevel"&gt;
&lt;TABLE class="xisDoc-summary"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TH class="xisDoc-summaryTip"&gt;Tip:&lt;/TH&gt;
&lt;TD class="xisDoc-summaryText"&gt;You can use multiple VAR statements.&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/BLOCKQUOTE&gt;
&lt;DIV class="xisDoc-syntaxLevel"&gt;
&lt;P&gt;So you can have one or more variables weighted, and one or more variables not weighted.&lt;/P&gt;
&lt;P&gt;Eureka! We can now write the code like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=have;
by team;
var interest_rate / weight=loan_sum;
var loan_sum;
output
  out=want(drop=_:)
  mean(interest_rate)=weighted_avg_interest_rate
  N(loan_sum)=nr_obs 
  sum(loan_sum)=loan_sum
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and that's it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please take note that I am no expert for these procedures at all; all I did was to study the documentation and apply the knowledge gained from that.&amp;nbsp;&lt;U&gt;&lt;EM&gt;You can do that also. Everyone can.&lt;/EM&gt;&lt;/U&gt;&lt;/P&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;</description>
      <pubDate>Wed, 11 Nov 2020 08:14:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Weighted-average-by-group/m-p/698094#M213461</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-11-11T08:14:55Z</dc:date>
    </item>
    <item>
      <title>Re: Weighted average by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Weighted-average-by-group/m-p/698095#M213462</link>
      <description>&lt;P&gt;Just to be a friendly-minded voice in the crowd: Read the documentation &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Nov 2020 08:17:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Weighted-average-by-group/m-p/698095#M213462</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-11-11T08:17:43Z</dc:date>
    </item>
  </channel>
</rss>

