<?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 SQL group by + join in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/SQL-group-by-join/m-p/242354#M55789</link>
    <description>&lt;P&gt;How can I perform an sql join using a dataset cretaed by the sql group-by statement. Example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sales;
	input id $1. period sale;
	cards;
A 1 100
A 2 200
B 1 150
B 2 250
	;
run;

data names;
	input id $1. name $7.;
	cards;
A nameA
B nameB
	;
run;

data want;
	input id $1. sum_sales name $7.;
	cards;
A 300 nameA
B 400 nameB
	;
run;&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;I've tried to use the following code but it doesn't work. I know I could first merge the datasets sales and names and then run proc sql (or proc means) to obtain sum, but I wonder what is the problem with the code below.&lt;/P&gt;
&lt;PRE&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;CODE class=" language-sas"&gt;proc sql;

create table want as

(select id, sum(sale) as sum_sale

from sales

group by id) A

left join names B

on A.id=names.id;

quit;
&lt;/CODE&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 08 Jan 2016 12:07:59 GMT</pubDate>
    <dc:creator>chris2377</dc:creator>
    <dc:date>2016-01-08T12:07:59Z</dc:date>
    <item>
      <title>SQL group by + join</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/SQL-group-by-join/m-p/242354#M55789</link>
      <description>&lt;P&gt;How can I perform an sql join using a dataset cretaed by the sql group-by statement. Example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sales;
	input id $1. period sale;
	cards;
A 1 100
A 2 200
B 1 150
B 2 250
	;
run;

data names;
	input id $1. name $7.;
	cards;
A nameA
B nameB
	;
run;

data want;
	input id $1. sum_sales name $7.;
	cards;
A 300 nameA
B 400 nameB
	;
run;&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;I've tried to use the following code but it doesn't work. I know I could first merge the datasets sales and names and then run proc sql (or proc means) to obtain sum, but I wonder what is the problem with the code below.&lt;/P&gt;
&lt;PRE&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;CODE class=" language-sas"&gt;proc sql;

create table want as

(select id, sum(sale) as sum_sale

from sales

group by id) A

left join names B

on A.id=names.id;

quit;
&lt;/CODE&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 08 Jan 2016 12:07:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/SQL-group-by-join/m-p/242354#M55789</guid>
      <dc:creator>chris2377</dc:creator>
      <dc:date>2016-01-08T12:07:59Z</dc:date>
    </item>
    <item>
      <title>Re: SQL group by + join</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/SQL-group-by-join/m-p/242357#M55791</link>
      <description>&lt;P&gt;In your code the (outer) SELECT statement is missing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Corrected version:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as select a.*, b.name from
(select id, sum(sale) as sum_sales
 from sales
 group by id) A
left join names B
on A.id=names.id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here's an alternative solution without an inline view, i.e. with only one SELECT statement:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select a.id, sum(sale) as sum_sales, name
from sales a, names b
where a.id=b.id
group by a.id, name;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 08 Jan 2016 12:34:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/SQL-group-by-join/m-p/242357#M55791</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-01-08T12:34:29Z</dc:date>
    </item>
    <item>
      <title>Re: SQL group by + join</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/SQL-group-by-join/m-p/242358#M55792</link>
      <description>Thanks a lot. Now it's clear</description>
      <pubDate>Fri, 08 Jan 2016 12:53:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/SQL-group-by-join/m-p/242358#M55792</guid>
      <dc:creator>chris2377</dc:creator>
      <dc:date>2016-01-08T12:53:39Z</dc:date>
    </item>
  </channel>
</rss>

