BookmarkSubscribeRSS Feed
ddeb
Calcite | Level 5
within PROC SQL WITH CONNECTION TO ORACLE:
I am trying to replace typing in a date with a macro variable.

Instead of:
WHERE ( DATE_ADDED_TRANSACTION BETWEEN TO_DATE('20110101000000','YYYYMMDDHH24MISS')
AND TO_DATE('20110131000000','YYYYMMDDHH24MISS') )

I want:
WHERE( DATE_ADDED_TRANSACTION BETWEEN TO_DATE("&STRT_DT.000000",'YYYYMMDDHH24MISS')
AND TO_DATE("&END_DT.000000",'YYYYMMDDHH24MISS') )

But when I switch to double-quotes I get an error:
ORACLE prepare error: ORA-00904: "20110101000000": invalid identifier.

So the macro variable appears to be resolving correctly, but it doesn't like the double quotes?
4 REPLIES 4
darrylovia
Quartz | Level 8
Oracle does not accept double quotes. All strings and dates need to be wrapped in single quotes.

You'll have to use something like this below


%let ora_today=%str(%')%sysfunc(putn(%sysfunc(today()),yymmddn8.))000000%str(%');
%put ora_today=&ora_today;


Also I don't think you need the to_char Oracle is smart enough to distinguish between dates and datetime.

Try this


%let ora_today2=%str(%')%sysfunc(putn(%sysfunc(today()),date11.))%str(%');
%put ora_today2=&ora_today2;


Regards
Darryl
DF
Fluorite | Level 6 DF
Fluorite | Level 6
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.

where
History.Date between TO_DATE(%nrbquote(')&StartDateTime.%nrbquote('), 'DD:MM:YYYY:HH24:MI:SS')
and TO_DATE(%nrbquote(')&FinalDateTime.%nrbquote('), 'DD:MM:YYYY:HH24:MI:SS')


My StartDateTime and FinalDateTime macros are formatted as datetime20., but you could change this to your own format quite easily.

For example I might use:

data _null_;
call symput('StartDateTime',put(intnx('dtday',datetime(),-1),datetime20.));
call symput('FinalDateTime',put(intnx('dtday',datetime(),-1,'end'),datetime20.));
run;

%put &StartDateTime. &FinalDateTime.;
ddeb
Calcite | Level 5
DF and Darryl. Thanks so much. Worked perfectly. I was ready to give up and type in my date range for each run.

Yeah for efficiency!
DBailey
Lapis Lazuli | Level 10
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

data work.dbdate;
format
dbvalue1 date9.
dbvalue2 date9.;
input dbvalue1:date9. dbvalue2:date9.;
cards;
30nov2010 31dec2010
run;


data _null_;
set work.dbdate;
call symput('DAY1',"TO_DATE('"||PUT(DBVALUE1,YYMMDDN8.)||"','YYYYMMDD')");
call symput('DAY2',"TO_DATE('"||PUT(DBVALUE2,YYMMDDN8.)||"','YYYYMMDD')");
RUN;

proc sql;
connect to oracle as db(xxxxxxxxxxxxx);

create table tst as select * from connection to db (
select * from mdm.dbdates
where date_value between &day1 and &day2
);

disconnect from db;
quit;


The oracle prepared statement is:
select * from mdm.dbdates where date_value between TO_DATE('20101130','YYYYMMDD') and TO_DATE('20101231','YYYYMMDD')

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 7813 views
  • 0 likes
  • 4 in conversation