<?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: Writing a proc sql join based on conditions in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Writing-a-proc-sql-join-based-on-conditions/m-p/301974#M63986</link>
    <description>&lt;P&gt;It works for me. Does below give you the result you're after?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
  infile datalines dlm = ',';
  input first $ last $;
  datalines;
sri,ram
ram,pra
 ,ali
;
run;

data b;
  infile datalines dlm = ',';
  input third $ fourth $ fifth $;
  datalines;
sri,jagu,good
ram,weas,bad
abdul,ali,best
;
run;

proc sql;
  create table want as
  select a.*, b.*
  from a as X left join b as Y
  on     (x.first is NULL and x.last=y.fourth)
    or (x.first is not NULL and x.first=y.third)
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 03 Oct 2016 07:50:31 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2016-10-03T07:50:31Z</dc:date>
    <item>
      <title>Writing a proc sql join based on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Writing-a-proc-sql-join-based-on-conditions/m-p/301910#M63957</link>
      <description>&lt;P&gt;I have a situation where i have to check a column A and according to that decide the column to use in the join condition. For Example: If column A is null, i need to do left&amp;nbsp;join of tables&amp;nbsp;Table X and Table Y on column B and if Column A is not null, then i need to left join Table X and Y on the column A. Could you please help me how to approach a solution to this problem.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 02 Oct 2016 10:17:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Writing-a-proc-sql-join-based-on-conditions/m-p/301910#M63957</guid>
      <dc:creator>Jagadeesh2907</dc:creator>
      <dc:date>2016-10-02T10:17:51Z</dc:date>
    </item>
    <item>
      <title>Re: Writing a proc sql join based on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Writing-a-proc-sql-join-based-on-conditions/m-p/301911#M63958</link>
      <description>&lt;P&gt;You can have conditions in your join.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is is this what you mean:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;on case when x.a is null then x.b else x.a end = case when y.a is null then y.b else y.a end&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another option is COALESCE type function. Assuming a character variable&amp;nbsp;&lt;/P&gt;
&lt;P&gt;coalescec(x.a, x.b) = coalescec(y.a, y.b)&lt;/P&gt;</description>
      <pubDate>Sun, 02 Oct 2016 10:38:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Writing-a-proc-sql-join-based-on-conditions/m-p/301911#M63958</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-10-02T10:38:26Z</dc:date>
    </item>
    <item>
      <title>Re: Writing a proc sql join based on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Writing-a-proc-sql-join-based-on-conditions/m-p/301928#M63971</link>
      <description>&lt;P&gt;&lt;EM&gt;"If column A is null, i need to do left&amp;nbsp;join of tables&amp;nbsp;Table X and Table Y on column B and if Column A is not null, then i need to left join Table X and Y on the column A"&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For the given example, couldn't the join condition just look as below?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  X left join Y
  on &lt;BR /&gt;    (x.a is NULL and x.B=y.B)
    or (x.a is not NULL and x.A=y.A)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Oct 2016 00:17:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Writing-a-proc-sql-join-based-on-conditions/m-p/301928#M63971</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2016-10-03T00:17:03Z</dc:date>
    </item>
    <item>
      <title>Re: Writing a proc sql join based on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Writing-a-proc-sql-join-based-on-conditions/m-p/301970#M63982</link>
      <description>&lt;P&gt;i tried this, but i am not able to get the records for which the column x.a is null. please find one example below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data a ;&lt;BR /&gt;infile datalines dlm = ',' ;&lt;BR /&gt;input first $ last $;&lt;BR /&gt;datalines;&lt;BR /&gt;sri,ram&lt;BR /&gt;ram,pra&lt;BR /&gt;&amp;nbsp;,ali&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data b;&lt;BR /&gt;infile datalines dlm = ',' ;&lt;BR /&gt;input third $ fourth $ fifth $ ;&lt;BR /&gt;datalines;&lt;BR /&gt;sri,jagu,good&lt;BR /&gt;ram,weas,bad&lt;BR /&gt;abdul,ali,best&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;Method 1:&lt;/P&gt;&lt;P&gt;proc sql ;&lt;BR /&gt;create table c as&lt;BR /&gt;select&lt;BR /&gt;a.first,&lt;BR /&gt;a.last,&lt;BR /&gt;b.fourth,&lt;BR /&gt;b.fifth&lt;BR /&gt;from a left join b&lt;BR /&gt;on case when a.first is null then a.last else a.first end = case when a.first is null then b.fourth else b.third end;&lt;BR /&gt;quit&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Method 2:&lt;/P&gt;&lt;P&gt;proc sql ;&lt;BR /&gt;create table c as&lt;BR /&gt;select&lt;BR /&gt;a.first,&lt;BR /&gt;a.last,&lt;BR /&gt;b.fourth,&lt;BR /&gt;b.fifth&lt;BR /&gt;from a left join b&lt;BR /&gt;on (a.first is NULL and a.last=b.fourth)&amp;nbsp; or&amp;nbsp; (a.first is not NULL and a.first=b.third);&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Method 3:&lt;/P&gt;&lt;P&gt;proc sql ;&lt;BR /&gt;&amp;nbsp;create table c as&lt;BR /&gt;&amp;nbsp;select&lt;BR /&gt;&amp;nbsp;a.first,&lt;BR /&gt;&amp;nbsp;a.last,&lt;BR /&gt;&amp;nbsp;b.fourth,&lt;BR /&gt;&amp;nbsp;b.fifth&lt;BR /&gt;&amp;nbsp;from a left join b&lt;BR /&gt;on coalescec(a.first,a.last) = coalescec(b.third,b.fourth);&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the final result, I am expecting a result with 3 rows where i need the third record to be matched for the value 'ali' since 'Abdul' is missing in the first table. please suggest.&lt;/P&gt;</description>
      <pubDate>Mon, 03 Oct 2016 06:40:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Writing-a-proc-sql-join-based-on-conditions/m-p/301970#M63982</guid>
      <dc:creator>Jagadeesh2907</dc:creator>
      <dc:date>2016-10-03T06:40:28Z</dc:date>
    </item>
    <item>
      <title>Re: Writing a proc sql join based on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Writing-a-proc-sql-join-based-on-conditions/m-p/301971#M63983</link>
      <description>I tried this, but still could not get the records for which X.a is missing. I am using sas 9.2.</description>
      <pubDate>Mon, 03 Oct 2016 06:43:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Writing-a-proc-sql-join-based-on-conditions/m-p/301971#M63983</guid>
      <dc:creator>Jagadeesh2907</dc:creator>
      <dc:date>2016-10-03T06:43:40Z</dc:date>
    </item>
    <item>
      <title>Re: Writing a proc sql join based on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Writing-a-proc-sql-join-based-on-conditions/m-p/301974#M63986</link>
      <description>&lt;P&gt;It works for me. Does below give you the result you're after?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
  infile datalines dlm = ',';
  input first $ last $;
  datalines;
sri,ram
ram,pra
 ,ali
;
run;

data b;
  infile datalines dlm = ',';
  input third $ fourth $ fifth $;
  datalines;
sri,jagu,good
ram,weas,bad
abdul,ali,best
;
run;

proc sql;
  create table want as
  select a.*, b.*
  from a as X left join b as Y
  on     (x.first is NULL and x.last=y.fourth)
    or (x.first is not NULL and x.first=y.third)
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 03 Oct 2016 07:50:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Writing-a-proc-sql-join-based-on-conditions/m-p/301974#M63986</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2016-10-03T07:50:31Z</dc:date>
    </item>
    <item>
      <title>Re: Writing a proc sql join based on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Writing-a-proc-sql-join-based-on-conditions/m-p/301975#M63987</link>
      <description>&lt;P&gt;Please provide sample input data and desired output.&lt;/P&gt;</description>
      <pubDate>Mon, 03 Oct 2016 07:52:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Writing-a-proc-sql-join-based-on-conditions/m-p/301975#M63987</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2016-10-03T07:52:30Z</dc:date>
    </item>
    <item>
      <title>Re: Writing a proc sql join based on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Writing-a-proc-sql-join-based-on-conditions/m-p/302006#M63992</link>
      <description>Thank you Patrick. It works fine now. what mistake I did was I did not create alias for table names and used them as such. Many thanks.</description>
      <pubDate>Mon, 03 Oct 2016 11:27:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Writing-a-proc-sql-join-based-on-conditions/m-p/302006#M63992</guid>
      <dc:creator>Jagadeesh2907</dc:creator>
      <dc:date>2016-10-03T11:27:04Z</dc:date>
    </item>
    <item>
      <title>Re: Writing a proc sql join based on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Writing-a-proc-sql-join-based-on-conditions/m-p/302007#M63993</link>
      <description>Thank you Reeza. I could not use coalescec since I have missing values only on one table and not on both tables used in the join. however, using the case statement works fine now. Earlier I missed to create alias when joining the table hence they were not working. Many thanks for the solution.&lt;BR /&gt;</description>
      <pubDate>Mon, 03 Oct 2016 11:29:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Writing-a-proc-sql-join-based-on-conditions/m-p/302007#M63993</guid>
      <dc:creator>Jagadeesh2907</dc:creator>
      <dc:date>2016-10-03T11:29:10Z</dc:date>
    </item>
    <item>
      <title>Re: Writing a proc sql join based on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Writing-a-proc-sql-join-based-on-conditions/m-p/302008#M63994</link>
      <description>Hi, I got the solution. it was not working since I did not create alias for the joining tables. thank you.</description>
      <pubDate>Mon, 03 Oct 2016 11:30:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Writing-a-proc-sql-join-based-on-conditions/m-p/302008#M63994</guid>
      <dc:creator>Jagadeesh2907</dc:creator>
      <dc:date>2016-10-03T11:30:11Z</dc:date>
    </item>
  </channel>
</rss>

