<?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 for joining multiple tables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-joining-multiple-tables/m-p/372216#M275980</link>
    <description>&lt;P&gt;Suggestions:&lt;/P&gt;
&lt;P&gt;If this data is to be used for awhile I would suggest moving it to a library other than work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use the EXIST function to test if a set exists wherever it is used such as this example for the last one:&lt;/P&gt;
&lt;PRE&gt;proc sql;
create table mun&amp;amp;year0. as
select t1.id,
       t1.mun as mun&amp;amp;year0.,
       t2.mun as mun&amp;amp;year1.,
       t3.mun as mun&amp;amp;year2.,
       t4.mun as mun&amp;amp;year3.,
%if %sysfunc(exist(pop&amp;amp;year4.))%then %do;
       t5.mun as mun&amp;amp;year4.,
%end;
       &amp;amp;year0. as year
from pop&amp;amp;year0. as t1 left join pop&amp;amp;year1. as t2 on t1.id = t2.id
left join pop&amp;amp;year2. as t3 on t1.id = t3.id
left join pop&amp;amp;year3. as t4 on t1.id = t4.id
%if %sysfunc(exist(pop&amp;amp;year4.))%then %do;
   left join pop&amp;amp;year4. as t5 on t1.id = t5.id
%end;
order by id;
 &lt;/PRE&gt;
&lt;P&gt;Note that the %if can only be used inside a macro such as you using.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And shouldn't that Eval be using &amp;amp;start instead of year0???&lt;/P&gt;</description>
    <pubDate>Fri, 30 Jun 2017 15:24:02 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2017-06-30T15:24:02Z</dc:date>
    <item>
      <title>Macro for joining multiple tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-joining-multiple-tables/m-p/372066#M275979</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have some population data from 2007 to 2017 (one table for each year)&amp;nbsp;and I'm trying to create a macro that creates a table for&amp;nbsp;each year with an id, muncipality&amp;nbsp;of residence and muncipality of residense for each of the following four years and merge them together in one table. Here is my code (I'm using SAS 9.4 if that makes any difference):&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro sqlloop(start, end);&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;%do year0=&amp;amp;start. %to end;&lt;/P&gt;&lt;P&gt;%let year1=%eval(year0+1);&lt;/P&gt;&lt;P&gt;%let year2=%eval(year0+2);&lt;/P&gt;&lt;P&gt;%let year3=%eval(year0+3);&lt;/P&gt;&lt;P&gt;%let year4=%eval(year0+4);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;create table mun&amp;amp;year0. as&lt;/P&gt;&lt;P&gt;select t1.id,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; t1.mun as mun&amp;amp;year0.,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;t2.mun as mun&amp;amp;year1.,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; t3.mun as mun&amp;amp;year2.,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; t4.mun as mun&amp;amp;year3.,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;t5.mun as mun&amp;amp;year4.,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;amp;year0. as year&lt;/P&gt;&lt;P&gt;from pop&amp;amp;year0. as t1 left join pop&amp;amp;year1. as t2 on t1.id = t2.id&lt;/P&gt;&lt;P&gt;left join pop&amp;amp;year2. as t3 on t1.id = t3.id&lt;/P&gt;&lt;P&gt;left join pop&amp;amp;year3. as t4 on t1.id = t4.id&lt;/P&gt;&lt;P&gt;left join pop&amp;amp;year4. as t5 on t1.id = t5.id&lt;/P&gt;&lt;P&gt;order by id;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&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;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%sqlloop(start=2007, end=2017)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data mun07_2017;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set mun2007 mun2008 mun2009 mun2010 mun2011 mun2012 mun2013 mun2014 mun2015 mun2016 mun2017;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The problem that I have is that the macro stops after 2013, because it can't find a table for 2018 (residence 4 years after), which of course doesn't exist. Therefore, I want a condition that sets residence as missing if the corresponding table doesn't exist. I have already tried multiple solutions, but nothing have worked for me so far.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope you can help!&lt;/P&gt;</description>
      <pubDate>Fri, 30 Jun 2017 10:02:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-joining-multiple-tables/m-p/372066#M275979</guid>
      <dc:creator>chhl</dc:creator>
      <dc:date>2017-06-30T10:02:19Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for joining multiple tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-joining-multiple-tables/m-p/372216#M275980</link>
      <description>&lt;P&gt;Suggestions:&lt;/P&gt;
&lt;P&gt;If this data is to be used for awhile I would suggest moving it to a library other than work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use the EXIST function to test if a set exists wherever it is used such as this example for the last one:&lt;/P&gt;
&lt;PRE&gt;proc sql;
create table mun&amp;amp;year0. as
select t1.id,
       t1.mun as mun&amp;amp;year0.,
       t2.mun as mun&amp;amp;year1.,
       t3.mun as mun&amp;amp;year2.,
       t4.mun as mun&amp;amp;year3.,
%if %sysfunc(exist(pop&amp;amp;year4.))%then %do;
       t5.mun as mun&amp;amp;year4.,
%end;
       &amp;amp;year0. as year
from pop&amp;amp;year0. as t1 left join pop&amp;amp;year1. as t2 on t1.id = t2.id
left join pop&amp;amp;year2. as t3 on t1.id = t3.id
left join pop&amp;amp;year3. as t4 on t1.id = t4.id
%if %sysfunc(exist(pop&amp;amp;year4.))%then %do;
   left join pop&amp;amp;year4. as t5 on t1.id = t5.id
%end;
order by id;
 &lt;/PRE&gt;
&lt;P&gt;Note that the %if can only be used inside a macro such as you using.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And shouldn't that Eval be using &amp;amp;start instead of year0???&lt;/P&gt;</description>
      <pubDate>Fri, 30 Jun 2017 15:24:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-joining-multiple-tables/m-p/372216#M275980</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-06-30T15:24:02Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for joining multiple tables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-joining-multiple-tables/m-p/372672#M275981</link>
      <description>&lt;P&gt;Thank you very much!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Worked just as I wanted.&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jul 2017 08:16:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-joining-multiple-tables/m-p/372672#M275981</guid>
      <dc:creator>chhl</dc:creator>
      <dc:date>2017-07-03T08:16:36Z</dc:date>
    </item>
  </channel>
</rss>

