<?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: dynamically generate macro variable for SQL Join on LIKE statement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/dynamically-generate-macro-variable-for-SQL-Join-on-LIKE/m-p/522436#M141834</link>
    <description>&lt;P&gt;Certainly this would be simplified if you were to concatenate the two fields.&amp;nbsp; Here is the idea:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;/P&gt;
&lt;P&gt;create table TABLEC as&lt;/P&gt;
&lt;P&gt;select * from TABLEB&lt;/P&gt;
&lt;P&gt;where cats(group, id) in (select cats(group, id) from tableA);&lt;/P&gt;
&lt;P&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I might have the syntax slightly wrong here, but the idea should work with a little tweaking.&lt;/P&gt;</description>
    <pubDate>Wed, 19 Dec 2018 06:11:04 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2018-12-19T06:11:04Z</dc:date>
    <item>
      <title>dynamically generate macro variable for SQL Join on LIKE statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/dynamically-generate-macro-variable-for-SQL-Join-on-LIKE/m-p/522435#M141833</link>
      <description>&lt;P&gt;This is a similar post to my previous one but with modification to use LIKE with a wild card instead (apology to moderators)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;a quick question. i have a table (Table A) that contains two columns looking like this&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 130pt;" border="0" width="174" cellspacing="0" cellpadding="0"&gt;
&lt;TBODY&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD width="87" height="21" style="height: 16.0pt; width: 65pt;"&gt;Group&lt;/TD&gt;
&lt;TD width="87" style="width: 65pt;"&gt;ID&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD height="21" style="height: 16.0pt;"&gt;A&lt;/TD&gt;
&lt;TD align="right"&gt;24&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD height="21" style="height: 16.0pt;"&gt;A&lt;/TD&gt;
&lt;TD align="right"&gt;54&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD height="21" style="height: 16.0pt;"&gt;A&lt;/TD&gt;
&lt;TD align="right"&gt;92&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD height="21" style="height: 16.0pt;"&gt;A&lt;/TD&gt;
&lt;TD align="right"&gt;6&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD height="21" style="height: 16.0pt;"&gt;A&lt;/TD&gt;
&lt;TD align="right"&gt;7&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD height="21" style="height: 16.0pt;"&gt;B&lt;/TD&gt;
&lt;TD align="right"&gt;25&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD height="21" style="height: 16.0pt;"&gt;B&lt;/TD&gt;
&lt;TD align="right"&gt;11&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD height="21" style="height: 16.0pt;"&gt;B&lt;/TD&gt;
&lt;TD align="right"&gt;97&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD height="21" style="height: 16.0pt;"&gt;C&lt;/TD&gt;
&lt;TD align="right"&gt;13&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD height="21" style="height: 16.0pt;"&gt;C&lt;/TD&gt;
&lt;TD align="right"&gt;18&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD height="21" style="height: 16.0pt;"&gt;D&lt;/TD&gt;
&lt;TD align="right"&gt;81&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 16.0pt;"&gt;
&lt;TD height="21" style="height: 16.0pt;"&gt;D&lt;/TD&gt;
&lt;TD align="right"&gt;10&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then I have a very large table (table B) from which i want to extract data based on the group and ID in a proc sql.&lt;/P&gt;
&lt;P&gt;For a number of reasons, I wont be able to use a left join or inner join statement, so I am trying to come up with a different approach that would look like this&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql,
create table TABLEC as
select *
from tableB
where &amp;amp;selection&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;the macro variable &amp;amp;selection should be generated dynamically based on tableA and should at the end look like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;(group=A and (compress(ID,,’p’) like '%24%' or compress(ID,,’p’) like '%54%' or compress(ID,,’p’) like '%92%' or compress(ID,,’p’) like '%6%' or compress(ID,,’p’) like '%7%') ) or&lt;BR /&gt;&lt;BR /&gt;(group=B and (compress(ID,,’p’) like '%25%' or compress(ID,,’p’) like '%11%' or compress(ID,,’p’) like '%97%' )) or&lt;BR /&gt;&lt;BR /&gt;(group=C and (compress(ID,,’p’) like '%13%' or compress(ID,,’p’) like '%18%')) or&lt;BR /&gt;&lt;BR /&gt;(group=D and (compress(ID,,’p’) like '%81%' or compress(ID,,’p’) like '%10%' ))&lt;BR /&gt;&lt;BR /&gt;&lt;/PRE&gt;
&lt;P&gt;The number and name of groups is variable so the code should account for that too. The only thing that wont change is the name of columns (group, ID).&lt;/P&gt;
&lt;P&gt;Has anyone done something similar before?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yes, I know left join makes more sense but as I said, I need to this differently...&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;P&gt;Am&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Dec 2018 05:57:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/dynamically-generate-macro-variable-for-SQL-Join-on-LIKE/m-p/522435#M141833</guid>
      <dc:creator>ammarhm</dc:creator>
      <dc:date>2018-12-19T05:57:37Z</dc:date>
    </item>
    <item>
      <title>Re: dynamically generate macro variable for SQL Join on LIKE statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/dynamically-generate-macro-variable-for-SQL-Join-on-LIKE/m-p/522436#M141834</link>
      <description>&lt;P&gt;Certainly this would be simplified if you were to concatenate the two fields.&amp;nbsp; Here is the idea:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;/P&gt;
&lt;P&gt;create table TABLEC as&lt;/P&gt;
&lt;P&gt;select * from TABLEB&lt;/P&gt;
&lt;P&gt;where cats(group, id) in (select cats(group, id) from tableA);&lt;/P&gt;
&lt;P&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I might have the syntax slightly wrong here, but the idea should work with a little tweaking.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Dec 2018 06:11:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/dynamically-generate-macro-variable-for-SQL-Join-on-LIKE/m-p/522436#M141834</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-12-19T06:11:04Z</dc:date>
    </item>
    <item>
      <title>Re: dynamically generate macro variable for SQL Join on LIKE statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/dynamically-generate-macro-variable-for-SQL-Join-on-LIKE/m-p/522445#M141843</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;This approach will however not work if I am trying to use like %id%,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks anyhow for your time&lt;/P&gt;</description>
      <pubDate>Wed, 19 Dec 2018 07:19:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/dynamically-generate-macro-variable-for-SQL-Join-on-LIKE/m-p/522445#M141843</guid>
      <dc:creator>ammarhm</dc:creator>
      <dc:date>2018-12-19T07:19:34Z</dc:date>
    </item>
    <item>
      <title>Re: dynamically generate macro variable for SQL Join on LIKE statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/dynamically-generate-macro-variable-for-SQL-Join-on-LIKE/m-p/522460#M141850</link>
      <description>&lt;P&gt;You have a really bad setup there then, no getting away from that point.&amp;nbsp; Have to use sql, can't use joins, have to bang this nail into the wall can't use hammers - you see where I am going.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The simplest way to continue this mess is obviously to generate the code from the base table, though don't be suprised if the whole process falls over every run:&lt;/P&gt;
&lt;PRE&gt;data _null_;
  set have end=last;
  if _n_=1 then call execute('proc sql; create table tablec as select * from tableb where ');
  call execute(cats('(group="',group,'" and index(compress(id,,"p")',id,'))'));
  if last then call execute(';quit;');
run;&lt;/PRE&gt;
&lt;P&gt;This will create the proc sql with one group= for each line in the base dataset.&amp;nbsp; Note I use index() rather than like to avoid all the nasty % issues, the effect is the same, as is only doing one id per row rather than all in one row.&lt;/P&gt;
&lt;P&gt;Again, I strongly advise to rethink your whole process including how you got to this mess in the first place.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Dec 2018 09:33:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/dynamically-generate-macro-variable-for-SQL-Join-on-LIKE/m-p/522460#M141850</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-12-19T09:33:46Z</dc:date>
    </item>
    <item>
      <title>Re: dynamically generate macro variable for SQL Join on LIKE statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/dynamically-generate-macro-variable-for-SQL-Join-on-LIKE/m-p/522466#M141854</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;I agree, it is not the best situation to be in, yet again, it also good to be in these situations as you always learn something new. I never thought of using index() so thanks.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Dec 2018 10:16:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/dynamically-generate-macro-variable-for-SQL-Join-on-LIKE/m-p/522466#M141854</guid>
      <dc:creator>ammarhm</dc:creator>
      <dc:date>2018-12-19T10:16:52Z</dc:date>
    </item>
  </channel>
</rss>

