Hi,
I'd like to run a Proc SQL step to link 3 databases.
I'm trying to use the WHERE statement to filter results by subtracting a date one from table from the other.
Here's what it looks like right now:
proc sql;
connect to oracle;
create table MRSA as select * from connection to oracle
(
select distinct
c.MRN,
a.account_num, a.discharge_date,
b.collect_date, a.admit_date
from table1 a,
table2 b,
table3 c
where a.td_patient_id=b.td_patient_id and
b.td_patient_id=c.td_patient_id and
0 <= (datepart(b.collect_date) - datepart(a.admit_date)) <= 2 and
trunc(a.admit_date, 'DDD') >= '1-Jan-2012' and
trunc(a.admit_date, 'DDD') <= '1-Jan-2012'
);
disconnect from oracle;
quit;
I'm receiving this error when running:
ORACLE prepare error
ORA-00933: SQL command not properly ended
Thanks for the help.
Try replacing
0 <= (trunc(b.collect_date, 'DDD') - trunc(a.admit_date, 'DDD')) <= 2
with
(trunc(b.collect_date, 'DDD') - trunc(a.admit_date, 'DDD')) >= 0 and
(trunc(b.collect_date, 'DDD') - trunc(a.admit_date, 'DDD')) <= 2
then try using between 0 and 2
Ah good point. I tried using the trunc function instead but got the same error
proc sql;
connect to oracle;
create table MRSA as select * from connection to oracle
(
select distinct
c.MRN,
a.account_num, a.discharge_date,
b.collect_date, a.admit_date
from table1 a,
table2 b,
table3 c
where a.td_patient_id=b.td_patient_id and
b.td_patient_id=c.td_patient_id and
0 <= (trunc(b.collect_date, 'DDD') - trunc(a.admit_date, 'DDD')) <= 2 and
trunc(a.admit_date, 'DDD') >= '1-Jan-2012' and
trunc(a.admit_date, 'DDD') <= '1-Jan-2012'
);
disconnect from oracle;
quit;
To narrow down the cause of the error, I would try to remove more and more parts of the query (beginning with the WHERE clause) until the error disappears. Then reconstruct the original query in even smaller steps, until the error reappears. (If it doesn't reappear, you're lucky.) Of course, you have to make sure that each of the reduced queries still makes sense.
I think, at least one simplification could be done anyway: Aren't the last two lines of the WHERE clause equivalent to trunc(...)='...' or is Oracle's logic at odds with mathematics?
I QC'd and the cause of the error is this line:
0 <= (trunc(b.collect_date, 'DDD') - trunc(a.admit_date, 'DDD')) <= 2
When I comment it out, the code runs.
For the last two lines, I changed it just for QC purposes. The actual date range to pull the data will be over a couple of years.
Good. And, in general, Oracle can handle such chains of inequalities (as it is possible in SAS, except in SAS Macro language)?
Maybe you could try to use the BETWEEN condition trunc(...) - trunc(...) between 0 and 2 instead (if that is the correct syntax in Oracle). Or something like abs(trunc(...) - trunc(...) - 1) <= 1 (assuming there is an ABS function like the one in SAS).
Try replacing
0 <= (trunc(b.collect_date, 'DDD') - trunc(a.admit_date, 'DDD')) <= 2
with
(trunc(b.collect_date, 'DDD') - trunc(a.admit_date, 'DDD')) >= 0 and
(trunc(b.collect_date, 'DDD') - trunc(a.admit_date, 'DDD')) <= 2
then try using between 0 and 2
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.
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.