BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
problems99
Calcite | Level 5

 

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

1 ACCEPTED SOLUTION

Accepted Solutions
kannand
Lapis Lazuli | Level 10

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

View solution in original post

7 REPLIES 7
kannand
Lapis Lazuli | Level 10

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
problems99
Calcite | Level 5
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.
kannand
Lapis Lazuli | Level 10

Try  "A LEFT JOIN B"

Kannan Deivasigamani
kannand
Lapis Lazuli | Level 10

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
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

problems99
Calcite | Level 5

Left join, I forgot this basic trick T_T

 

Thanks everyone for your help and your quickness!

RichardDeVen
Barite | Level 11

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
;

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 1864 views
  • 0 likes
  • 4 in conversation