<?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: join two tables using proc sql in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/join-two-tables-using-proc-sql/m-p/506329#M135718</link>
    <description>&lt;P&gt;I see... thanks, I should have added the explanation and logic. I'm sorry..&lt;/P&gt;</description>
    <pubDate>Sun, 21 Oct 2018 20:14:46 GMT</pubDate>
    <dc:creator>asinusdk</dc:creator>
    <dc:date>2018-10-21T20:14:46Z</dc:date>
    <item>
      <title>join two tables using proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/join-two-tables-using-proc-sql/m-p/506320#M135710</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Hi, I wonder what kind of joining this kind of table it is.&lt;BR /&gt;Could you please let me know proc sql code?&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;data a;
input year  id	v1;
cards;
1 1 23
2 1 25
3 1 26
1 3 27 
2 3 28
;

data b;
input year id v2 v3;
cards;
1 1 1 34 
2 1 0 35
3 1 1 245
1 2 0 234
2 2 1 325
3 2 0 3
1 3 1 25
2 3 0 23
3 3 1 34
;
&lt;BR /&gt;The expected table looks like this.&lt;BR /&gt;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;year&lt;/TD&gt;&lt;TD&gt;id&lt;/TD&gt;&lt;TD&gt;numb&lt;/TD&gt;&lt;TD&gt;v1&lt;/TD&gt;&lt;TD&gt;v2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;23&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;34&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;25&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;35&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;26&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;245&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;27&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;25&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;28&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;23&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;34&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;BR /&gt;+addition: I want to extract all the information (v2 and v3 for all years 1-3) from table b&lt;/P&gt;&lt;P&gt;(even though there is one or two rows in table a) for only ids existing in table a.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 21 Oct 2018 20:11:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/join-two-tables-using-proc-sql/m-p/506320#M135710</guid>
      <dc:creator>asinusdk</dc:creator>
      <dc:date>2018-10-21T20:11:40Z</dc:date>
    </item>
    <item>
      <title>Re: join two tables using proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/join-two-tables-using-proc-sql/m-p/506321#M135711</link>
      <description>&lt;P&gt;Because it is not clear why you include the last row in your desired output table, this desired output doesn't correspond to any built in JOIN in PROC SQL. If you can clearly state a reason why that last row is included, then it becomes easier to figure out how it can be programmed.&lt;/P&gt;</description>
      <pubDate>Sun, 21 Oct 2018 19:39:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/join-two-tables-using-proc-sql/m-p/506321#M135711</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-10-21T19:39:01Z</dc:date>
    </item>
    <item>
      <title>Re: join two tables using proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/join-two-tables-using-proc-sql/m-p/506324#M135714</link>
      <description>&lt;P&gt;Thanks for your response.&lt;/P&gt;&lt;P&gt;I want to extract all the information (all years 1-3) from table b (even though there&lt;BR /&gt;is one or two rows in table a) for only ids existing in table a.&lt;/P&gt;</description>
      <pubDate>Sun, 21 Oct 2018 19:54:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/join-two-tables-using-proc-sql/m-p/506324#M135714</guid>
      <dc:creator>asinusdk</dc:creator>
      <dc:date>2018-10-21T19:54:18Z</dc:date>
    </item>
    <item>
      <title>Re: join two tables using proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/join-two-tables-using-proc-sql/m-p/506325#M135715</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
input year  id	v1;
cards;
1 1 23
2 1 25
3 1 26
1 3 27 
2 3 28
;

data b;
input year id v2 v3;
cards;
1 1 1 34 
2 1 0 35
3 1 1 245
1 2 0 234
2 2 1 325
3 2 0 3
1 3 1 25
2 3 0 23
3 3 1 34
;

data want;
if 0 then merge a b;
call missing(v1);
merge a(in=a) b(in=b);
by id;
if a and b;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 21 Oct 2018 20:00:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/join-two-tables-using-proc-sql/m-p/506325#M135715</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-10-21T20:00:23Z</dc:date>
    </item>
    <item>
      <title>Re: join two tables using proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/join-two-tables-using-proc-sql/m-p/506326#M135716</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/240083"&gt;@asinusdk&lt;/a&gt;&amp;nbsp; Honestly the above is just to see data and attempt to get the output using some code I didn't pay attention to logical business need or objective as the question didn't reflect that. In other words, there is no what &amp;amp; why&lt;/P&gt;</description>
      <pubDate>Sun, 21 Oct 2018 20:02:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/join-two-tables-using-proc-sql/m-p/506326#M135716</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-10-21T20:02:12Z</dc:date>
    </item>
    <item>
      <title>Re: join two tables using proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/join-two-tables-using-proc-sql/m-p/506328#M135717</link>
      <description>&lt;P&gt;what if there are more variable in data a and I want to include these variables in the expected outcome?&lt;/P&gt;</description>
      <pubDate>Sun, 21 Oct 2018 20:14:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/join-two-tables-using-proc-sql/m-p/506328#M135717</guid>
      <dc:creator>asinusdk</dc:creator>
      <dc:date>2018-10-21T20:14:06Z</dc:date>
    </item>
    <item>
      <title>Re: join two tables using proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/join-two-tables-using-proc-sql/m-p/506329#M135718</link>
      <description>&lt;P&gt;I see... thanks, I should have added the explanation and logic. I'm sorry..&lt;/P&gt;</description>
      <pubDate>Sun, 21 Oct 2018 20:14:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/join-two-tables-using-proc-sql/m-p/506329#M135718</guid>
      <dc:creator>asinusdk</dc:creator>
      <dc:date>2018-10-21T20:14:46Z</dc:date>
    </item>
    <item>
      <title>Re: join two tables using proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/join-two-tables-using-proc-sql/m-p/506330#M135719</link>
      <description>&lt;P&gt;did you try my code?&lt;/P&gt;</description>
      <pubDate>Sun, 21 Oct 2018 20:19:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/join-two-tables-using-proc-sql/m-p/506330#M135719</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-10-21T20:19:06Z</dc:date>
    </item>
    <item>
      <title>Re: join two tables using proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/join-two-tables-using-proc-sql/m-p/506331#M135720</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/240083"&gt;@asinusdk&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;what if there are more variable in data a and I want to include these variables in the expected outcome?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/240083"&gt;@asinusdk&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
input year id v1 morevar;
cards;
1 1 23 9
2 1 25 8
3 1 26 7
1 3 27 6
2 3 28 5
;

data b;
input year id v2 v3;
cards;
1 1 1 34 
2 1 0 35
3 1 1 245
1 2 0 234
2 2 1 325
3 2 0 3
1 3 1 25
2 3 0 23
3 3 1 34
;

proc sql;
create table want as
select b.year, b.id, v1 as numb, v2 as v1, v3 as v2, morevar
from a right join b
on a.id=b.id &amp;amp; a.year=b.year
where exists (select * from a where a.id=b.id)
order by id, year;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Edit:&lt;/P&gt;
&lt;P&gt;Slightly shorter with&lt;/P&gt;
&lt;PRE&gt;from a &lt;STRONG&gt;natural&lt;/STRONG&gt; right join b&lt;/PRE&gt;
&lt;P&gt;and without the ON clause.&lt;/P&gt;</description>
      <pubDate>Sun, 21 Oct 2018 20:32:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/join-two-tables-using-proc-sql/m-p/506331#M135720</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2018-10-21T20:32:10Z</dc:date>
    </item>
  </channel>
</rss>

