<?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 conditional JOIN with OR operator or CASE WHEN in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-conditional-JOIN-with-OR-operator-or-CASE-WHEN/m-p/288472#M269945</link>
    <description>&lt;P&gt;A join considers all the conditionals when evaulating each row. &amp;nbsp;You can split it up by (). &amp;nbsp;However in the example you gave, what you seem to be looking for is a distinct list. &amp;nbsp;To do your join, process your incoming data such that it matches the criteria you want to achieve. &amp;nbsp;Also note, put test data as a datastep so we can see what the data is:&lt;/P&gt;
&lt;PRE&gt;data ik;
  length kn_code $50;
  kn_code="08022200"; output;
  kn_code="08022200"; output;
run;

data pb;
  length kn_code $50;
  kn_code="08022200"; output;
  kn_code="ex08022200"; output;
run;

proc sql;
  create table WANT as
  select  distinct
          IK.KN_CODE as IK_KN_CODE,
          PB.KN_CODE as PB_KN_CODE
  from    (select strip(tranwrd(KN_CODE,"ex","")) as KN_CODE from IK) IK
  full join (select strip(tranwrd(KN_CODE,"ex","")) as KN_CODE from PB) PB
  on      IK.KN_CODE=PB.KN_CODE;
quit;&lt;/PRE&gt;</description>
    <pubDate>Mon, 01 Aug 2016 08:30:02 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2016-08-01T08:30:02Z</dc:date>
    <item>
      <title>PROC SQL conditional JOIN with OR operator or CASE WHEN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-conditional-JOIN-with-OR-operator-or-CASE-WHEN/m-p/288119#M269940</link>
      <description>&lt;P&gt;Hi SAS Experts,&lt;BR /&gt;&lt;BR /&gt;I got a simple PROC SQL statement that does a conditional joining to another table.&lt;BR /&gt;The problem is that the OR is not treated sequentially but more like an AS WELL AS.&lt;BR /&gt;The data gets multiplied. But I need it as an EITHER rather than an AS WELL AS.&lt;BR /&gt;&lt;BR /&gt;I tried the same with a CASE WHEN but no luck. The first works fine, 5 rows but the second produces 5 rows again although it’s a clear non-match. Only the first condition actually works.&lt;BR /&gt;&lt;BR /&gt;proc sql ; &lt;BR /&gt;CREATE TABLE Import AS&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;SELECT DISTINCT&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;X&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; FROM Importkont ik&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LEFT JOIN &lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p_import pb &lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ON &lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (COMPRESS(ik.KN_CODE) = pb.CN ) &lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;OR&amp;nbsp; ('ex'||COMPRESS(ik.KN_CODE) = pb.CN ) &amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;;&lt;BR /&gt;QUIT;&lt;/P&gt;</description>
      <pubDate>Fri, 29 Jul 2016 13:57:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-conditional-JOIN-with-OR-operator-or-CASE-WHEN/m-p/288119#M269940</guid>
      <dc:creator>metallon</dc:creator>
      <dc:date>2016-07-29T13:57:26Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL conditional JOIN with OR operator or CASE WHEN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-conditional-JOIN-with-OR-operator-or-CASE-WHEN/m-p/288133#M269941</link>
      <description>&lt;P&gt;Two things, first there is no test data (form of a datastep) to test this on, so this is untested, secondly code formatting makes code far easier to read. &amp;nbsp;Now for your problem (and no test data so can't say), can you not just strip out the EX from the other variable. &amp;nbsp;It looks like your attempting to merge all of IK to PB, regardless of wether the start with EX or not, so just remove that bit:&lt;/P&gt;
&lt;PRE&gt;proc sql; 
  create table import as
  select  distinct X
  from    IMPORTKONT IK
  left join P_IMPORT PB 
  on      strip(IK.KN_CODE)=strip(tranwrd(PB.CN,"ex",""));
quit;&lt;/PRE&gt;</description>
      <pubDate>Fri, 29 Jul 2016 14:33:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-conditional-JOIN-with-OR-operator-or-CASE-WHEN/m-p/288133#M269941</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-07-29T14:33:42Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL conditional JOIN with OR operator or CASE WHEN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-conditional-JOIN-with-OR-operator-or-CASE-WHEN/m-p/288134#M269942</link>
      <description>Please attach some sample data, it would help try your logic.</description>
      <pubDate>Fri, 29 Jul 2016 14:34:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-conditional-JOIN-with-OR-operator-or-CASE-WHEN/m-p/288134#M269942</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2016-07-29T14:34:46Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL conditional JOIN with OR operator or CASE WHEN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-conditional-JOIN-with-OR-operator-or-CASE-WHEN/m-p/288192#M269943</link>
      <description>&lt;P&gt;EITHER, meaning when either condition match but not both would be&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ON (COMPRESS(ik.KN_CODE) = pb.CN) EQ ('ex'||COMPRESS(ik.KN_CODE) = pb.CN )&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;but both conditions can never match at the same time. So, AS WELL AS should give the same result as EITHER.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please illustrate the problem further with some test data and expected results.&lt;/P&gt;</description>
      <pubDate>Mon, 01 Aug 2016 18:08:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-conditional-JOIN-with-OR-operator-or-CASE-WHEN/m-p/288192#M269943</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-08-01T18:08:37Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL conditional JOIN with OR operator or CASE WHEN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-conditional-JOIN-with-OR-operator-or-CASE-WHEN/m-p/288461#M269944</link>
      <description>Hi everyone,&lt;BR /&gt;&lt;BR /&gt;Thanks for the replies!&lt;BR /&gt;I try to first match without adding 'ex' that is the number one priority join codintion&lt;BR /&gt;only then I want to join whatever could not be joined by adding 'ex' and seeing if there&lt;BR /&gt;is a match.&lt;BR /&gt;&lt;BR /&gt;Your suggestion&lt;BR /&gt;&lt;BR /&gt;"      (COMPRESS(ik.KN_CODE) = pb.CN )&lt;BR /&gt;&lt;BR /&gt;        OR  ('ex'||COMPRESS(ik.KN_CODE) = pb.CN )  "&lt;BR /&gt;&lt;BR /&gt;did not improve my result.&lt;BR /&gt;&lt;BR /&gt;Addressing the urgend call for test data. Here it comes:&lt;BR /&gt;&lt;BR /&gt;left table ik&lt;BR /&gt;&lt;BR /&gt;08022200&lt;BR /&gt;08022200&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;right table (pb)&lt;BR /&gt;&lt;BR /&gt;08022200&lt;BR /&gt;ex08022200&lt;BR /&gt;&lt;BR /&gt;expected result:&lt;BR /&gt;&lt;BR /&gt;ik		pb&lt;BR /&gt;08022200	08022200&lt;BR /&gt;&lt;BR /&gt;attained result:&lt;BR /&gt;&lt;BR /&gt;ik		pb&lt;BR /&gt;08022200	08022200&lt;BR /&gt;08022200        ex08022200&lt;BR /&gt;&lt;BR /&gt;I need a join condition that does not look at a second condition if the first&lt;BR /&gt;one matched. ex08022200 should have never been joined with 08022200 because 08022200=08022200 worked&lt;BR /&gt;and I dont want that the join considers the second condition i.e. 'ex'||08022200=08022200&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Mon, 01 Aug 2016 07:57:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-conditional-JOIN-with-OR-operator-or-CASE-WHEN/m-p/288461#M269944</guid>
      <dc:creator>metallon</dc:creator>
      <dc:date>2016-08-01T07:57:02Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL conditional JOIN with OR operator or CASE WHEN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-conditional-JOIN-with-OR-operator-or-CASE-WHEN/m-p/288472#M269945</link>
      <description>&lt;P&gt;A join considers all the conditionals when evaulating each row. &amp;nbsp;You can split it up by (). &amp;nbsp;However in the example you gave, what you seem to be looking for is a distinct list. &amp;nbsp;To do your join, process your incoming data such that it matches the criteria you want to achieve. &amp;nbsp;Also note, put test data as a datastep so we can see what the data is:&lt;/P&gt;
&lt;PRE&gt;data ik;
  length kn_code $50;
  kn_code="08022200"; output;
  kn_code="08022200"; output;
run;

data pb;
  length kn_code $50;
  kn_code="08022200"; output;
  kn_code="ex08022200"; output;
run;

proc sql;
  create table WANT as
  select  distinct
          IK.KN_CODE as IK_KN_CODE,
          PB.KN_CODE as PB_KN_CODE
  from    (select strip(tranwrd(KN_CODE,"ex","")) as KN_CODE from IK) IK
  full join (select strip(tranwrd(KN_CODE,"ex","")) as KN_CODE from PB) PB
  on      IK.KN_CODE=PB.KN_CODE;
quit;&lt;/PRE&gt;</description>
      <pubDate>Mon, 01 Aug 2016 08:30:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-conditional-JOIN-with-OR-operator-or-CASE-WHEN/m-p/288472#M269945</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-08-01T08:30:02Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL conditional JOIN with OR operator or CASE WHEN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-conditional-JOIN-with-OR-operator-or-CASE-WHEN/m-p/288490#M269946</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am sorry but the DISTINCT wont work. That is the problem with providing "test" data. It is hardly ever complete.&lt;/P&gt;
&lt;P&gt;There are many more rows that make the rows truly unique. I need a prioritiested conditional join handling.&lt;/P&gt;</description>
      <pubDate>Mon, 01 Aug 2016 09:17:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-conditional-JOIN-with-OR-operator-or-CASE-WHEN/m-p/288490#M269946</guid>
      <dc:creator>metallon</dc:creator>
      <dc:date>2016-08-01T09:17:54Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL conditional JOIN with OR operator or CASE WHEN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-conditional-JOIN-with-OR-operator-or-CASE-WHEN/m-p/288493#M269947</link>
      <description>&lt;P&gt;Without seeing some test data which matches your requirements, I don't see how we can supply logic?&lt;/P&gt;</description>
      <pubDate>Mon, 01 Aug 2016 09:22:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-conditional-JOIN-with-OR-operator-or-CASE-WHEN/m-p/288493#M269947</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-08-01T09:22:42Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL conditional JOIN with OR operator or CASE WHEN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-conditional-JOIN-with-OR-operator-or-CASE-WHEN/m-p/288495#M269948</link>
      <description>Abstract thinking. I believe in you. You can do it!&lt;BR /&gt;&lt;BR /&gt;This is a solution I just came up with 2 minutes ago. I basically exclude with a subquery all the rows&lt;BR /&gt;that did match in the first join. But its a bit lame I think:&lt;BR /&gt;..&lt;BR /&gt;ON            &lt;BR /&gt;COMPRESS(ik.KN_CODE) = pb.CN    &lt;BR /&gt;OR &lt;BR /&gt;(&lt;BR /&gt;'ex'||COMPRESS(ik.KN_CODE) = pb.CN  AND ik.KN_CODE NOT IN (SELECT ik.KN_CODE FROM yyy ik INNER JOIN xxx pb ON COMPRESS(ik.KN_CODE) = pb.CN ) &lt;BR /&gt;) 	&lt;BR /&gt;</description>
      <pubDate>Mon, 01 Aug 2016 09:31:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-conditional-JOIN-with-OR-operator-or-CASE-WHEN/m-p/288495#M269948</guid>
      <dc:creator>metallon</dc:creator>
      <dc:date>2016-08-01T09:31:14Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL conditional JOIN with OR operator or CASE WHEN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-conditional-JOIN-with-OR-operator-or-CASE-WHEN/m-p/288680#M269949</link>
      <description>&lt;P&gt;I would try:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
CREATE TABLE import AS
SELECT coalesce(ex.X, pb.X) as X
FROM 
    Importkont as ik LEFT JOIN 
    p_import as ex on cats("ex", ik.KN_CODE) = ex.CN left join
    p_import as pb ON COMPRESS(ik.KN_CODE) = pb.CN;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and add DISTINCT if needed.&lt;/P&gt;</description>
      <pubDate>Mon, 01 Aug 2016 18:33:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/PROC-SQL-conditional-JOIN-with-OR-operator-or-CASE-WHEN/m-p/288680#M269949</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-08-01T18:33:14Z</dc:date>
    </item>
  </channel>
</rss>

