<?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: PROC SQL - Include zeros in totals in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-Include-zeros-in-totals/m-p/790772#M32537</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data elect;
set SASHELP.ELECTRIC;
if mod(_n_,6)=0 then call missing (revenue);
run;

proc means data=elect nway completetypes missing;
class Customer 'Year'n;
var Revenue;
output out=sumy sum= / autoname;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="a1.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/67486i90858A8B216F1273/image-size/large?v=v2&amp;amp;px=999" role="button" title="a1.png" alt="a1.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 18 Jan 2022 22:13:17 GMT</pubDate>
    <dc:creator>acordes</dc:creator>
    <dc:date>2022-01-18T22:13:17Z</dc:date>
    <item>
      <title>PROC SQL - Include zeros in totals</title>
      <link>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-Include-zeros-in-totals/m-p/790767#M32536</link>
      <description>&lt;P&gt;I have a large table (HAVE) with variables Units, Code, and Year.&amp;nbsp; I'm trying to create a summary of counts by code and year:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc sql;
&amp;nbsp; &amp;nbsp; create table WANT as
&amp;nbsp; &amp;nbsp; select Code, Year, sum(Units) as Total
&amp;nbsp; &amp;nbsp; from HAVE
&amp;nbsp; &amp;nbsp; group by Code, Year
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This works, except there are some combinations of Code and Year that have no Units, which means the resulting table doesn't include those combinations at all.&amp;nbsp; I want to include all combinations, and show 0 in the Total column when there are no Units.&amp;nbsp; I've tried playing with the coalesce() function [as coalesce(sum(Units),0) and as sum(coalesce(Units,0))] but it hasn't given me the desired result so far.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Current result:&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: inherit;"&gt;Code&amp;nbsp; &amp;nbsp; Year&amp;nbsp; &amp;nbsp; Total&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: inherit;"&gt;A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2020&amp;nbsp; &amp;nbsp;1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2020&amp;nbsp; &amp;nbsp;3&lt;/P&gt;&lt;P&gt;B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2021&amp;nbsp; &amp;nbsp;7&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Desired result:&lt;/P&gt;&lt;P&gt;Code&amp;nbsp; &amp;nbsp; Year&amp;nbsp; &amp;nbsp; Total&lt;/P&gt;&lt;P&gt;A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2020&amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2020&amp;nbsp; &amp;nbsp;3&lt;/P&gt;&lt;P&gt;A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2021&amp;nbsp; &amp;nbsp;0&lt;/P&gt;&lt;P&gt;B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2021&amp;nbsp; &amp;nbsp;7&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jan 2022 21:54:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-Include-zeros-in-totals/m-p/790767#M32536</guid>
      <dc:creator>osbornejo</dc:creator>
      <dc:date>2022-01-18T21:54:20Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL - Include zeros in totals</title>
      <link>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-Include-zeros-in-totals/m-p/790772#M32537</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data elect;
set SASHELP.ELECTRIC;
if mod(_n_,6)=0 then call missing (revenue);
run;

proc means data=elect nway completetypes missing;
class Customer 'Year'n;
var Revenue;
output out=sumy sum= / autoname;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="a1.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/67486i90858A8B216F1273/image-size/large?v=v2&amp;amp;px=999" role="button" title="a1.png" alt="a1.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jan 2022 22:13:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-Include-zeros-in-totals/m-p/790772#M32537</guid>
      <dc:creator>acordes</dc:creator>
      <dc:date>2022-01-18T22:13:17Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL - Include zeros in totals</title>
      <link>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-Include-zeros-in-totals/m-p/790776#M32538</link>
      <description>&lt;P&gt;Use PROC FREQ instead.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc freq data=have noprint;
table code*year / out=want SPARSE;
weight units;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/406934"&gt;@osbornejo&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have a large table (HAVE) with variables Units, Code, and Year.&amp;nbsp; I'm trying to create a summary of counts by code and year:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;proc sql;
&amp;nbsp; &amp;nbsp; create table WANT as
&amp;nbsp; &amp;nbsp; select Code, Year, sum(Units) as Total
&amp;nbsp; &amp;nbsp; from HAVE
&amp;nbsp; &amp;nbsp; group by Code, Year
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This works, except there are some combinations of Code and Year that have no Units, which means the resulting table doesn't include those combinations at all.&amp;nbsp; I want to include all combinations, and show 0 in the Total column when there are no Units.&amp;nbsp; I've tried playing with the coalesce() function [as coalesce(sum(Units),0) and as sum(coalesce(Units,0))] but it hasn't given me the desired result so far.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Current result:&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="font-family: inherit;"&gt;Code&amp;nbsp; &amp;nbsp; Year&amp;nbsp; &amp;nbsp; Total&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="font-family: inherit;"&gt;A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2020&amp;nbsp; &amp;nbsp;1&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2020&amp;nbsp; &amp;nbsp;3&lt;/P&gt;
&lt;P&gt;B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2021&amp;nbsp; &amp;nbsp;7&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Desired result:&lt;/P&gt;
&lt;P&gt;Code&amp;nbsp; &amp;nbsp; Year&amp;nbsp; &amp;nbsp; Total&lt;/P&gt;
&lt;P&gt;A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2020&amp;nbsp; &amp;nbsp;1&lt;/P&gt;
&lt;P&gt;B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2020&amp;nbsp; &amp;nbsp;3&lt;/P&gt;
&lt;P&gt;A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2021&amp;nbsp; &amp;nbsp;0&lt;/P&gt;
&lt;P&gt;B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2021&amp;nbsp; &amp;nbsp;7&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jan 2022 22:37:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-Include-zeros-in-totals/m-p/790776#M32538</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-01-18T22:37:57Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL - Include zeros in totals</title>
      <link>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-Include-zeros-in-totals/m-p/790778#M32539</link>
      <description>&lt;P&gt;It should be possible as well&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data elect;
set SASHELP.ELECTRIC;
if mod(_n_,6) ne 0 then output;
run;

proc sql;
create table have2 as 
select c.customer, c.'year'n, case when sum(revenue) in (. 0) then 0 else sum(revenue) end as revenue_sum format = dollar10.
from elect d 
right join 
(
select a.customer, b.'year'n 
from (select distinct customer from elect ) a cross join (select distinct 'year'n from elect) b
) c 
on c.customer||put(c.'year'n, 4.)=d.customer||put(d.'year'n,4.)
group by d.customer, d.'year'n
order by c.customer, c.'year'n 
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 18 Jan 2022 22:44:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-Include-zeros-in-totals/m-p/790778#M32539</guid>
      <dc:creator>acordes</dc:creator>
      <dc:date>2022-01-18T22:44:56Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL - Include zeros in totals</title>
      <link>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-Include-zeros-in-totals/m-p/790874#M32549</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
 set sashelp.class;
rename name=code age=year weight=units;
run;


proc sql;
    create table WANT as
select a.*,coalesce(b.total,0) as total
from (select * from (select distinct code from have),(select distinct year from have)) as a
natural left join
(
    select Code, Year, sum(Units) as Total
    from HAVE
    group by Code, Year 
) as b;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 19 Jan 2022 12:50:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/PROC-SQL-Include-zeros-in-totals/m-p/790874#M32549</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-01-19T12:50:16Z</dc:date>
    </item>
  </channel>
</rss>

