<?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: can I make my query dynamically ignore join codes? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/can-I-make-my-query-dynamically-ignore-join-codes/m-p/462325#M117697</link>
    <description>&lt;P&gt;If the key variables are the only variables they have in common the consider using NATURAL joins.&amp;nbsp; SAS will automatically join based on variables with the same names.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;
create table want as
select *
from b2 
natural join b1
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 15 May 2018 12:32:12 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2018-05-15T12:32:12Z</dc:date>
    <item>
      <title>can I make my query dynamically ignore join codes?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/can-I-make-my-query-dynamically-ignore-join-codes/m-p/462049#M117556</link>
      <description>&lt;P&gt;I have to upload various tables(xlsx files) and join them together via a query.&amp;nbsp; I,e upload table A1, and join it with A2, A3.....etc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can I make it such that my query is dynamic enough to ignore join statements if my table does not have certain columns.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example,&amp;nbsp; uploading table A1, A2, A3,&amp;nbsp; I might join them with primary Key "ID" and 'name'.&amp;nbsp; However, if I upload table B1, B2, B3,&amp;nbsp; it might not have Primary Key "name."&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So I want to have a query that has all the join staements&amp;nbsp; for name and ID, but be able to ignore the 'name' join if these columns aren't available.&lt;/P&gt;</description>
      <pubDate>Mon, 14 May 2018 14:03:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/can-I-make-my-query-dynamically-ignore-join-codes/m-p/462049#M117556</guid>
      <dc:creator>mrdlau</dc:creator>
      <dc:date>2018-05-14T14:03:26Z</dc:date>
    </item>
    <item>
      <title>Re: can I make my query dynamically ignore join codes?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/can-I-make-my-query-dynamically-ignore-join-codes/m-p/462054#M117557</link>
      <description>&lt;P&gt;Whilst there might be, after some major code work, a way of doing something similar to what you want the real question is why you don't know your data.&amp;nbsp; Programming is 95% data and documentation, with a small amount of coding at the end.&amp;nbsp; Writing code based off data you don't know and trying to generalise things will just cause you far more work.&amp;nbsp; Know the data, know the linking variables - this is the data model and fundamental to any programming endeavor.&lt;/P&gt;</description>
      <pubDate>Mon, 14 May 2018 14:14:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/can-I-make-my-query-dynamically-ignore-join-codes/m-p/462054#M117557</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-05-14T14:14:24Z</dc:date>
    </item>
    <item>
      <title>Re: can I make my query dynamically ignore join codes?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/can-I-make-my-query-dynamically-ignore-join-codes/m-p/462055#M117558</link>
      <description>&lt;P&gt;Sure.&amp;nbsp; Use dictionary.columns to check to see if the field(s) exist and then use macro code (%if) to bypass joins that wouldn't work (make the whole thing a macro to use things like %if).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;something like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PROC SQL noprint ;&lt;BR /&gt;&amp;nbsp; SELECT *&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; FROM DICTIONARY.COLUMNS&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; WHERE LIBNAME='5XX' and memname='B1' and name='name'&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ; &lt;BR /&gt;QUIT;&lt;BR /&gt;%if &amp;amp;sqlobs&amp;gt;0 %then... (do your join by name)&lt;/P&gt;</description>
      <pubDate>Mon, 14 May 2018 14:23:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/can-I-make-my-query-dynamically-ignore-join-codes/m-p/462055#M117558</guid>
      <dc:creator>tomrvincent</dc:creator>
      <dc:date>2018-05-14T14:23:47Z</dc:date>
    </item>
    <item>
      <title>Re: can I make my query dynamically ignore join codes?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/can-I-make-my-query-dynamically-ignore-join-codes/m-p/462084#M117570</link>
      <description>Yeah dictionary or a proc contents out=. Maybe with a select into:.</description>
      <pubDate>Mon, 14 May 2018 15:20:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/can-I-make-my-query-dynamically-ignore-join-codes/m-p/462084#M117570</guid>
      <dc:creator>ProcWes</dc:creator>
      <dc:date>2018-05-14T15:20:56Z</dc:date>
    </item>
    <item>
      <title>Re: can I make my query dynamically ignore join codes?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/can-I-make-my-query-dynamically-ignore-join-codes/m-p/462087#M117573</link>
      <description>&lt;P&gt;I considered that, but I didn't see anything that needed to be saved...just the existence of the field itself is all that's needed.&lt;/P&gt;</description>
      <pubDate>Mon, 14 May 2018 15:23:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/can-I-make-my-query-dynamically-ignore-join-codes/m-p/462087#M117573</guid>
      <dc:creator>tomrvincent</dc:creator>
      <dc:date>2018-05-14T15:23:28Z</dc:date>
    </item>
    <item>
      <title>Re: can I make my query dynamically ignore join codes?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/can-I-make-my-query-dynamically-ignore-join-codes/m-p/462098#M117579</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/38339"&gt;@mrdlau&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So I want to have a query that has all the join staements&amp;nbsp; for name and ID, but be able to ignore the 'name' join if these columns aren't available.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Would those tables still be joined with ID or ignored entirely?&lt;/P&gt;</description>
      <pubDate>Mon, 14 May 2018 15:40:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/can-I-make-my-query-dynamically-ignore-join-codes/m-p/462098#M117579</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-05-14T15:40:57Z</dc:date>
    </item>
    <item>
      <title>Re: can I make my query dynamically ignore join codes?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/can-I-make-my-query-dynamically-ignore-join-codes/m-p/462147#M117604</link>
      <description>These tables would still need to be joined by ID,  so I would just want the ‘name’ join to be ignored,  since that table doesn’t have a name column.</description>
      <pubDate>Mon, 14 May 2018 17:39:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/can-I-make-my-query-dynamically-ignore-join-codes/m-p/462147#M117604</guid>
      <dc:creator>mrdlau</dc:creator>
      <dc:date>2018-05-14T17:39:25Z</dc:date>
    </item>
    <item>
      <title>Re: can I make my query dynamically ignore join codes?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/can-I-make-my-query-dynamically-ignore-join-codes/m-p/462255#M117655</link>
      <description>&lt;P&gt;Thank you.&amp;nbsp; Im' still new to SAS and macros, and still need help understanding.&amp;nbsp; Would appreciate some assistance if possible.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's&amp;nbsp;an example full code that I have.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select *
from 
work.table1 t1
left join work.table2 t2 on t1.ID = t2.ID and t1.name = t2.name;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In my tables, t2 does not have a 'name' column, so running the query like this, it'll error out.&amp;nbsp; I want my query to be able to ignore the name joins so it'll run as if there wasn't a name join&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I understand the first part of you code where it's filtering the dictionary columns to the 'Name' column.&amp;nbsp; However, I dont understand the rest of it.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select * from dictionary.columns
where LIBNAME = 'WORK'
AND memname = 'TABLE2'
AND name = 'Name';
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is &amp;amp;sqlobs a predefined macro?&amp;nbsp; and is this already&amp;nbsp;referencing the above script?&amp;nbsp; I'm actually still very new to sas and macros, so I'm still unsure how to apply what you gave me to my code above.&amp;nbsp; my extent to macros at the moment is %let = variable, and then&amp;nbsp;referencing it throughout the code&lt;/P&gt;</description>
      <pubDate>Tue, 15 May 2018 04:33:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/can-I-make-my-query-dynamically-ignore-join-codes/m-p/462255#M117655</guid>
      <dc:creator>mrdlau</dc:creator>
      <dc:date>2018-05-15T04:33:47Z</dc:date>
    </item>
    <item>
      <title>Re: can I make my query dynamically ignore join codes?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/can-I-make-my-query-dynamically-ignore-join-codes/m-p/462291#M117682</link>
      <description>&lt;P&gt;Here is an example:&lt;/P&gt;
&lt;PRE&gt;data a1;
  id=1; var1=123; output;
  id=2; var1=345; output;
run;
data a2;
  id=1; name="abc"; var2=12; output;
  id=3; name="def"; var2=90; output;
run;
data a3;
  id=3; name="tmp"; var3=20; output;
  id=4; name="rtt"; var3=34; output;
run;

%macro Join_ID (t1=,t2=,use_name=N,tresult=);
  proc sql;
    create table &amp;amp;tresult. as
    select *
    from   &amp;amp;t1. t1
    left join &amp;amp;t2. t2
    on     t1.id=t2.id
    %if &amp;amp;use_name.=Y %then %do;
      and t1.name=t2.name
    %end;
    ;
  quit;
%mend Join_ID;

%Join_ID (t1=a1,t2=a2,tresult=first);
%Join_ID (t1=first,t2=a3,use_name=Y,tresult=second);&lt;/PRE&gt;
&lt;P&gt;You can switch the call of Join_ID based on what is in sashelp.vcolumn.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, once again, I strongly recommend you don't go down that route as you will have problems, not just logical ones, but inaccurate joins and such like.&amp;nbsp; Know your data, program to your data, this is the way to write robust clean code which can repeatedly produce the same accurate result.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 May 2018 07:57:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/can-I-make-my-query-dynamically-ignore-join-codes/m-p/462291#M117682</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-05-15T07:57:32Z</dc:date>
    </item>
    <item>
      <title>Re: can I make my query dynamically ignore join codes?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/can-I-make-my-query-dynamically-ignore-join-codes/m-p/462325#M117697</link>
      <description>&lt;P&gt;If the key variables are the only variables they have in common the consider using NATURAL joins.&amp;nbsp; SAS will automatically join based on variables with the same names.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;
create table want as
select *
from b2 
natural join b1
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 15 May 2018 12:32:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/can-I-make-my-query-dynamically-ignore-join-codes/m-p/462325#M117697</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-05-15T12:32:12Z</dc:date>
    </item>
  </channel>
</rss>

