- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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).
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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