<?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: Using Coalesce for join in proc sql in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-Coalesce-for-join-in-proc-sql/m-p/793016#M254115</link>
    <description>&lt;P&gt;How do you want to connect the two datasets?&amp;nbsp; What is the criteria?&lt;/P&gt;
&lt;P&gt;If you want to test if CODE matches any of the CODE1 to CODE3 values then use WHICHC().&lt;/P&gt;
&lt;P&gt;First let's make some workable example data steps.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test1;
  a_row+1;
  input code $15.;
datalines;
a
b
c
c
;

data test2;
  b_row+1;
  input (code1-code3) (:$20.);
datalines;
a b c
. b a
c . .
e . k
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now let's make a join using the WHICHC() function. It will return which of the three codes were match (1, 2 or 3) and it will set and 0 if not found.&amp;nbsp; SAS will read 0 as FALSE and any other number as TRUE.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as 
  select *
  from test1 a 
  inner join test2 b
    on whichc(a.code,b.code1,b.code2,b.code3)
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;Obs    a_row    code    b_row    code1    code2    code3

 1       1       a        1        a        b        c
 2       1       a        2                 b        a
 3       2       b        1        a        b        c
 4       2       b        2                 b        a
 5       3       c        1        a        b        c
 6       3       c        3        c
 7       4       c        1        a        b        c
 8       4       c        3        c
&lt;/PRE&gt;</description>
    <pubDate>Thu, 27 Jan 2022 22:24:36 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-01-27T22:24:36Z</dc:date>
    <item>
      <title>Using Coalesce for join in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Coalesce-for-join-in-proc-sql/m-p/793012#M254111</link>
      <description>&lt;PRE&gt;data test1;
input  code $15.;
datalines;
a
b
c
c
;

data test2;
input code1 $20. code2 $20. code3.;
datalines;
a b c
b a
c
e    k
;
run;

Proc sql;
create table my table as
select .*
from test1 a inner join test2 b on coalesce (b.code1, b.code2, b.code3);
Quit;&lt;/PRE&gt;
&lt;P&gt;Hello team,&lt;/P&gt;
&lt;P&gt;I want to connect these two datasets:&lt;/P&gt;
&lt;P&gt;First issue is: second dataset is not built properly.&lt;/P&gt;
&lt;P&gt;Second issue is: I need to join first dataset to second dataset, if the code1 is not a match for code, then we need to look at code2 and so on.&lt;/P&gt;
&lt;P&gt;Then I need to add a field to call it myvalues, if a match is found, myvalue should be flagged 1.&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;Blue &amp;amp; Blue&lt;/P&gt;</description>
      <pubDate>Thu, 27 Jan 2022 22:01:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Coalesce-for-join-in-proc-sql/m-p/793012#M254111</guid>
      <dc:creator>GN0001</dc:creator>
      <dc:date>2022-01-27T22:01:22Z</dc:date>
    </item>
    <item>
      <title>Re: Using Coalesce for join in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Coalesce-for-join-in-proc-sql/m-p/793016#M254115</link>
      <description>&lt;P&gt;How do you want to connect the two datasets?&amp;nbsp; What is the criteria?&lt;/P&gt;
&lt;P&gt;If you want to test if CODE matches any of the CODE1 to CODE3 values then use WHICHC().&lt;/P&gt;
&lt;P&gt;First let's make some workable example data steps.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test1;
  a_row+1;
  input code $15.;
datalines;
a
b
c
c
;

data test2;
  b_row+1;
  input (code1-code3) (:$20.);
datalines;
a b c
. b a
c . .
e . k
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now let's make a join using the WHICHC() function. It will return which of the three codes were match (1, 2 or 3) and it will set and 0 if not found.&amp;nbsp; SAS will read 0 as FALSE and any other number as TRUE.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as 
  select *
  from test1 a 
  inner join test2 b
    on whichc(a.code,b.code1,b.code2,b.code3)
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;Obs    a_row    code    b_row    code1    code2    code3

 1       1       a        1        a        b        c
 2       1       a        2                 b        a
 3       2       b        1        a        b        c
 4       2       b        2                 b        a
 5       3       c        1        a        b        c
 6       3       c        3        c
 7       4       c        1        a        b        c
 8       4       c        3        c
&lt;/PRE&gt;</description>
      <pubDate>Thu, 27 Jan 2022 22:24:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Coalesce-for-join-in-proc-sql/m-p/793016#M254115</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-01-27T22:24:36Z</dc:date>
    </item>
    <item>
      <title>Re: Using Coalesce for join in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Coalesce-for-join-in-proc-sql/m-p/793230#M254205</link>
      <description>Hello Tom,&lt;BR /&gt;I think that is. I need to try it first. I need to see how it works on a big data set. Will it increase the number of rows?&lt;BR /&gt;Regards,&lt;BR /&gt;blue &amp;amp; blue</description>
      <pubDate>Fri, 28 Jan 2022 21:24:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Coalesce-for-join-in-proc-sql/m-p/793230#M254205</guid>
      <dc:creator>GN0001</dc:creator>
      <dc:date>2022-01-28T21:24:35Z</dc:date>
    </item>
    <item>
      <title>Re: Using Coalesce for join in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Coalesce-for-join-in-proc-sql/m-p/793233#M254206</link>
      <description>&lt;P&gt;It will potentially blow up the size of the dataset if you use that method.&lt;/P&gt;
&lt;P&gt;Basically it is performing a cartesian product.&amp;nbsp; So if the main dataset has 10,000 observations and there 10 codes you are looking&amp;nbsp; to find in the other dataset then the result is going to have 10 * 10,000 = 100,000 observations.&lt;/P&gt;</description>
      <pubDate>Fri, 28 Jan 2022 22:07:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Coalesce-for-join-in-proc-sql/m-p/793233#M254206</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-01-28T22:07:48Z</dc:date>
    </item>
    <item>
      <title>Re: Using Coalesce for join in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Coalesce-for-join-in-proc-sql/m-p/794326#M254695</link>
      <description>it worked for my case. Although, I need to test the result and compare it to see how it goes.</description>
      <pubDate>Thu, 03 Feb 2022 19:07:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Coalesce-for-join-in-proc-sql/m-p/794326#M254695</guid>
      <dc:creator>GN0001</dc:creator>
      <dc:date>2022-02-03T19:07:37Z</dc:date>
    </item>
    <item>
      <title>Re: Using Coalesce for join in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Coalesce-for-join-in-proc-sql/m-p/794333#M254698</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;Yes, I agree with you, coalesce takes a long time to give me results.&lt;/P&gt;
&lt;P&gt;my first dataset has 50 observations and the other dataset had 5 million rows. It took a long time and the result was so big that I didn't know what to do.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for your help.&lt;/P&gt;
&lt;P&gt;Regards,&lt;/P&gt;
&lt;P&gt;blue &amp;amp; blue&lt;/P&gt;</description>
      <pubDate>Thu, 03 Feb 2022 19:28:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Coalesce-for-join-in-proc-sql/m-p/794333#M254698</guid>
      <dc:creator>GN0001</dc:creator>
      <dc:date>2022-02-03T19:28:27Z</dc:date>
    </item>
    <item>
      <title>Re: Using Coalesce for join in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Coalesce-for-join-in-proc-sql/m-p/794336#M254700</link>
      <description>&lt;P&gt;Hello team,&lt;/P&gt;
&lt;P&gt;can we convert the whichc to&lt;/P&gt;
&lt;P&gt;a couple of joins with OR.&lt;/P&gt;
&lt;P&gt;like on (a.code1=b.code2) or (a.code1=b.code3) or (a.code1=b.code4).&lt;/P&gt;
&lt;P&gt;?&lt;/P&gt;
&lt;P&gt;Please shed some light on.&lt;/P&gt;
&lt;P&gt;Regards,&lt;/P&gt;
&lt;P&gt;blue &amp;amp; blue&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Feb 2022 19:31:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Coalesce-for-join-in-proc-sql/m-p/794336#M254700</guid>
      <dc:creator>GN0001</dc:creator>
      <dc:date>2022-02-03T19:31:05Z</dc:date>
    </item>
    <item>
      <title>Re: Using Coalesce for join in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Coalesce-for-join-in-proc-sql/m-p/794341#M254701</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/202329"&gt;@GN0001&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello team,&lt;/P&gt;
&lt;P&gt;can we convert the whichc to&lt;/P&gt;
&lt;P&gt;a couple of joins with OR.&lt;/P&gt;
&lt;P&gt;like on (a.code1=b.code2) or (a.code1=b.code3) or (a.code1=b.code4).&lt;/P&gt;
&lt;P&gt;?&lt;/P&gt;
&lt;P&gt;Please shed some light on.&lt;/P&gt;
&lt;P&gt;Regards,&lt;/P&gt;
&lt;P&gt;blue &amp;amp; blue&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yes.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The WHICHC() function is much more useful in data step code where you could use a variable list.&lt;/P&gt;
&lt;P&gt;For example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;whichc(code, of code1-code50)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But PROC SQL does not support variable list syntax.&lt;/P&gt;</description>
      <pubDate>Thu, 03 Feb 2022 19:59:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Coalesce-for-join-in-proc-sql/m-p/794341#M254701</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-02-03T19:59:05Z</dc:date>
    </item>
  </channel>
</rss>

