<?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 passing dates to filter on oracle for  macro program in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/passing-dates-to-filter-on-oracle-for-macro-program/m-p/883271#M348987</link>
    <description>&lt;P&gt;I am trying to create a macro program that iterates over a dataset in Oracle and filters on the publish_yr_mth variable and publish date. So I would have a publish date of something like 201001 (Jan 2010) and then publish_dt&amp;gt;01-JAN-2010. And then move to the next set of dates (but not always at even intervals, just whatever is set in the dataset).&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I create the macro, it will work with the publish_yr_mnth, but I cannot even just put in a string for the publish_dt or the macro won't run. How do I pass&amp;nbsp; a date that's readable to Oracle in this macro . Outside of macros, the 'DD-MON-YYYY' is being accepted as the correct format and will bring back a result.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Or is there something else causing this not to run?&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;non-marco version that will run:&lt;/P&gt;&lt;PRE&gt;proc sql ;

connect to oracle as mydb(user=user orapw="&amp;amp;dbpass" path=oracledb );

create table bk_samp as select * from connection to mdb (

&amp;nbsp; &amp;nbsp; &amp;nbsp;select author ,title from books_db where publish_dt &amp;gt;=’01-JAN-2010’ and publish_yr_mnth=201001&amp;nbsp; ) ;

quit;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Macro version that I'm trying to run:&lt;/P&gt;&lt;PRE&gt;&lt;BR /&gt;data months;&lt;BR /&gt;input publishdt publish_yr_mnth;&lt;BR /&gt;datalines ;&lt;BR /&gt;201001 01-JAN-2010&lt;BR /&gt;201002 02-FEB-2010&lt;BR /&gt;201005 02-MAY-2010&lt;BR /&gt;;&lt;BR /&gt;&lt;BR /&gt;proc sql ;&lt;BR /&gt;select distinct publishdt&lt;BR /&gt;into: publishdt1-&lt;BR /&gt;from months;&lt;BR /&gt;quit&lt;BR /&gt;p&lt;/PRE&gt;&lt;PRE&gt;%macro books(publishdt, publish_yr_mnth): &lt;BR /&gt;%do i =1 %to &amp;amp;sqlobs; proc sql ; &lt;BR /&gt;connect to oracle as mydb(user=user orapw="&amp;amp;dbpass" path=oracledb );&lt;BR /&gt; create table bk_samp as select * from connection to mdb (&lt;BR /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;select author ,title from books_db where publish_dt &amp;gt;=&amp;amp;&amp;amp;publishdt&amp;amp;i and publish_yr_mnth=&amp;amp;&amp;amp;publishyrmth&amp;amp;i&lt;BR /&gt; ) ;&lt;BR /&gt; quit; &lt;BR /&gt;%end; &lt;BR /&gt;%mend books&lt;BR /&gt;%books();&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 03 Jul 2023 01:07:48 GMT</pubDate>
    <dc:creator>KatWinSASland</dc:creator>
    <dc:date>2023-07-03T01:07:48Z</dc:date>
    <item>
      <title>passing dates to filter on oracle for  macro program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/passing-dates-to-filter-on-oracle-for-macro-program/m-p/883271#M348987</link>
      <description>&lt;P&gt;I am trying to create a macro program that iterates over a dataset in Oracle and filters on the publish_yr_mth variable and publish date. So I would have a publish date of something like 201001 (Jan 2010) and then publish_dt&amp;gt;01-JAN-2010. And then move to the next set of dates (but not always at even intervals, just whatever is set in the dataset).&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I create the macro, it will work with the publish_yr_mnth, but I cannot even just put in a string for the publish_dt or the macro won't run. How do I pass&amp;nbsp; a date that's readable to Oracle in this macro . Outside of macros, the 'DD-MON-YYYY' is being accepted as the correct format and will bring back a result.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Or is there something else causing this not to run?&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;non-marco version that will run:&lt;/P&gt;&lt;PRE&gt;proc sql ;

connect to oracle as mydb(user=user orapw="&amp;amp;dbpass" path=oracledb );

create table bk_samp as select * from connection to mdb (

&amp;nbsp; &amp;nbsp; &amp;nbsp;select author ,title from books_db where publish_dt &amp;gt;=’01-JAN-2010’ and publish_yr_mnth=201001&amp;nbsp; ) ;

quit;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Macro version that I'm trying to run:&lt;/P&gt;&lt;PRE&gt;&lt;BR /&gt;data months;&lt;BR /&gt;input publishdt publish_yr_mnth;&lt;BR /&gt;datalines ;&lt;BR /&gt;201001 01-JAN-2010&lt;BR /&gt;201002 02-FEB-2010&lt;BR /&gt;201005 02-MAY-2010&lt;BR /&gt;;&lt;BR /&gt;&lt;BR /&gt;proc sql ;&lt;BR /&gt;select distinct publishdt&lt;BR /&gt;into: publishdt1-&lt;BR /&gt;from months;&lt;BR /&gt;quit&lt;BR /&gt;p&lt;/PRE&gt;&lt;PRE&gt;%macro books(publishdt, publish_yr_mnth): &lt;BR /&gt;%do i =1 %to &amp;amp;sqlobs; proc sql ; &lt;BR /&gt;connect to oracle as mydb(user=user orapw="&amp;amp;dbpass" path=oracledb );&lt;BR /&gt; create table bk_samp as select * from connection to mdb (&lt;BR /&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;select author ,title from books_db where publish_dt &amp;gt;=&amp;amp;&amp;amp;publishdt&amp;amp;i and publish_yr_mnth=&amp;amp;&amp;amp;publishyrmth&amp;amp;i&lt;BR /&gt; ) ;&lt;BR /&gt; quit; &lt;BR /&gt;%end; &lt;BR /&gt;%mend books&lt;BR /&gt;%books();&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jul 2023 01:07:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/passing-dates-to-filter-on-oracle-for-macro-program/m-p/883271#M348987</guid>
      <dc:creator>KatWinSASland</dc:creator>
      <dc:date>2023-07-03T01:07:48Z</dc:date>
    </item>
    <item>
      <title>Re: passing dates to filter on oracle for  macro program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/passing-dates-to-filter-on-oracle-for-macro-program/m-p/883279#M348989</link>
      <description>&lt;P&gt;If ORACLE accepts 'MM-MON-YYYY' strings as valid date literals then that is what you should put into the macro variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  call symputx('oracle_date',quote(put(date(),date11.),"'"));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;1    data _null_;
2      call symputx('oracle_date',quote(put(date(),date11.),"'"));
3    run;

NOTE: DATA statement used (Total process time):
      real time           0.06 seconds
      cpu time            0.01 seconds


4    %put &amp;amp;=oracle_date;
ORACLE_DATE='02-JUL-2023'


&lt;/PRE&gt;
&lt;P&gt;If you must do it in pure macro code (why???) then try something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let oracle_date=%bquote('%sysfunc(date(),date11.)');
%put &amp;amp;=oracle_date;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;5    %let oracle_date=%bquote('%sysfunc(date(),date11.)');
6    %put &amp;amp;=oracle_date;
ORACLE_DATE='02-JUL-2023'
&lt;/PRE&gt;
&lt;P&gt;If the macro quoting causes you trouble wrap the whole thing in an %unquote() call.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let oracle_date=%unquote(%bquote('%sysfunc(date(),date11.)'));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;PS Your example macro has flawed logic.&amp;nbsp; It is just creating the same dataset over and over again.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jul 2023 03:27:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/passing-dates-to-filter-on-oracle-for-macro-program/m-p/883279#M348989</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-07-03T03:27:02Z</dc:date>
    </item>
    <item>
      <title>Re: passing dates to filter on oracle for  macro program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/passing-dates-to-filter-on-oracle-for-macro-program/m-p/883312#M348998</link>
      <description>&lt;P&gt;I believe Oracle will accept a string like&amp;nbsp;'01-JAN-2010' in a where clause and interpret it as a date if used for comparison with a variable of type date. If not then use Oracle function to_date()&lt;/P&gt;
&lt;P&gt;Oracle will need the date string in single quotes but SAS will not resolve a macro variable that's embedded in single quotes. To get around this use the following in your Oracle where clause:&lt;/P&gt;
&lt;P&gt;%tslit(&amp;amp;&amp;lt;your macro var with the date string&amp;gt;)&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sample code&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let test=01-JAN-2010;
%put %tslit(&amp;amp;test);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Printed to SAS log:&lt;/P&gt;
&lt;P&gt;'01-JAN-2010'&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If it was me then I'd probably would first try to go for an approach where I generate the where clause condition and then query the database only once. You might be able to generate such code with a SQL Select ... into :where_expression ....&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jul 2023 11:06:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/passing-dates-to-filter-on-oracle-for-macro-program/m-p/883312#M348998</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-07-03T11:06:11Z</dc:date>
    </item>
    <item>
      <title>Re: passing dates to filter on oracle for  macro program</title>
      <link>https://communities.sas.com/t5/SAS-Programming/passing-dates-to-filter-on-oracle-for-macro-program/m-p/883315#M348999</link>
      <description>&lt;P&gt;There seems to be several problems with your code:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;As&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;already wrote, you must get the Oracle date strings into single quotes&lt;/LI&gt;
&lt;LI&gt;You swapped the PUBLISH_DT and the PUBLISH_YR_MNTH variables in the MONTHS table, and your input will not work (at least the second variable should be character) - I assume you have a MONTHS table from elsewhere, and did not test the code to create it as shown.&lt;/LI&gt;
&lt;LI&gt;The code will overwrite the same output table (WORK.BK_SAMP) repeatedly&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;Instead of using a macro, I would consider writing the program to a temporary SAS file, e.g.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data months;
  input publish_yr_mnth 7. publishdt $11.;
datalines ;
201001 01-JAN-2010
201002 02-FEB-2010
201005 02-MAY-2010
;run;  

filename tempsas temp;
data _null_;
  set months;
  file tempsas;
put 
  'proc sql ; ' /
  '  connect to oracle as mydb(user=user orapw="&amp;amp;dbpass" path=oracledb );' /
  '  create table bk_samp' _N_ ' as select * from connection to mdb (  '/
  '    select author ,title from books_db where publish_dt &amp;gt;=''' publishdt +(-1) '''  and publish_yr_mnth=' publish_yr_mnth /
  ' ) ; '/
  'quit;';  
  ;

%include tempsas / source2;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To test the code, you can start by commenting out the "file tempsas" line and the "%include" line, and just look at the generated code in the log until it's OK, then uncomment and run for real.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I put in the _N_ after "BK_SAMP" so that 3 tables are created (BK_SAMP1-3). You may want to use the PUBLIST_YR_MNTH as a suffix instead, or collect all the data in a single table (remember to select the&amp;nbsp;PUBLIST_YR_MNTH column also in that case).&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jul 2023 11:00:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/passing-dates-to-filter-on-oracle-for-macro-program/m-p/883315#M348999</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2023-07-03T11:00:52Z</dc:date>
    </item>
  </channel>
</rss>

