<?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: Using PROC SQL iterating on tables using loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-PROC-SQL-iterating-on-tables-using-loop/m-p/745473#M233671</link>
    <description>I run that every months for the last 4 months &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;Thank you</description>
    <pubDate>Thu, 03 Jun 2021 14:21:02 GMT</pubDate>
    <dc:creator>CC31</dc:creator>
    <dc:date>2021-06-03T14:21:02Z</dc:date>
    <item>
      <title>Using PROC SQL iterating on tables using loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-PROC-SQL-iterating-on-tables-using-loop/m-p/745447#M233653</link>
      <description>&lt;P&gt;Hello everyone, I'm stuck with a PROC SQL that I'd like to do automatically every month.&lt;/P&gt;&lt;P&gt;I have many tables called "table_YYYYMM" in a library "My_Lib" and my PROC is th following:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;LIBNAME My_Lib "path_of_my_library";
PROC SQL;
CREATE TABLE WANT AS
SELECT t1.ID, t2.ID, t3.ID, t4.ID
FROM My_Lib.table_202104 t1
FULL JOIN My_Lib.table_202103 t2 ON (t1.ID = t2.ID)
FULL JOIN My_Lib.table_202102 t3 ON (t1.ID = t3.ID)
FULL JOIN My_Lib.table_202101 t4 ON (t1.ID = t4.ID)

QUIT;

&lt;/PRE&gt;&lt;P&gt;It means that each month I want it to execute it such that every dates that are in the names of tables will change.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do you have any idea? I have tried with some DO loop but I have some problems to change dates in tables' names.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks a lot in advance !&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;CC&lt;/P&gt;</description>
      <pubDate>Thu, 03 Jun 2021 12:11:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-PROC-SQL-iterating-on-tables-using-loop/m-p/745447#M233653</guid>
      <dc:creator>CC31</dc:creator>
      <dc:date>2021-06-03T12:11:32Z</dc:date>
    </item>
    <item>
      <title>Re: Using PROC SQL iterating on tables using loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-PROC-SQL-iterating-on-tables-using-loop/m-p/745468#M233668</link>
      <description>&lt;P&gt;Do you always run that for 4 months, or the whole available year, or all datasets available in the library?&lt;/P&gt;
&lt;P&gt;Your code will throw a WARNING because ID is already present in the output dataset, and the output dataset will contain one variable only.&lt;/P&gt;</description>
      <pubDate>Thu, 03 Jun 2021 14:05:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-PROC-SQL-iterating-on-tables-using-loop/m-p/745468#M233668</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-06-03T14:05:30Z</dc:date>
    </item>
    <item>
      <title>Re: Using PROC SQL iterating on tables using loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-PROC-SQL-iterating-on-tables-using-loop/m-p/745471#M233669</link>
      <description>&lt;P&gt;I run that every 4 months.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yes sorry I realized that I've made a mistake by copying the code it is in fact&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;SELECT t1.ID, t2.ID AS ID_2, t3.ID AS ID_3, t4.ID AS ID_4&lt;/PRE&gt;&lt;P&gt;Sorry about that&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;CC&lt;/P&gt;</description>
      <pubDate>Thu, 03 Jun 2021 14:15:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-PROC-SQL-iterating-on-tables-using-loop/m-p/745471#M233669</guid>
      <dc:creator>CC31</dc:creator>
      <dc:date>2021-06-03T14:15:08Z</dc:date>
    </item>
    <item>
      <title>Re: Using PROC SQL iterating on tables using loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-PROC-SQL-iterating-on-tables-using-loop/m-p/745472#M233670</link>
      <description>&lt;P&gt;Please do also answer my first question.&lt;/P&gt;</description>
      <pubDate>Thu, 03 Jun 2021 14:19:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-PROC-SQL-iterating-on-tables-using-loop/m-p/745472#M233670</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-06-03T14:19:27Z</dc:date>
    </item>
    <item>
      <title>Re: Using PROC SQL iterating on tables using loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-PROC-SQL-iterating-on-tables-using-loop/m-p/745473#M233671</link>
      <description>I run that every months for the last 4 months &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;Thank you</description>
      <pubDate>Thu, 03 Jun 2021 14:21:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-PROC-SQL-iterating-on-tables-using-loop/m-p/745473#M233671</guid>
      <dc:creator>CC31</dc:creator>
      <dc:date>2021-06-03T14:21:02Z</dc:date>
    </item>
    <item>
      <title>Re: Using PROC SQL iterating on tables using loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-PROC-SQL-iterating-on-tables-using-loop/m-p/745474#M233672</link>
      <description>&lt;P&gt;Set the current date and calculate the table names from that.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let now='01MAY2021'd;
CREATE TABLE WANT AS
SELECT t1.ID as id1, t2.ID as id2, t3.ID as id3, t4.ID as id4
FROM My_Lib.table_%sysfunc(intnx(month,&amp;amp;now,-1),yymm6.) t1
FULL JOIN My_Lib.table_%sysfunc(intnx(month,&amp;amp;now,-2),yymm6.) t2 ON (t1.ID = t2.ID)
FULL JOIN My_Lib.table_%sysfunc(intnx(month,&amp;amp;now,-3),yymm6.) t3 ON (t1.ID = t3.ID)
FULL JOIN My_Lib.table_%sysfunc(intnx(month,&amp;amp;now,-4),yymm6.) t4 ON (t1.ID = t4.ID)
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 03 Jun 2021 14:33:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-PROC-SQL-iterating-on-tables-using-loop/m-p/745474#M233672</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-06-03T14:33:46Z</dc:date>
    </item>
    <item>
      <title>Re: Using PROC SQL iterating on tables using loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-PROC-SQL-iterating-on-tables-using-loop/m-p/745477#M233674</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/364432"&gt;@CC31&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;With the code in your question as the basis, I am giving a sample code.&lt;BR /&gt;This should serve your purpose. Modify / adapt as nee&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*Create dates*/
data _null_;
this_mnth=put(today(),yymmn6.);
call symput('THIS_MNTH',this_mnth);
pre1=put(intnx('month',today(),-1,'b'),yymmn6.);
call symput ("PRE1",pre1);
pre2=put(intnx('month',today(),-2,'b'),yymmn6.);
call symput ("PRE2",pre2);
pre3=put(intnx('month',today(),-3,'b'),yymmn6.);
call symput ("PRE3",pre3);
pre4=put(intnx('month',today(),-4,'b'),yymmn6.);
call symput ("PRE4",pre4);
run;

/* Use the dates*/

LIBNAME My_Lib "path_of_my_library";
PROC SQL;
CREATE TABLE WANT AS
SELECT t1.ID, t2.ID AS ID_2, t3.ID AS ID_3, t4.ID AS ID_4
FROM My_Lib.table_&amp;amp;PRE1 t1
FULL JOIN My_Lib.table_&amp;amp;PRE2. t2 ON (t1.ID = t2.ID)
FULL JOIN My_Lib.table_&amp;amp;PRE3. t3 ON (t1.ID = t3.ID)
FULL JOIN My_Lib.table_&amp;amp;PRE4. t4 ON (t1.ID = t4.ID)

QUIT;
/*
if you need the want table to have a date, then replace want with
want_&amp;amp;THIS_MNTH.)
*/&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Jun 2021 14:36:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-PROC-SQL-iterating-on-tables-using-loop/m-p/745477#M233674</guid>
      <dc:creator>Sajid01</dc:creator>
      <dc:date>2021-06-03T14:36:50Z</dc:date>
    </item>
    <item>
      <title>Re: Using PROC SQL iterating on tables using loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-PROC-SQL-iterating-on-tables-using-loop/m-p/745478#M233675</link>
      <description>&lt;P&gt;Thanks a lot Tom !&amp;nbsp;&lt;/P&gt;&lt;P&gt;It helps me a lot, I'm quite new at SAS. Do you think that it would be possible to get it in a loop in order to have a nicer code (because I'm thinking about generalizing it to last 12 months also) ?&lt;/P&gt;</description>
      <pubDate>Thu, 03 Jun 2021 14:37:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-PROC-SQL-iterating-on-tables-using-loop/m-p/745478#M233675</guid>
      <dc:creator>CC31</dc:creator>
      <dc:date>2021-06-03T14:37:11Z</dc:date>
    </item>
    <item>
      <title>Re: Using PROC SQL iterating on tables using loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-PROC-SQL-iterating-on-tables-using-loop/m-p/745480#M233676</link>
      <description>&lt;P&gt;Use a macro with a loop:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let curr=%sysfunc(inputn(202104,yymmn6.));
%macro run_all_four;
oroc sql;
create table want as
  select
    t1.ID as id,
    t2.ID as id_2,
    t3.ID as id_3,
    t4.ID as id_4
  from my_lib.table_%sysfunc(putn(&amp;amp;curr.,yymmn6.)) t1
%do i = 1 %to 3;
  full join my_lib.table_%sysfunc(intnx(month,&amp;amp;curr.,-&amp;amp;i.),yymmn6.) t%eval(&amp;amp;i.+1) on (t1.ID = t%eval(&amp;amp;i.+1).ID)
%end;
;
quit;
%mend;
%run_all_four&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 03 Jun 2021 14:40:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-PROC-SQL-iterating-on-tables-using-loop/m-p/745480#M233676</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-06-03T14:40:13Z</dc:date>
    </item>
    <item>
      <title>Re: Using PROC SQL iterating on tables using loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-PROC-SQL-iterating-on-tables-using-loop/m-p/745483#M233679</link>
      <description>&lt;P&gt;Many thanks KurtBremser &lt;span class="lia-unicode-emoji" title=":grinning_face_with_big_eyes:"&gt;😃&lt;/span&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is exactly what I wanted, and I can see now where were my misstakes. (it was on the %eval function).&lt;/P&gt;&lt;P&gt;Thanks again !&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;CC&lt;/P&gt;</description>
      <pubDate>Thu, 03 Jun 2021 14:45:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-PROC-SQL-iterating-on-tables-using-loop/m-p/745483#M233679</guid>
      <dc:creator>CC31</dc:creator>
      <dc:date>2021-06-03T14:45:54Z</dc:date>
    </item>
  </channel>
</rss>

