<?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: SAS Proc SQL Macro Efficiency/Automation Involving Teradata in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-Proc-SQL-Macro-Efficiency-Automation-Involving-Teradata/m-p/592802#M170009</link>
    <description>&lt;P&gt;Why are you running 12 separate queries to get a year's-worth of data? It would be likely more efficient to do this in one query.&lt;/P&gt;</description>
    <pubDate>Mon, 30 Sep 2019 19:26:44 GMT</pubDate>
    <dc:creator>SASKiwi</dc:creator>
    <dc:date>2019-09-30T19:26:44Z</dc:date>
    <item>
      <title>SAS Proc SQL Macro Efficiency/Automation Involving Teradata</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Proc-SQL-Macro-Efficiency-Automation-Involving-Teradata/m-p/592784#M169999</link>
      <description>&lt;P&gt;Greetings,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've developed a macro to help produce data sets pulling from different tables and date ranges, however I believe I'm not doing this in the most efficient way. Btw I'm working with Teradata so it is passing through Teradata. Here is my code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test_data;
infile datalines;
format cust_id 3. depo_dt yymmddd10. deposit_amt dollar6.2 year_month source;
input cust_id 3. depo_dt :yymmdd10. deposit_amt dollar6.2 year_month source $16.;
datalines;
871 2019-02-21 $20.00 201902 USBANK
888 2019-05-13 $90.00 201905 WELLSFARGO
847 2019-03-25 $60.00 201903 CHASE
821 2019-09-18 $30.00 201909 CITIGROUP
;;
run;


%macro td_sql_macro (tbl_nme,yyyymm,mnth_1,mnth2);
proc sql noerrorstop;
connect to teradata (database=BANK_DATA user="user" password="pw" tdpid="tdpid" mode=teradata);
create table work.&amp;amp;tbl_nme as select * from connection to teradata (
  select cust_id,
         depo_dt,
	     deposit_amt,
	     year_month,
	     source
  from BANK_DATA.source_data_&amp;amp;yyyymm._hist
  where depo_dt &amp;gt;= date &amp;amp;mnth_1
  and depo_dt &amp;lt; date &amp;amp;mnth2
  and prc_dt = &amp;amp;yyyymm
  and cust_id &amp;lt;&amp;gt; 0);
disconnect from teradata;
quit;
%mend;

%td_sql_macro(tbl_nme=bank_data_201809,yyyymm=201809,mnth_1='2018-09-01',mnth2='2018-10-01')
%td_sql_macro(tbl_nme=bank_data_201810,yyyymm=201810,mnth_1='2018-10-01',mnth2='2018-11-01');
%td_sql_macro(tbl_nme=bank_data_201811,yyyymm=201811,mnth_1='2018-11-01',mnth2='2018-12-01');
%td_sql_macro(tbl_nme=bank_data_201812,yyyymm=201812,mnth_1='2018-12-01',mnth2='2019-01-01');
%td_sql_macro(tbl_nme=bank_data_201901,yyyymm=201901,mnth_1='2019-01-01',mnth2='2019-02-01');
%td_sql_macro(tbl_nme=bank_data_201902,yyyymm=201902,mnth_1='2019-02-01',mnth2='2019-03-01');
%td_sql_macro(tbl_nme=bank_data_201903,yyyymm=201903,mnth_1='2019-03-01',mnth2='2019-04-01');
%td_sql_macro(tbl_nme=bank_data_201904,yyyymm=201904,mnth_1='2019-04-01',mnth2='2019-05-01');
%td_sql_macro(tbl_nme=bank_data_201905,yyyymm=201905,mnth_1='2019-05-01',mnth2='2019-06-01');
%td_sql_macro(tbl_nme=bank_data_201906,yyyymm=201906,mnth_1='2019-06-01',mnth2='2019-07-01');
%td_sql_macro(tbl_nme=bank_data_201907,yyyymm=201907,mnth_1='2019-07-01',mnth2='2019-08-01');
%td_sql_macro(tbl_nme=bank_data_201908,yyyymm=201908,mnth_1='2019-08-01',mnth2='2019-09-01');

libname BNK_DPST teradata tdpid="tdpid" database=bnk user="user" password="pw" bulkload=yes;

data BNK_DPST.deposits_all;
  set bank_data_201908
      bank_data_201907
	  bank_data_201906
	  bank_data_201905
	  bank_data_201904
	  bank_data_201903
	  bank_data_201902
	  bank_data_201901
      bank_data_201812
      bank_data_201811
	  bank_data_201810
	  bank_data_201809;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The beginning data step is just to see what the data is like, just pretend it is from one of the BANK_DATA.source_data_ tables. So this code works, however I want to make it more efficient/automated in the sense that instead of creating lines calling the macro with different values, I want to automate the macro to just loop through the different values, rather than hard-coding them. Looping through the different tables as well as date filters. The ultimate goal as you can see on the bottom is to combine all the created tables and then create the table in Teradata. I want the data to be a 12-month lookback period. Starting from todays month - 1. So if I was running it for September, I would want the date loops to start at August, going back 12 months.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anyone help? See opportunities for improvement on how I can automatically loop through the different values without hard-coding table names and dates.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Mon, 30 Sep 2019 18:18:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Proc-SQL-Macro-Efficiency-Automation-Involving-Teradata/m-p/592784#M169999</guid>
      <dc:creator>Time_Looper47</dc:creator>
      <dc:date>2019-09-30T18:18:56Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Proc SQL Macro Efficiency/Automation Involving Teradata</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Proc-SQL-Macro-Efficiency-Automation-Involving-Teradata/m-p/592797#M170004</link>
      <description>Any indeces in BANK_DATA.source_data_&amp;amp;yyyymm._hist?</description>
      <pubDate>Mon, 30 Sep 2019 19:19:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Proc-SQL-Macro-Efficiency-Automation-Involving-Teradata/m-p/592797#M170004</guid>
      <dc:creator>tomrvincent</dc:creator>
      <dc:date>2019-09-30T19:19:47Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Proc SQL Macro Efficiency/Automation Involving Teradata</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Proc-SQL-Macro-Efficiency-Automation-Involving-Teradata/m-p/592798#M170005</link>
      <description>&lt;P&gt;Examples of common macro usage&lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The macro appendix has an example of looping through dates. My question would be do you actually need to loop? Can you not just do one pass to filter the entire period at once?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would add a PROC APPEND at the bottom of the macro and remove intermediary data sets as well, this way your data is stacked automatically rather than hard coding any dates.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use PROC DATASETS to drop tables.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/293046"&gt;@Time_Looper47&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Greetings,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've developed a macro to help produce data sets pulling from different tables and date ranges, however I believe I'm not doing this in the most efficient way. Btw I'm working with Teradata so it is passing through Teradata. Here is my code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test_data;
infile datalines;
format cust_id 3. depo_dt yymmddd10. deposit_amt dollar6.2 year_month source;
input cust_id 3. depo_dt :yymmdd10. deposit_amt dollar6.2 year_month source $16.;
datalines;
871 2019-02-21 $20.00 201902 USBANK
888 2019-05-13 $90.00 201905 WELLSFARGO
847 2019-03-25 $60.00 201903 CHASE
821 2019-09-18 $30.00 201909 CITIGROUP
;;
run;


%macro td_sql_macro (tbl_nme,yyyymm,mnth_1,mnth2);
proc sql noerrorstop;
connect to teradata (database=BANK_DATA user="user" password="pw" tdpid="tdpid" mode=teradata);
create table work.&amp;amp;tbl_nme as select * from connection to teradata (
  select cust_id,
         depo_dt,
	     deposit_amt,
	     year_month,
	     source
  from BANK_DATA.source_data_&amp;amp;yyyymm._hist
  where depo_dt &amp;gt;= date &amp;amp;mnth_1
  and depo_dt &amp;lt; date &amp;amp;mnth2
  and prc_dt = &amp;amp;yyyymm
  and cust_id &amp;lt;&amp;gt; 0);
disconnect from teradata;
quit;
%mend;

%td_sql_macro(tbl_nme=bank_data_201809,yyyymm=201809,mnth_1='2018-09-01',mnth2='2018-10-01')
%td_sql_macro(tbl_nme=bank_data_201810,yyyymm=201810,mnth_1='2018-10-01',mnth2='2018-11-01');
%td_sql_macro(tbl_nme=bank_data_201811,yyyymm=201811,mnth_1='2018-11-01',mnth2='2018-12-01');
%td_sql_macro(tbl_nme=bank_data_201812,yyyymm=201812,mnth_1='2018-12-01',mnth2='2019-01-01');
%td_sql_macro(tbl_nme=bank_data_201901,yyyymm=201901,mnth_1='2019-01-01',mnth2='2019-02-01');
%td_sql_macro(tbl_nme=bank_data_201902,yyyymm=201902,mnth_1='2019-02-01',mnth2='2019-03-01');
%td_sql_macro(tbl_nme=bank_data_201903,yyyymm=201903,mnth_1='2019-03-01',mnth2='2019-04-01');
%td_sql_macro(tbl_nme=bank_data_201904,yyyymm=201904,mnth_1='2019-04-01',mnth2='2019-05-01');
%td_sql_macro(tbl_nme=bank_data_201905,yyyymm=201905,mnth_1='2019-05-01',mnth2='2019-06-01');
%td_sql_macro(tbl_nme=bank_data_201906,yyyymm=201906,mnth_1='2019-06-01',mnth2='2019-07-01');
%td_sql_macro(tbl_nme=bank_data_201907,yyyymm=201907,mnth_1='2019-07-01',mnth2='2019-08-01');
%td_sql_macro(tbl_nme=bank_data_201908,yyyymm=201908,mnth_1='2019-08-01',mnth2='2019-09-01');

libname BNK_DPST teradata tdpid="tdpid" database=bnk user="user" password="pw" bulkload=yes;

data BNK_DPST.deposits_all;
  set bank_data_201908
      bank_data_201907
	  bank_data_201906
	  bank_data_201905
	  bank_data_201904
	  bank_data_201903
	  bank_data_201902
	  bank_data_201901
      bank_data_201812
      bank_data_201811
	  bank_data_201810
	  bank_data_201809;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The beginning data step is just to see what the data is like, just pretend it is from one of the BANK_DATA.source_data_ tables. So this code works, however I want to make it more efficient/automated in the sense that instead of creating lines calling the macro with different values, I want to automate the macro to just loop through the different values, rather than hard-coding them. Looping through the different tables as well as date filters. The ultimate goal as you can see on the bottom is to combine all the created tables and then create the table in Teradata. I want the data to be a 12-month lookback period. Starting from todays month - 1. So if I was running it for September, I would want the date loops to start at August, going back 12 months.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can anyone help? See opportunities for improvement on how I can automatically loop through the different values without hard-coding table names and dates.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Sep 2019 19:22:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Proc-SQL-Macro-Efficiency-Automation-Involving-Teradata/m-p/592798#M170005</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-09-30T19:22:37Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Proc SQL Macro Efficiency/Automation Involving Teradata</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Proc-SQL-Macro-Efficiency-Automation-Involving-Teradata/m-p/592802#M170009</link>
      <description>&lt;P&gt;Why are you running 12 separate queries to get a year's-worth of data? It would be likely more efficient to do this in one query.&lt;/P&gt;</description>
      <pubDate>Mon, 30 Sep 2019 19:26:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Proc-SQL-Macro-Efficiency-Automation-Involving-Teradata/m-p/592802#M170009</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2019-09-30T19:26:44Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Proc SQL Macro Efficiency/Automation Involving Teradata</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Proc-SQL-Macro-Efficiency-Automation-Involving-Teradata/m-p/592805#M170012</link>
      <description>&lt;P&gt;Yes, the cust_id&lt;/P&gt;</description>
      <pubDate>Mon, 30 Sep 2019 19:30:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Proc-SQL-Macro-Efficiency-Automation-Involving-Teradata/m-p/592805#M170012</guid>
      <dc:creator>Time_Looper47</dc:creator>
      <dc:date>2019-09-30T19:30:07Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Proc SQL Macro Efficiency/Automation Involving Teradata</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Proc-SQL-Macro-Efficiency-Automation-Involving-Teradata/m-p/592806#M170013</link>
      <description>&lt;P&gt;Because - look at the from:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;from BANK_DATA.source_data_&amp;amp;yyyymm._hist&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;it changes so I can't just do it all in one. I pull from source_data_201908_hist with the August date range in the WHERE. Then pull from souce_data_201907_hist with the July date range in the WHERE. An so on and so forth.&lt;/P&gt;</description>
      <pubDate>Mon, 30 Sep 2019 19:32:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Proc-SQL-Macro-Efficiency-Automation-Involving-Teradata/m-p/592806#M170013</guid>
      <dc:creator>Time_Looper47</dc:creator>
      <dc:date>2019-09-30T19:32:22Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Proc SQL Macro Efficiency/Automation Involving Teradata</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Proc-SQL-Macro-Efficiency-Automation-Involving-Teradata/m-p/592809#M170014</link>
      <description>&lt;P&gt;Thank you for the resource, I think this will be very helpful. The reason I can't do it with one date range is because the source tables are broken out by month - look at the FROM. The table changes.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So&amp;nbsp;the query will run&amp;nbsp;for BANK_DATA.source_data_201908_hist with August in the WHERE filter.&lt;/P&gt;&lt;P&gt;Then BANK_DATA.source_data_201907_hist with the July filter in the WHERE.&lt;/P&gt;&lt;P&gt;Then BANK_DATA.source_data_201906_hist with the June filter in the WHERE. And so on.&lt;/P&gt;</description>
      <pubDate>Mon, 30 Sep 2019 19:38:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Proc-SQL-Macro-Efficiency-Automation-Involving-Teradata/m-p/592809#M170014</guid>
      <dc:creator>Time_Looper47</dc:creator>
      <dc:date>2019-09-30T19:38:32Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Proc SQL Macro Efficiency/Automation Involving Teradata</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Proc-SQL-Macro-Efficiency-Automation-Involving-Teradata/m-p/592813#M170016</link>
      <description>I can't speak for everywhere, but in most places there is a table with a year or two of history in addition to the monthly tables. It may be worth asking your DB. If you're certain that's the limit then using a data step to loop is an option.</description>
      <pubDate>Mon, 30 Sep 2019 19:57:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Proc-SQL-Macro-Efficiency-Automation-Involving-Teradata/m-p/592813#M170016</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-09-30T19:57:47Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Proc SQL Macro Efficiency/Automation Involving Teradata</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Proc-SQL-Macro-Efficiency-Automation-Involving-Teradata/m-p/592821#M170020</link>
      <description>&lt;P&gt;In that case you could do a UNION of the 12 tables in Teradata and probably still be more efficient than doing one at a time.&lt;/P&gt;</description>
      <pubDate>Mon, 30 Sep 2019 20:18:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Proc-SQL-Macro-Efficiency-Automation-Involving-Teradata/m-p/592821#M170020</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2019-09-30T20:18:01Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Proc SQL Macro Efficiency/Automation Involving Teradata</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Proc-SQL-Macro-Efficiency-Automation-Involving-Teradata/m-p/592826#M170024</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro td_sql_macro (tbl_nme,yyyymm,mnth_1,mnth2);
&lt;FONT size="3" color="#800080"&gt;&lt;STRONG&gt; *Change to have your date start and end or date start and number of intervals;&lt;/STRONG&gt;&lt;/FONT&gt;

&lt;FONT size="4" color="#FF6600"&gt;&lt;STRONG&gt;[insert do loop for dates here according to macro link]&lt;/STRONG&gt;&lt;/FONT&gt;

proc sql noerrorstop;
connect to teradata (database=BANK_DATA user="user" password="pw" tdpid="tdpid" mode=teradata);
create table work.&amp;amp;tbl_nme as select * from connection to teradata (
  select cust_id,
         depo_dt,
	     deposit_amt,
	     year_month,
	     source
  from BANK_DATA.source_data_&amp;amp;yyyymm._hist
  where depo_dt &amp;gt;= date &amp;amp;mnth_1
  and depo_dt &amp;lt; date &amp;amp;mnth2
  and prc_dt = &amp;amp;yyyymm
  and cust_id &amp;lt;&amp;gt; 0);
disconnect from teradata;
quit;

*Append to main data table;
proc append base = final_data data=work.&amp;amp;tbl_nme force;
run;

*delete temporary data set;
proc datasets lib=work nodetails nolist;
delete work.&amp;amp;tbl_nme;
run;quit;

&lt;FONT size="4" color="#FF6600"&gt;&lt;STRONG&gt;[END DO LOOP HERE]&lt;/STRONG&gt;&lt;/FONT&gt;


%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;My thoughts on how I would do this. There are other ways of course.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One would be to have it driven by a data null step + CALL EXECUTE instead.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's my full references for Macros that may be helpful:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;UCLA introductory tutorial on macro variables and macros&lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/" target="_blank" rel="noopener"&gt;https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;Tutorial on converting a working program to a macro&lt;/STRONG&gt;&lt;BR /&gt;&lt;BR /&gt;This method is pretty robust and helps prevent errors and makes it much easier to debug your code. Obviously biased, because I wrote it &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; &lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md" target="_blank" rel="noopener"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;Examples of common macro usage&lt;/STRONG&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Sep 2019 20:38:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Proc-SQL-Macro-Efficiency-Automation-Involving-Teradata/m-p/592826#M170024</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-09-30T20:38:39Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Proc SQL Macro Efficiency/Automation Involving Teradata</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Proc-SQL-Macro-Efficiency-Automation-Involving-Teradata/m-p/595491#M171324</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've made things a bit simpler for the purpose of experimentation and so you can see better what I'm trying to do, however I am stuck as to how to dynamically move through macro variable values - I need to loop through the dates as well as table name and year/month. See the following simplified code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data source_data_201907_hist;
infile datalines;
format cust_id 3. depo_dt yymmddd10. deposit_amt dollar6.2 year_month source;
input cust_id 3. depo_dt :yymmdd10. deposit_amt dollar6.2 year_month source $16.;
datalines;
822 2019-07-27 $25.00 201907 PNCFS
268 2019-07-07 $68.00 201907 JPMORGAN
878 2019-07-18 $50.00 201907 STATESTREET
790 2019-07-26 $26.00 201907 USBANKCORP
;;
run;

data source_data_201908_hist;
infile datalines;
format cust_id 3. depo_dt yymmddd10. deposit_amt dollar6.2 year_month source;
input cust_id 3. depo_dt :yymmdd10. deposit_amt dollar6.2 year_month source $16.;
datalines;
822 2019-08-23 $30.00 201908 CHASE
268 2019-08-09 $70.00 201908 PNCFS
878 2019-08-22 $80.00 201908 USBANK
790 2019-08-19 $20.00 201908 BOA
;;
run;

data source_data_201909_hist;
infile datalines;
format cust_id 3. depo_dt yymmddd10. deposit_amt dollar6.2 year_month source;
input cust_id 3. depo_dt :yymmdd10. deposit_amt dollar6.2 year_month source $16.;
datalines;
871 2019-09-21 $20.00 201909 USBANK
888 2019-09-13 $90.00 201909 WELLSFARGO
847 2019-09-25 $60.00 201909 CHASE
821 2019-09-18 $30.00 201909 CITIGROUP
;;
run;
&lt;BR /&gt;/* Hard-coded versions */
proc sql;
  create table work.test1 as select *
  from work.source_data_201907_hist
  where depo_dt &amp;gt;= '01jul2019'd
  and depo_dt &amp;lt; '01aug2019'd
  and year_month = 201907;
quit;

proc sql;
  create table work.test2 as select *
  from work.source_data_201908_hist
  where depo_dt &amp;gt;= '01aug2019'd
  and depo_dt &amp;lt; '01sep2019'd
  and year_month = 201908;
quit;

proc sql;
  create table work.test3 as select *
  from work.source_data_201909_hist
  where depo_dt &amp;gt;= '01sep2019'd
  and depo_dt &amp;lt; '01oct2019'd
  and year_month = 201909;
quit;
&lt;BR /&gt;/* Macro version */
%macro sql_loop(tbl,start,end,ym);

proc sql;
  create table work.&amp;amp;tbl as select *
  from work.source_data_&amp;amp;ym._hist
  where depo_dt &amp;gt;= &amp;amp;start
  and depo_dt &amp;lt; &amp;amp;end
  and year_month = &amp;amp;ym;
quit;

proc append base=final_data data=work.&amp;amp;tbl force;
run;

proc datasets lib=work nodetails nolist;
delete work.&amp;amp;tbl;
run; quit;

%mend;&lt;BR /&gt;&lt;BR /&gt;%sql_loop;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The idea for me is I don't want to make any explicit calls at the end, just call the macro at the end like I have above. I found the code below&amp;nbsp;that loops through dates but am confused about how to work&amp;nbsp;the do loop&amp;nbsp;into my query and define the macro variable within the macro. And again I need to loop through not just dates but the table name and yearmonth.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro date_loop(start,end);
   %let start=%sysfunc(inputn(&amp;amp;start,anydtdte9.));
   %let end=%sysfunc(inputn(&amp;amp;end,anydtdte9.));
   %let dif=%sysfunc(intck(month,&amp;amp;start,&amp;amp;end));
     %do i=0 %to &amp;amp;dif;
      %let date=%sysfunc(intnx(month,&amp;amp;start,&amp;amp;i,b),date9.);
      %put &amp;amp;date;
     %end;
   %mend date_loop;

   %date_loop(01jul2015,01feb2016)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The documentation you provided has helped me think about it and understand the examples a bit&amp;nbsp;but I'm just stuck on implemented it within mine.&amp;nbsp;Any help would be greatly appreciated.&lt;/P&gt;</description>
      <pubDate>Thu, 10 Oct 2019 16:51:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Proc-SQL-Macro-Efficiency-Automation-Involving-Teradata/m-p/595491#M171324</guid>
      <dc:creator>Time_Looper47</dc:creator>
      <dc:date>2019-10-10T16:51:18Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Proc SQL Macro Efficiency/Automation Involving Teradata</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Proc-SQL-Macro-Efficiency-Automation-Involving-Teradata/m-p/595498#M171326</link>
      <description>&lt;P&gt;Nothing is stopping you from making as many macro variables as you need to help make your logic clear and your code generation easy.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let n_months=%sysfunc(intck(month,&amp;amp;start,&amp;amp;end));
%do index=0 to &amp;amp;n_months;
   %let start_of_month = %sysfunc(intnx(month,&amp;amp;start,&amp;amp;index,s));
   %let part_of_table_name = %sysfunc(putn(&amp;amp;start_of_month,yymmn6.));
   %let td_start=DATE(%str(%')%sysfunc(putn(&amp;amp;start_of_month,yymmddd10.))%str('));
   %let td_end=DATE(%str(%')%sysfunc(intnx(month(&amp;amp;start_of_month,0,e),yymmddd10.)%str('));
  ...
  from xxxx&amp;amp;part_of_table_name.
  where datevar between &amp;amp;td_start and &amp;amp;td_end 
  ...
%end;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 10 Oct 2019 17:03:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Proc-SQL-Macro-Efficiency-Automation-Involving-Teradata/m-p/595498#M171326</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-10-10T17:03:30Z</dc:date>
    </item>
  </channel>
</rss>

