<?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: Macro function to create an &amp;quot;EquiJoin&amp;quot; Condition in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-function-to-create-an-quot-EquiJoin-quot-Condition/m-p/633876#M188083</link>
    <description>&lt;P&gt;it could do something other than a natural join. For instance, if both tables contain a column like "creation_timestmp" then that should not be used in the join criteria. But maybe my knowledge of the natural join is not perfect!&lt;BR /&gt;It was also used inside a another macro that did the checking,.&lt;/P&gt;&lt;P&gt;It could also be used to for a slightly more generic solution to&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-or-DATA-STEP-union-with-list-of-var-in-quot-on-quot/m-p/633279#M77866" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-or-DATA-STEP-union-with-list-of-var-in-quot-on-quot/m-p/633279#M77866&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 21 Mar 2020 20:56:31 GMT</pubDate>
    <dc:creator>DavePrinsloo</dc:creator>
    <dc:date>2020-03-21T20:56:31Z</dc:date>
    <item>
      <title>Macro function to create an "EquiJoin" Condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-function-to-create-an-quot-EquiJoin-quot-Condition/m-p/633871#M188078</link>
      <description>&lt;P&gt;This code presents a macro that generates an &lt;STRONG&gt;EquiJoin&lt;/STRONG&gt; condition.&amp;nbsp; &amp;nbsp; i.e.&amp;nbsp; an SQL Join condition where both tables have one or more columns that must have equal values in the join condition.&amp;nbsp; &amp;nbsp;For Example, if both tables contain the key columns KeyA KeyB and KeyC, then a join&amp;nbsp;&lt;/P&gt;&lt;P&gt;would be something like:&amp;nbsp; &amp;nbsp; t1.KeyA=t2.KeyA and t1.KeyB=t2.KeyB and&amp;nbsp; t1.KeyC=t2.KeyC&amp;nbsp;&lt;/P&gt;&lt;P&gt;The macro is generic but was designed to be used with the macro &lt;A href="https://communities.sas.com/t5/SAS-Programming/Macro-function-to-return-a-list-of-variables-in-a-table-in-a/m-p/633536" target="_blank" rel="noopener"&gt;%ut_varlist&lt;/A&gt;&amp;nbsp;I posted recently.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The full source code&amp;nbsp; is attached, but usage examples are shown here.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I welcome feedback!&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Exampe 1 :&amp;nbsp; Simple call&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let join_cond = %ut_sql_equijoin(in_cols = ABC DEF XYZ
, intab1 = t1
, intab2 = t2) ;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;STRONG&gt;Example 2 : Hardcoded-call embedded in Proc SQL code&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;* First create a couple of temporary tables to use in the macro call ;
data a(keep=abc_id xyz_id aaa) b(keep=abc_id xyz_id bbb) ;
do abc_id = 1 to 10;
   do xyz_id = 4 to 14;
        aaa = abc_id + xyz_id;
        bbb = xyz_id * abc_id;
       output;
    end;  
end;
run;

proc sql noprint;
create table joined
as select a.*, 
b.bbb
from a 
left join 
b
on %ut_sql_equijoin(
in_cols = abc_id xyz_id
, intab1 = a
, intab2 = b)
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;STRONG&gt;Example 3:&amp;nbsp; This example has no&amp;nbsp;knowledge of the columns in the tables other than that the join keys have a suffix "_id".&lt;/STRONG&gt;&lt;BR /&gt;The join selects creates a join on these _Id columns and selects all columns from both tables&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
create table joined
as select  a.*, 
           %ut_varlist(table=b, add_prefix=b., newdlm=comma 
                            , exclude=%ut_varlist(table=a) )
   from a, b
   where %ut_sql_equijoin(
                 in_cols      =  %ut_varlist(table=a, contain=_ID, contain_pos=END, select=%ut_varlist(table=b) )
               , intab1       =  a
               , intab2       =  b)
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The generated code looks like:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
 create table joined as select a.*,
MPRINT(UT_VARLIST):   b.bbb
  from a, b where
MPRINT(UT_SQL_EQUIJOIN):   (a.abc_id = b.abc_id) AND (a.xyz_id = b.xyz_id)
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 21 Mar 2020 20:06:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-function-to-create-an-quot-EquiJoin-quot-Condition/m-p/633871#M188078</guid>
      <dc:creator>DavePrinsloo</dc:creator>
      <dc:date>2020-03-21T20:06:02Z</dc:date>
    </item>
    <item>
      <title>Re: Macro function to create an "EquiJoin" Condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-function-to-create-an-quot-EquiJoin-quot-Condition/m-p/633872#M188079</link>
      <description>&lt;P&gt;Is this supposed to do something different than NATURAL join?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What does the macro do when one or more of the key variables is not present on one or more of the datasets being joined? Does it generate an error message or dose it just generate the SAS code anyway and let SAS generate an error when it tries to compile it?&lt;/P&gt;</description>
      <pubDate>Sat, 21 Mar 2020 20:15:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-function-to-create-an-quot-EquiJoin-quot-Condition/m-p/633872#M188079</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-03-21T20:15:56Z</dc:date>
    </item>
    <item>
      <title>Re: Macro function to create an "EquiJoin" Condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-function-to-create-an-quot-EquiJoin-quot-Condition/m-p/633876#M188083</link>
      <description>&lt;P&gt;it could do something other than a natural join. For instance, if both tables contain a column like "creation_timestmp" then that should not be used in the join criteria. But maybe my knowledge of the natural join is not perfect!&lt;BR /&gt;It was also used inside a another macro that did the checking,.&lt;/P&gt;&lt;P&gt;It could also be used to for a slightly more generic solution to&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-or-DATA-STEP-union-with-list-of-var-in-quot-on-quot/m-p/633279#M77866" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/SAS-Procedures/PROC-SQL-or-DATA-STEP-union-with-list-of-var-in-quot-on-quot/m-p/633279#M77866&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 21 Mar 2020 20:56:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-function-to-create-an-quot-EquiJoin-quot-Condition/m-p/633876#M188083</guid>
      <dc:creator>DavePrinsloo</dc:creator>
      <dc:date>2020-03-21T20:56:31Z</dc:date>
    </item>
  </channel>
</rss>

