<?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 Proc sql: sum if not exists? in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Proc-sql-sum-if-not-exists/m-p/231783#M54609</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is a simplified example of what I trie to do :&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;data a;
input date sales;
datalines;
4 4000
;

data b;
input date value_purchased_items;
datalines;
1 100
2 200
3 300
;

proc sql;
create table summary as
select a.date, sum(b.value_purchased_items,0), a.sales
from  a a, b b
where a.date=b.date;&lt;BR /&gt;&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;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;I have no row with this request but I would like to have a row with&amp;nbsp;"4 0 4000". How to deal with this? I tried coalesce by the same way and it did not work too.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;Thanks&lt;/FONT&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 27 Oct 2015 10:14:18 GMT</pubDate>
    <dc:creator>problems99</dc:creator>
    <dc:date>2015-10-27T10:14:18Z</dc:date>
    <item>
      <title>Proc sql: sum if not exists?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-sql-sum-if-not-exists/m-p/231783#M54609</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is a simplified example of what I trie to do :&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;data a;
input date sales;
datalines;
4 4000
;

data b;
input date value_purchased_items;
datalines;
1 100
2 200
3 300
;

proc sql;
create table summary as
select a.date, sum(b.value_purchased_items,0), a.sales
from  a a, b b
where a.date=b.date;&lt;BR /&gt;&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;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;I have no row with this request but I would like to have a row with&amp;nbsp;"4 0 4000". How to deal with this? I tried coalesce by the same way and it did not work too.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New" size="2"&gt;Thanks&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Oct 2015 10:14:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-sql-sum-if-not-exists/m-p/231783#M54609</guid>
      <dc:creator>problems99</dc:creator>
      <dc:date>2015-10-27T10:14:18Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql: sum if not exists?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-sql-sum-if-not-exists/m-p/231787#M54610</link>
      <description>&lt;P&gt;Hello, not sure what you are trying to accomplish here. You are getting no rows because of the criteria you have specified in the where clause. There is no date common between the two tables.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Besides, you mentioned that you like to see 3 0 4000. you need to explain based on what criteria you expect this to be on your output so I understand.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Oct 2015 10:10:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-sql-sum-if-not-exists/m-p/231787#M54610</guid>
      <dc:creator>kannand</dc:creator>
      <dc:date>2015-10-27T10:10:04Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql: sum if not exists?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-sql-sum-if-not-exists/m-p/231789#M54611</link>
      <description>&lt;P&gt;Yes, provide and example of what you want out, and why.&lt;/P&gt;</description>
      <pubDate>Tue, 27 Oct 2015 10:14:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-sql-sum-if-not-exists/m-p/231789#M54611</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2015-10-27T10:14:31Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql: sum if not exists?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-sql-sum-if-not-exists/m-p/231790#M54612</link>
      <description>Sorry I did a mistake. I wanted to say "4 0 4000"&lt;BR /&gt;&lt;BR /&gt;a.date is my reference date. Let us consider that there is always only one row into a.</description>
      <pubDate>Tue, 27 Oct 2015 10:16:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-sql-sum-if-not-exists/m-p/231790#M54612</guid>
      <dc:creator>problems99</dc:creator>
      <dc:date>2015-10-27T10:16:59Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql: sum if not exists?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-sql-sum-if-not-exists/m-p/231791#M54613</link>
      <description>&lt;P&gt;Try&amp;nbsp;&amp;nbsp;"A LEFT JOIN B"&lt;/P&gt;</description>
      <pubDate>Tue, 27 Oct 2015 10:22:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-sql-sum-if-not-exists/m-p/231791#M54613</guid>
      <dc:creator>kannand</dc:creator>
      <dc:date>2015-10-27T10:22:55Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql: sum if not exists?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-sql-sum-if-not-exists/m-p/231792#M54614</link>
      <description>&lt;P&gt;Hope this helps:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
input date sales;
datalines;
4 4000
;

data b;
input date value_purchased_items;
datalines;
1 100
2 200
3 300
;

proc sql;
select a.date, sum(b.value_purchased_items,0) as sum_value_purchased_items, a.sales
from  a left join b on
a.date=b.date;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 27 Oct 2015 10:30:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-sql-sum-if-not-exists/m-p/231792#M54614</guid>
      <dc:creator>kannand</dc:creator>
      <dc:date>2015-10-27T10:30:51Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql: sum if not exists?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-sql-sum-if-not-exists/m-p/231793#M54615</link>
      <description>&lt;P&gt;Left join, I forgot this basic trick T_T&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks everyone for your help and your quickness!&lt;/P&gt;</description>
      <pubDate>Tue, 27 Oct 2015 10:34:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-sql-sum-if-not-exists/m-p/231793#M54615</guid>
      <dc:creator>problems99</dc:creator>
      <dc:date>2015-10-27T10:34:46Z</dc:date>
    </item>
    <item>
      <title>Re: Proc sql: sum if not exists?</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Proc-sql-sum-if-not-exists/m-p/231880#M54620</link>
      <description>&lt;P&gt;A full join may be what you need. &amp;nbsp;I would also anticipate the need to rollup by date both sales and purchases prior to joining.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data a;
input date sales;
datalines;
4 4000
5 8000
5 2000
;
data b;
input date value_purchased_items;
datalines;
1 100
2 200
3 300
3 600
5 125
5 250
5 500
5 1000
5 5000
;
proc sql;
create table summary as
select
coalesce(a.date,b.date) as date
, b.value_total
, b.value_line_count
, a.sales_total
, a.sales_line_count
from 
(select date, sum(b.value_purchased_items) as value_total, count(b.value_purchased_items) as value_line_count 
from b group by date) b
full join
(select date, sum(a.sales) as sales_total, count(a.sales) as sales_line_count
from a group by date) a
on
a.date = b.date
;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Oct 2015 17:52:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Proc-sql-sum-if-not-exists/m-p/231880#M54620</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2015-10-27T17:52:44Z</dc:date>
    </item>
  </channel>
</rss>

