<?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: Compute weighted average using SQL: missing values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Compute-weighted-average-using-SQL-missing-values/m-p/746030#M233971</link>
    <description>&lt;P&gt;I want to compute wave and ave using the maximum number of nonmissing observations. My current code is&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table mytable as
select sum(X * Y) / sum(Y * (X ne .) ) as wave, mean(X) as ave
from my_dataset
group by Z;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This way wave and ave should be both valid and comparable?&lt;/P&gt;</description>
    <pubDate>Sat, 05 Jun 2021 19:08:12 GMT</pubDate>
    <dc:creator>xyxu</dc:creator>
    <dc:date>2021-06-05T19:08:12Z</dc:date>
    <item>
      <title>Compute weighted average using SQL: missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compute-weighted-average-using-SQL-missing-values/m-p/745953#M233927</link>
      <description>&lt;P&gt;I compute weighted average using the following&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table mytable as
select sum(X * Y) / sum(Y) as wave, mean(X) as ave
from my_dataset
group by Z;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I realize that there are errors in computed weighted average "wave" when some values of variable "X" are missing within a group "Z", whereas "Y" are nonmissing. The denominator would be the sum of all nonmissing values of "Y", but the numerator is only the sum of nonmissing "X". Is there a good practice to solve this issue?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 05 Jun 2021 01:43:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compute-weighted-average-using-SQL-missing-values/m-p/745953#M233927</guid>
      <dc:creator>xyxu</dc:creator>
      <dc:date>2021-06-05T01:43:48Z</dc:date>
    </item>
    <item>
      <title>Re: Compute weighted average using SQL: missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compute-weighted-average-using-SQL-missing-values/m-p/745954#M233928</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table mytable as
select sum(X * Y) / sum(case when X is missing then . else Y end) as wave, mean(X) as ave
from my_dataset
group by Z;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(untested)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You might want to modify the expression for ave as well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 05 Jun 2021 01:52:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compute-weighted-average-using-SQL-missing-values/m-p/745954#M233928</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2021-06-05T01:52:57Z</dc:date>
    </item>
    <item>
      <title>Re: Compute weighted average using SQL: missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compute-weighted-average-using-SQL-missing-values/m-p/746003#M233956</link>
      <description>&lt;P&gt;Now, if you wanted wave and ave to be consistent (i.e. to represent the same data), things would be simpler:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table mytable as
select sum(X * Y) / sum(Y) as wave, mean(X) as ave
from my_dataset
where nmiss(X, Y) = 0
group by Z;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(untested)&lt;/P&gt;</description>
      <pubDate>Sat, 05 Jun 2021 15:45:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compute-weighted-average-using-SQL-missing-values/m-p/746003#M233956</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2021-06-05T15:45:12Z</dc:date>
    </item>
    <item>
      <title>Re: Compute weighted average using SQL: missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compute-weighted-average-using-SQL-missing-values/m-p/746030#M233971</link>
      <description>&lt;P&gt;I want to compute wave and ave using the maximum number of nonmissing observations. My current code is&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table mytable as
select sum(X * Y) / sum(Y * (X ne .) ) as wave, mean(X) as ave
from my_dataset
group by Z;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This way wave and ave should be both valid and comparable?&lt;/P&gt;</description>
      <pubDate>Sat, 05 Jun 2021 19:08:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compute-weighted-average-using-SQL-missing-values/m-p/746030#M233971</guid>
      <dc:creator>xyxu</dc:creator>
      <dc:date>2021-06-05T19:08:12Z</dc:date>
    </item>
    <item>
      <title>Re: Compute weighted average using SQL: missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compute-weighted-average-using-SQL-missing-values/m-p/746031#M233972</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/99650"&gt;@xyxu&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I want to compute wave and ave using the maximum number of nonmissing observations. My current code is&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table mytable as
select sum(X * Y) / sum(Y * (X ne .) ) as wave, mean(X) as ave
from my_dataset
group by Z;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This way wave and ave should be both valid and comparable?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If there are missing Y values corresponding to non-missing X values then&lt;EM&gt; ave&lt;/EM&gt; will be based on more observations than &lt;EM&gt;wave&lt;/EM&gt;.&lt;/P&gt;</description>
      <pubDate>Sat, 05 Jun 2021 19:14:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compute-weighted-average-using-SQL-missing-values/m-p/746031#M233972</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2021-06-05T19:14:53Z</dc:date>
    </item>
    <item>
      <title>Re: Compute weighted average using SQL: missing values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compute-weighted-average-using-SQL-missing-values/m-p/746033#M233973</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/99650"&gt;@xyxu&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I compute weighted average using the following&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table mytable as
select sum(X * Y) / sum(Y) as wave, mean(X) as ave
from my_dataset
group by Z;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I realize that there are errors in computed weighted average "wave" when some values of variable "X" are missing within a group "Z", whereas "Y" are nonmissing. The denominator would be the sum of all nonmissing values of "Y", but the numerator is only the sum of nonmissing "X". Is there a good practice to solve this issue?&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My opinion, a good practice to solve this issue, is that you should compute weighted averages in PROC SUMMARY/PROC MEANS because SAS gets it right in the presence of missing values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you use your own SQL code, you could easily get it wrong.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You (or your company or university) are paying for SAS and all of its benefits, including the work that went into getting this correct and applicable when missings are present, the debugging work and the verification that SAS performs. Use that work. Don't do it your self.&lt;/P&gt;</description>
      <pubDate>Sat, 05 Jun 2021 20:31:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compute-weighted-average-using-SQL-missing-values/m-p/746033#M233973</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-06-05T20:31:23Z</dc:date>
    </item>
  </channel>
</rss>

