<?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: Proc SQL left join in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-left-join/m-p/815216#M321777</link>
    <description>&lt;P&gt;For the posted code you could just use normal SAS code instead of SQL.&amp;nbsp; The only reason to be forced to have to jury rig something using SQL would be if you wanted to do a many to many join.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want the values from B to overwrite the values from A then use a MERGE.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  merge a b;
  by id1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you only want the non-missing values from B to override the values from A then use UPDATE instead.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  update a b;
  by id1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 26 May 2022 12:49:39 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-05-26T12:49:39Z</dc:date>
    <item>
      <title>Proc SQL left join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-left-join/m-p/815200#M321765</link>
      <description>&lt;P&gt;i have 2 datasets :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A 1 p &lt;BR /&gt;B 2 p&lt;BR /&gt;C 1 p&lt;BR /&gt;D 1 p&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;B 2 s&lt;BR /&gt;D 1 s&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and want to merge to this :&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A 1 p&lt;BR /&gt;B 2 s&lt;BR /&gt;C 1 p&lt;BR /&gt;D 1 s&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;this is the V1 is always populated with values from both tables A and B&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
input ID1 $ ID2 V1 $;
 datalines;
A 1 p 
B 2 p
C 1 p
D 1 p
;
run;

data b;
input ID1 $ ID2 V1 $;
 datalines;
B 2 s
D 1 s
;
run;&lt;BR /&gt;&lt;BR /&gt;
proc sql;
create table c  as
 select
 x.ID1,
 x.ID2,
 y.v1
  from A  as x left join B as y
on x.ID1 =y.ID1 and x.ID2 =y.ID2; 
quit;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 26 May 2022 11:52:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-left-join/m-p/815200#M321765</guid>
      <dc:creator>Toni2</dc:creator>
      <dc:date>2022-05-26T11:52:49Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL left join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-left-join/m-p/815203#M321767</link>
      <description>&lt;PRE&gt;data a;
input ID1 $ ID2 V1 $;
 datalines;
A 1 p 
B 2 p
C 1 p
D 1 p
;
run;

data b;
input ID1 $ ID2 V1 $;
 datalines;
B 2 s
D 1 s
;
run;
proc sql;
create table c  as
 select
 x.ID1,
 x.ID2,
 coalescec(y.v1,x.v1) as v1
  from A  as x left join B as y
on x.ID1 =y.ID1 and x.ID2 =y.ID2; 
quit;&lt;/PRE&gt;</description>
      <pubDate>Thu, 26 May 2022 12:09:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-left-join/m-p/815203#M321767</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-05-26T12:09:55Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL left join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-left-join/m-p/815204#M321768</link>
      <description>&lt;P&gt;Thank you for providing all the sample data as fully working data steps, providing the SQL you've already got and also showing us the desired result. That made it really simple to "fill in" the last little gap which was that you didn't actually formulate your question.&lt;/P&gt;
&lt;P&gt;What you're looking for is the coalesce() function which will pick the first non-missing value.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
  input ID1 $ ID2 V1 $;
  datalines;
A 1 p 
B 2 p
C 1 p
D 1 p
;
run;

data b;
  input ID1 $ ID2 V1 $;
  datalines;
B 2 s
D 1 s
;
run;

proc sql;
  create table c  as
    select
      x.ID1,
      x.ID2,
      coalesce(y.v1,x.v1) as v1
    from A  as x left join B as y
      on x.ID1 =y.ID1 and x.ID2 =y.ID2;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 May 2022 12:14:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-left-join/m-p/815204#M321768</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2022-05-26T12:14:09Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL left join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-left-join/m-p/815216#M321777</link>
      <description>&lt;P&gt;For the posted code you could just use normal SAS code instead of SQL.&amp;nbsp; The only reason to be forced to have to jury rig something using SQL would be if you wanted to do a many to many join.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want the values from B to overwrite the values from A then use a MERGE.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  merge a b;
  by id1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you only want the non-missing values from B to override the values from A then use UPDATE instead.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  update a b;
  by id1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 26 May 2022 12:49:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-left-join/m-p/815216#M321777</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-05-26T12:49:39Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL left join</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-left-join/m-p/815224#M321780</link>
      <description>&lt;P&gt;Generic comment: If there are &lt;STRONG&gt;no duplicates&lt;/STRONG&gt; of the ID and ID2 combination in the first data set then this is actually an UPDATE, replacing a value on matching values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data c;
  update a b;
  by id1 id2;
run;&lt;/PRE&gt;
&lt;P&gt;Though the data step UPDATE will require sets A and B to be sorted by the By variables if not actually in that order.&lt;/P&gt;</description>
      <pubDate>Thu, 26 May 2022 14:08:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-SQL-left-join/m-p/815224#M321780</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-05-26T14:08:59Z</dc:date>
    </item>
  </channel>
</rss>

