Solved
Contributor
Posts: 29

# Proc sql: sum if not exists?

[ Edited ]

Hello,

Here is a simplified example of what I trie to do :

``````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;``````

I have no row with this request but I would like to have a row with "4 0 4000". How to deal with this? I tried coalesce by the same way and it did not work too.

Thanks

Accepted Solutions
Solution
‎10-27-2015 06:31 AM
Regular Contributor
Posts: 161

## Re: Proc sql: sum if not exists?

[ Edited ]

Hope this helps:

``````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;``````
Kannan Deivasigamani

All Replies
Regular Contributor
Posts: 161

## Re: Proc sql: sum if not exists?

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.

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.

Kannan Deivasigamani
Contributor
Posts: 29

## Re: Proc sql: sum if not exists?

Sorry I did a mistake. I wanted to say "4 0 4000"

a.date is my reference date. Let us consider that there is always only one row into a.
Regular Contributor
Posts: 161

## Re: Proc sql: sum if not exists?

Try  "A LEFT JOIN B"

Kannan Deivasigamani
Solution
‎10-27-2015 06:31 AM
Regular Contributor
Posts: 161

## Re: Proc sql: sum if not exists?

[ Edited ]

Hope this helps:

``````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;``````
Kannan Deivasigamani
Super User
Posts: 9,599

## Re: Proc sql: sum if not exists?

Yes, provide and example of what you want out, and why.

Contributor
Posts: 29

## Re: Proc sql: sum if not exists?

Left join, I forgot this basic trick T_T

Occasional Contributor
Posts: 10

## Re: Proc sql: sum if not exists?

[ Edited ]

A full join may be what you need.  I would also anticipate the need to rollup by date both sales and purchases prior to joining.

```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
;```

🔒 This topic is solved and locked.