<?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: proc sql with connection to oracle:  to_date with macro variable in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-with-connection-to-oracle-to-date-with-macro-variable/m-p/23724#M5290</link>
    <description>I think its easier to create the macro variable such that it includes the TO_DATE() as well.  Then you can use use the variable plainly as&lt;BR /&gt;
&lt;BR /&gt;
data work.dbdate;&lt;BR /&gt;
format &lt;BR /&gt;
	dbvalue1 date9.&lt;BR /&gt;
	dbvalue2 date9.;&lt;BR /&gt;
input dbvalue1:date9. dbvalue2:date9.;&lt;BR /&gt;
cards;&lt;BR /&gt;
30nov2010 31dec2010&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data _null_;&lt;BR /&gt;
set work.dbdate;&lt;BR /&gt;
call symput('DAY1',"TO_DATE('"||PUT(DBVALUE1,YYMMDDN8.)||"','YYYYMMDD')");&lt;BR /&gt;
call symput('DAY2',"TO_DATE('"||PUT(DBVALUE2,YYMMDDN8.)||"','YYYYMMDD')");&lt;BR /&gt;
RUN;&lt;BR /&gt;
&lt;BR /&gt;
proc sql;&lt;BR /&gt;
connect to oracle as db(xxxxxxxxxxxxx);&lt;BR /&gt;
&lt;BR /&gt;
create table tst as select * from connection to db (&lt;BR /&gt;
	select * from mdm.dbdates &lt;BR /&gt;
	where date_value between &amp;amp;day1 and &amp;amp;day2&lt;BR /&gt;
);&lt;BR /&gt;
&lt;BR /&gt;
disconnect from db;&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
The oracle prepared statement is:&lt;BR /&gt;
select * from mdm.dbdates where date_value between TO_DATE('20101130','YYYYMMDD') and TO_DATE('20101231','YYYYMMDD')</description>
    <pubDate>Tue, 08 Mar 2011 14:31:13 GMT</pubDate>
    <dc:creator>DBailey</dc:creator>
    <dc:date>2011-03-08T14:31:13Z</dc:date>
    <item>
      <title>proc sql with connection to oracle:  to_date with macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-with-connection-to-oracle-to-date-with-macro-variable/m-p/23720#M5286</link>
      <description>within PROC SQL WITH CONNECTION TO ORACLE:&lt;BR /&gt;
I am trying to replace typing in a date with a macro variable.&lt;BR /&gt;
&lt;BR /&gt;
Instead of: &lt;BR /&gt;
WHERE ( DATE_ADDED_TRANSACTION BETWEEN TO_DATE('&lt;B&gt;20110101&lt;/B&gt;000000','YYYYMMDDHH24MISS') &lt;BR /&gt;
AND TO_DATE('&lt;B&gt;20110131&lt;/B&gt;000000','YYYYMMDDHH24MISS') )&lt;BR /&gt;
&lt;BR /&gt;
I want:&lt;BR /&gt;
WHERE( DATE_ADDED_TRANSACTION BETWEEN TO_DATE&lt;B&gt;("&amp;amp;STRT_DT.&lt;/B&gt;000000",'YYYYMMDDHH24MISS') &lt;BR /&gt;
AND TO_DATE&lt;B&gt;("&amp;amp;END_DT.&lt;/B&gt;000000",'YYYYMMDDHH24MISS') )&lt;BR /&gt;
&lt;BR /&gt;
But when I switch to double-quotes I get an error:&lt;BR /&gt;
ORACLE prepare error: ORA-00904: "20110101000000": invalid identifier.&lt;BR /&gt;
&lt;BR /&gt;
So the macro variable appears to be resolving correctly, but it doesn't like the double quotes?</description>
      <pubDate>Mon, 07 Mar 2011 19:21:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/proc-sql-with-connection-to-oracle-to-date-with-macro-variable/m-p/23720#M5286</guid>
      <dc:creator>ddeb</dc:creator>
      <dc:date>2011-03-07T19:21:28Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql with connection to oracle:  to_date with macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-with-connection-to-oracle-to-date-with-macro-variable/m-p/23721#M5287</link>
      <description>Oracle does not accept double quotes.  All strings and dates need to be wrapped in single quotes.&lt;BR /&gt;
&lt;BR /&gt;
You'll have to use something like this below&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
%let ora_today=%str(%')%sysfunc(putn(%sysfunc(today()),yymmddn8.))000000%str(%');&lt;BR /&gt;
%put ora_today=&amp;amp;ora_today;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Also I don't think you need the to_char Oracle is smart enough to distinguish between dates and datetime.&lt;BR /&gt;
&lt;BR /&gt;
Try this&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
%let ora_today2=%str(%')%sysfunc(putn(%sysfunc(today()),date11.))%str(%');&lt;BR /&gt;
%put ora_today2=&amp;amp;ora_today2;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Regards&lt;BR /&gt;
Darryl</description>
      <pubDate>Mon, 07 Mar 2011 20:22:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/proc-sql-with-connection-to-oracle-to-date-with-macro-variable/m-p/23721#M5287</guid>
      <dc:creator>darrylovia</dc:creator>
      <dc:date>2011-03-07T20:22:00Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql with connection to oracle:  to_date with macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-with-connection-to-oracle-to-date-with-macro-variable/m-p/23722#M5288</link>
      <description>I've used something similar to Darryl's approach, using %nbrquote rather than %str (I'm not sure what difference this makes).  This gets around the issue that Oracle can't use double quotes, but that SAS won't decode a macro value inside single quotes.&lt;BR /&gt;
&lt;BR /&gt;
where&lt;BR /&gt;
History.Date between TO_DATE(%nrbquote(')&amp;amp;StartDateTime.%nrbquote('), 'DD:MM:YYYY:HH24:MI:SS')&lt;BR /&gt;
                 and TO_DATE(%nrbquote(')&amp;amp;FinalDateTime.%nrbquote('), 'DD:MM:YYYY:HH24:MI:SS')&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
My StartDateTime and FinalDateTime macros are formatted as datetime20., but you could change this to your own format quite easily.&lt;BR /&gt;
&lt;BR /&gt;
For example I might use:&lt;BR /&gt;
&lt;BR /&gt;
data _null_;&lt;BR /&gt;
call symput('StartDateTime',put(intnx('dtday',datetime(),-1),datetime20.));&lt;BR /&gt;
call symput('FinalDateTime',put(intnx('dtday',datetime(),-1,'end'),datetime20.));&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
%put &amp;amp;StartDateTime. &amp;amp;FinalDateTime.;</description>
      <pubDate>Tue, 08 Mar 2011 13:00:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/proc-sql-with-connection-to-oracle-to-date-with-macro-variable/m-p/23722#M5288</guid>
      <dc:creator>DF</dc:creator>
      <dc:date>2011-03-08T13:00:42Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql with connection to oracle:  to_date with macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-with-connection-to-oracle-to-date-with-macro-variable/m-p/23723#M5289</link>
      <description>DF and Darryl.  Thanks so much.  Worked perfectly.  I was ready to give up and type in my date range for each run.&lt;BR /&gt;
&lt;BR /&gt;
Yeah for efficiency!</description>
      <pubDate>Tue, 08 Mar 2011 14:23:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/proc-sql-with-connection-to-oracle-to-date-with-macro-variable/m-p/23723#M5289</guid>
      <dc:creator>ddeb</dc:creator>
      <dc:date>2011-03-08T14:23:15Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql with connection to oracle:  to_date with macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/proc-sql-with-connection-to-oracle-to-date-with-macro-variable/m-p/23724#M5290</link>
      <description>I think its easier to create the macro variable such that it includes the TO_DATE() as well.  Then you can use use the variable plainly as&lt;BR /&gt;
&lt;BR /&gt;
data work.dbdate;&lt;BR /&gt;
format &lt;BR /&gt;
	dbvalue1 date9.&lt;BR /&gt;
	dbvalue2 date9.;&lt;BR /&gt;
input dbvalue1:date9. dbvalue2:date9.;&lt;BR /&gt;
cards;&lt;BR /&gt;
30nov2010 31dec2010&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data _null_;&lt;BR /&gt;
set work.dbdate;&lt;BR /&gt;
call symput('DAY1',"TO_DATE('"||PUT(DBVALUE1,YYMMDDN8.)||"','YYYYMMDD')");&lt;BR /&gt;
call symput('DAY2',"TO_DATE('"||PUT(DBVALUE2,YYMMDDN8.)||"','YYYYMMDD')");&lt;BR /&gt;
RUN;&lt;BR /&gt;
&lt;BR /&gt;
proc sql;&lt;BR /&gt;
connect to oracle as db(xxxxxxxxxxxxx);&lt;BR /&gt;
&lt;BR /&gt;
create table tst as select * from connection to db (&lt;BR /&gt;
	select * from mdm.dbdates &lt;BR /&gt;
	where date_value between &amp;amp;day1 and &amp;amp;day2&lt;BR /&gt;
);&lt;BR /&gt;
&lt;BR /&gt;
disconnect from db;&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
The oracle prepared statement is:&lt;BR /&gt;
select * from mdm.dbdates where date_value between TO_DATE('20101130','YYYYMMDD') and TO_DATE('20101231','YYYYMMDD')</description>
      <pubDate>Tue, 08 Mar 2011 14:31:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/proc-sql-with-connection-to-oracle-to-date-with-macro-variable/m-p/23724#M5290</guid>
      <dc:creator>DBailey</dc:creator>
      <dc:date>2011-03-08T14:31:13Z</dc:date>
    </item>
  </channel>
</rss>

