<?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: left join multiple tables with macro into 1 output in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/left-join-multiple-tables-with-macro-into-1-output/m-p/347253#M273286</link>
    <description>&lt;P&gt;hello, thank you for your reply.&lt;/P&gt;&lt;P&gt;I managed to rename the fields&amp;nbsp;using the following, but i still have difficulty to figure out the loop over the several left joins..&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%let&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; name=&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%SUBSTR&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;(&amp;amp;item,&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%LENGTH&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;(&amp;amp;item)-1);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 05 Apr 2017 09:02:20 GMT</pubDate>
    <dc:creator>nesslee</dc:creator>
    <dc:date>2017-04-05T09:02:20Z</dc:date>
    <item>
      <title>left join multiple tables with macro into 1 output</title>
      <link>https://communities.sas.com/t5/SAS-Programming/left-join-multiple-tables-with-macro-into-1-output/m-p/347180#M273284</link>
      <description>&lt;P&gt;hello, i am beginner in macros.&lt;/P&gt;&lt;P&gt;I would like to left join multiple tables to another table using a macro. My macro is overwriting instead&amp;nbsp;and I do not know how to prevent that.&lt;/P&gt;&lt;P&gt;&lt;U&gt;so i would like to create a macro which will do this:&lt;/U&gt;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;create table result2 as&lt;/P&gt;&lt;P&gt;select&lt;/P&gt;&lt;P&gt;t1.*,&lt;/P&gt;&lt;P&gt;t_sa_00.code as code_sa_00,&lt;/P&gt;&lt;P&gt;t_sa_01.code as code_sa_01,&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;from work.result1 as t1&lt;/P&gt;&lt;P&gt;left join work.t_sa_00 on t1.id=t_sa_00.id&lt;/P&gt;&lt;P&gt;left join work.t_sa_01 on t1.id=t_sa_01.id&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;and my actual&amp;nbsp;macro looks like this:&lt;/U&gt;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;select distinct (memname), count(distinct memname)&lt;/P&gt;&lt;P&gt;into :tbl separated by ' ' , :ntbl&lt;/P&gt;&lt;P&gt;from dictionary.tables&lt;/P&gt;&lt;P&gt;where&lt;/P&gt;&lt;P&gt;libname='WORK'&lt;/P&gt;&lt;P&gt;and memname like 'T_SA_%'&lt;/P&gt;&lt;P&gt;order by memname;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro result2;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;%DO i=1 %TO &amp;amp;ntbl.;&lt;/P&gt;&lt;P&gt;%LET item = %scan(&amp;amp;tbl., &amp;amp;i.);&lt;/P&gt;&lt;P&gt;CREATE TABLE RESULT2 AS (&lt;/P&gt;&lt;P&gt;SELECT&lt;/P&gt;&lt;P&gt;t1.*,&lt;/P&gt;&lt;P&gt;t2.code_sa&lt;/P&gt;&lt;P&gt;from work.RESULT1 as t1 LEFT JOIN &amp;amp;item. as t2&lt;/P&gt;&lt;P&gt;on t1.id=t2.id&lt;/P&gt;&lt;P&gt;);&lt;/P&gt;&lt;P&gt;%END;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;%mend;&lt;/P&gt;&lt;P&gt;%result2&lt;/P&gt;</description>
      <pubDate>Tue, 04 Apr 2017 20:48:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/left-join-multiple-tables-with-macro-into-1-output/m-p/347180#M273284</guid>
      <dc:creator>nesslee</dc:creator>
      <dc:date>2017-04-04T20:48:05Z</dc:date>
    </item>
    <item>
      <title>Re: left join multiple tables with macro into 1 output</title>
      <link>https://communities.sas.com/t5/SAS-Programming/left-join-multiple-tables-with-macro-into-1-output/m-p/347181#M273285</link>
      <description>&lt;P&gt;it appears that you may want &lt;STRONG&gt;2&lt;/STRONG&gt; loops&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;/P&gt;
&lt;P&gt;create table result2 as&lt;/P&gt;
&lt;P&gt;select&lt;/P&gt;
&lt;P&gt;t1.*,&lt;/P&gt;
&lt;P&gt;/* one loop would go here to create the rename codes*/&lt;/P&gt;
&lt;P&gt;t_sa_00.code as code_sa_00,&lt;/P&gt;
&lt;P&gt;t_sa_01.code as code_sa_01,&lt;/P&gt;
&lt;P&gt;...&lt;/P&gt;
&lt;P&gt;/* and end here for the rename&amp;nbsp; not that the last one shouldn't end with a comma&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; so it may work better to look at generating , t_sa_00.code as code_sa_00 lines*/&lt;/P&gt;
&lt;P&gt;from work.result1 as t1&lt;/P&gt;
&lt;P&gt;/* and the other parallel loop to go over the data set names*/&lt;/P&gt;
&lt;P&gt;left join work.t_sa_00 on t1.id=t_sa_00.id&lt;/P&gt;
&lt;P&gt;left join work.t_sa_01 on t1.id=t_sa_01.id&lt;/P&gt;
&lt;P&gt;...&lt;/P&gt;
&lt;P&gt;/* and the loop ends here*/&lt;/P&gt;
&lt;P&gt;;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You do not want the the Create table or start of the select inside the loop, that is what would overwrite your resulting sets. You should make sure that you have the non-macro coding work for at least 2 sets before attempting to code a macro (3 would be better)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that you can turn on system options Mprint and symbolgen to se what your macro code is actually doing.&lt;/P&gt;
&lt;P&gt;Options mprint symbolgen;&lt;/P&gt;
&lt;P&gt;%yourmacrocall&lt;/P&gt;
&lt;P&gt;options nomprint nosymbolgen.&lt;/P&gt;</description>
      <pubDate>Tue, 04 Apr 2017 21:01:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/left-join-multiple-tables-with-macro-into-1-output/m-p/347181#M273285</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-04-04T21:01:29Z</dc:date>
    </item>
    <item>
      <title>Re: left join multiple tables with macro into 1 output</title>
      <link>https://communities.sas.com/t5/SAS-Programming/left-join-multiple-tables-with-macro-into-1-output/m-p/347253#M273286</link>
      <description>&lt;P&gt;hello, thank you for your reply.&lt;/P&gt;&lt;P&gt;I managed to rename the fields&amp;nbsp;using the following, but i still have difficulty to figure out the loop over the several left joins..&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%let&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; name=&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%SUBSTR&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;(&amp;amp;item,&lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%LENGTH&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;(&amp;amp;item)-1);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Apr 2017 09:02:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/left-join-multiple-tables-with-macro-into-1-output/m-p/347253#M273286</guid>
      <dc:creator>nesslee</dc:creator>
      <dc:date>2017-04-05T09:02:20Z</dc:date>
    </item>
    <item>
      <title>Re: left join multiple tables with macro into 1 output</title>
      <link>https://communities.sas.com/t5/SAS-Programming/left-join-multiple-tables-with-macro-into-1-output/m-p/347333#M273287</link>
      <description>&lt;P&gt;So you want to generate&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table result2 as
select t1.*
/*  One group */
     , t_sa_00.code as code_sa_00
     , t_sa_01.code as code_sa_01
...
from work.result1 as t1
/* Second group */
left join work.t_sa_00 on t1.id=t_sa_00.id
left join work.t_sa_01 on t1.id=t_sa_01.id
...
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So your macro will need two loops.&lt;/P&gt;</description>
      <pubDate>Wed, 05 Apr 2017 12:58:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/left-join-multiple-tables-with-macro-into-1-output/m-p/347333#M273287</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-04-05T12:58:28Z</dc:date>
    </item>
  </channel>
</rss>

