<?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 Excluding current observation in calculating a weighted average by a group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Excluding-current-observation-in-calculating-a-weighted-average/m-p/579244#M164404</link>
    <description>&lt;P&gt;Hi all -&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance for any guidance you may provide. I have searched through the forums to find specifically what I was looking for and could not, so please point me to the page if you can.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Where as WAVG by product is simply sum(risk*quantity)/sum(quantity)&lt;/P&gt;&lt;P&gt;And WAVG by Product (excluding current ID) follows the same logic sans ID 1 for example.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you could provide me some guidance as to how to implement the WAVG by Product (excluding the current ID), this would be very helpful.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Jeremy&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;Product&lt;/TD&gt;&lt;TD&gt;Risk&lt;/TD&gt;&lt;TD&gt;Quantity&lt;/TD&gt;&lt;TD&gt;WAVG by Product&lt;/TD&gt;&lt;TD&gt;WAVG by Product (excluding Current ID)&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;10%&lt;/TD&gt;&lt;TD&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&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
    <pubDate>Mon, 05 Aug 2019 22:33:16 GMT</pubDate>
    <dc:creator>JMiraglia</dc:creator>
    <dc:date>2019-08-05T22:33:16Z</dc:date>
    <item>
      <title>Excluding current observation in calculating a weighted average by a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Excluding-current-observation-in-calculating-a-weighted-average/m-p/579244#M164404</link>
      <description>&lt;P&gt;Hi all -&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance for any guidance you may provide. I have searched through the forums to find specifically what I was looking for and could not, so please point me to the page if you can.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Where as WAVG by product is simply sum(risk*quantity)/sum(quantity)&lt;/P&gt;&lt;P&gt;And WAVG by Product (excluding current ID) follows the same logic sans ID 1 for example.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you could provide me some guidance as to how to implement the WAVG by Product (excluding the current ID), this would be very helpful.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Jeremy&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;Product&lt;/TD&gt;&lt;TD&gt;Risk&lt;/TD&gt;&lt;TD&gt;Quantity&lt;/TD&gt;&lt;TD&gt;WAVG by Product&lt;/TD&gt;&lt;TD&gt;WAVG by Product (excluding Current ID)&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;10%&lt;/TD&gt;&lt;TD&gt;200&lt;/TD&gt;&lt;TD&gt;0.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TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Mon, 05 Aug 2019 22:33:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Excluding-current-observation-in-calculating-a-weighted-average/m-p/579244#M164404</guid>
      <dc:creator>JMiraglia</dc:creator>
      <dc:date>2019-08-05T22:33:16Z</dc:date>
    </item>
    <item>
      <title>Re: Excluding current observation in calculating a weighted average by a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Excluding-current-observation-in-calculating-a-weighted-average/m-p/579247#M164406</link>
      <description>&lt;P&gt;One approach would be to accumulate the total numerator and total denominator for each product.&amp;nbsp; Then compute by subtracting the values in the current observation from the numerator and denominator.&amp;nbsp; For example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
total_numerator = 0;
total_denominator = 0;
do until (last.product) ;
   set have;
   by product;
   total_numerator + risk * quantity;
   total_denominator + quantity;
end;
do until (last.product) ;
   set have;
   by product;
   wavg = (total_numerator - risk * quantity) / (total_denominator - quantity);
   output;
end;
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The top loop reads all observations for a PRODUCT.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The bottom loop reads exactly the same observations as the top loop, calculates, and outputs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that multiplication gets performed before subtraction, so the calculations execute properly.&amp;nbsp; It wouldn't hurt to add parentheses to clarify that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Of course your percentages have to be actual numbers.&amp;nbsp; A value such as 0.3 would be fine, but a character string such as "30%" will not work.&lt;/P&gt;</description>
      <pubDate>Mon, 05 Aug 2019 23:03:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Excluding-current-observation-in-calculating-a-weighted-average/m-p/579247#M164406</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-08-05T23:03:14Z</dc:date>
    </item>
    <item>
      <title>Re: Excluding current observation in calculating a weighted average by a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Excluding-current-observation-in-calculating-a-weighted-average/m-p/579248#M164407</link>
      <description>&lt;P&gt;SQL&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input ID $	Product $	Risk percent.	Quantity;*	WAVG by Product	WAVG by Product (excluding Current ID) ;
format risk percent5.;
cards;
1	A	10%	200	0.222222222	0.257142857
2	A	20%	300	0.222222222	0.233333333
3	A	30%	400	0.222222222	0.16
4	B	30%	500	0.526923077	0.580952381
5	B	40%	600	0.526923077	Did not calc
6	B	60%	700	0.526923077	Did not calc
7	B	70%	800	0.526923077	0.45
;

proc sql;
create table want as
select a.* ,sum(b.risk*b.quantity)/sum(b.quantity) as wavg2
from 
(select *, sum(risk*quantity)/sum(quantity) as wavg1 from have a group by product) a left join have b
on a.product=b.product and a.id ne b.id
group by a.product,a.id,a.risk,a.quantity;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 05 Aug 2019 23:11:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Excluding-current-observation-in-calculating-a-weighted-average/m-p/579248#M164407</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-08-05T23:11:34Z</dc:date>
    </item>
    <item>
      <title>Re: Excluding current observation in calculating a weighted average by a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Excluding-current-observation-in-calculating-a-weighted-average/m-p/579265#M164415</link>
      <description>&lt;P&gt;Use SAS/SQL auto-remerging to your advantage:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select id, product,
    sum(risk*quantity)/sum(quantity) as wavg format=percent8.2,
    (sum(risk*quantity)-risk*quantity) / 
    (sum(quantity)-quantity) as wavgProd format=percent8.2
from have
group by product
order by product, id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;                       ID        Product       wavg  wavgProd
                       --------------------------------------
                       1         A          22.22%    25.71%
                       2         A          22.22%    23.33%
                       3         A          22.22%    16.00%
                       4         B          52.69%    58.10%
                       5         B          52.69%    56.50%
                       6         B          52.69%    50.00%
                       7         B          52.69%    45.00%

&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Aug 2019 03:40:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Excluding-current-observation-in-calculating-a-weighted-average/m-p/579265#M164415</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-08-06T03:40:31Z</dc:date>
    </item>
    <item>
      <title>Re: Excluding current observation in calculating a weighted average by a group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Excluding-current-observation-in-calculating-a-weighted-average/m-p/579410#M164471</link>
      <description>Thank you so much! This worked like a charm.&lt;BR /&gt;&lt;BR /&gt;Thanks for all the suggestions as well -- really helped me look at my code critically.&lt;BR /&gt;&lt;BR /&gt;JM</description>
      <pubDate>Tue, 06 Aug 2019 15:02:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Excluding-current-observation-in-calculating-a-weighted-average/m-p/579410#M164471</guid>
      <dc:creator>JMiraglia</dc:creator>
      <dc:date>2019-08-06T15:02:05Z</dc:date>
    </item>
  </channel>
</rss>

