SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
RAW_newbie
Obsidian | Level 7

I am passing a date range to a PROC SQL and attempting to use them in a where clause.  The query is against an Oracle database; the EFFECTIVEDATE comes in as date time format. 

 

If I supply the dates from within the SAS code:

%let Data_DT_MAX_EF= 18JUL2018; /*Used for testing*/

%let Data_DT_MIN_EF= 12JUL2018; /*Used for testing*/

And use them in the where clause:

EFFECTIVEDATE between &Data_DT_MIN_EF and &Data_DT_MAX_EF

There are no errors.

 

If I pass the dates from an EG program, the dates are passed between EGP and SAS however they are presented as numeric and the query fails.

Data work.Research_Date;

  Data_DT_MIN_EF = intnx('week1.5',&Report_Date3,-1,'End');

  Data_DT_MAX_EF = intnx('week1.5',&Report_Date3,-1,'Begin');

  format _all_ DATE9.;

  put (_all_)(=/);

  call symput ('Data_DT_MIN_EF', Data_DT_MIN_EF);

 

  call symput ('Data_DT_MAX_EF', Data_DT_MAX_EF);

run;

 

ERROR: ORACLE prepare error: ORA-00932: inconsistent datatypes: expected DATE got NUMBER. SQL statement: SELECT TRANID,...

where  EFFECTIVEDATE between 21383 and 21377

 

When and how is the best way to format the date variables to make the where clause successful?  

1 ACCEPTED SOLUTION

Accepted Solutions
RAW_newbie
Obsidian | Level 7

My failure was due to the formatting of the date being passed to the where clause.  Being new to converting dates I was missing the line in bold (middle of the 3 lines).  Currently a set of date intervals are developed within the EG program and passed to SAS base code with an "include" statement.  The same EGP loops through a dataset to supply a variable also supplied to the base code to produce 9 separate date dependent reports.

 

Data_DT_MIN_EF = intnx('week1.5',&Report_Date1,-1,'End');

 

Data_DT_MIN_EF2=put(Data_DT_MIN_EF,date9.);

call symput ('Data_DT_MIN_EF', Data_DT_MIN_EF2);

 

The SAS base code now interprets the dates correctly in the "where" clause which uses the EFFECTIVEDATE field within an Oracle table.

 

Where clause:

EFFECTIVEDATE between %bquote('&Data_DT_MAX_EF') and %bquote('&Data_DT_MIN_EF')

 

Quotes are supplied within the statement above to comply with Oracle needs.  The dates without quotes as supplied by the EGP statement are used elsewhere in the reports.

View solution in original post

3 REPLIES 3
Reeza
Super User

Remember it needs to be valid SAS syntax and your code would resolve to:

 

*What you have;
EFFECTIVEDATE between &Data_DT_MIN_EF and &Data_DT_MAX_EF

*what sas sees;
EFFECTIVEDATE between 12Jul2018 and 18Jul2018

*valid SAS syntax;
EFFECTIVEDATE between "&Data_DT_MIN_EF"d and "&Data_DT_MAX_EF"d

If you're using SQL Pass through you'll need a different method, since they dates usually need to be formatted dd-mm-yy with single quotes or something like that. It varies slightly depending on the DB. 

 


@RAW_newbie wrote:

I am passing a date range to a PROC SQL and attempting to use them in a where clause.  The query is against an Oracle database; the EFFECTIVEDATE comes in as date time format. 

 

If I supply the dates from within the SAS code:

%let Data_DT_MAX_EF= 18JUL2018; /*Used for testing*/

%let Data_DT_MIN_EF= 12JUL2018; /*Used for testing*/

And use them in the where clause:

EFFECTIVEDATE between &Data_DT_MIN_EF and &Data_DT_MAX_EF

There are no errors.

 

If I pass the dates from an EG program, the dates are passed between EGP and SAS however they are presented as numeric and the query fails.

Data work.Research_Date;

  Data_DT_MIN_EF = intnx('week1.5',&Report_Date3,-1,'End');

  Data_DT_MAX_EF = intnx('week1.5',&Report_Date3,-1,'Begin');

  format _all_ DATE9.;

  put (_all_)(=/);

  call symput ('Data_DT_MIN_EF', Data_DT_MIN_EF);

 

  call symput ('Data_DT_MAX_EF', Data_DT_MAX_EF);

run;

 

ERROR: ORACLE prepare error: ORA-00932: inconsistent datatypes: expected DATE got NUMBER. SQL statement: SELECT TRANID,...

where  EFFECTIVEDATE between 21383 and 21377

 

When and how is the best way to format the date variables to make the where clause successful?  


 

ChrisNZ
Tourmaline | Level 20

 

%let Data_DT_MAX_EF= 18JUL2018; /*Used for testing*/
%let Data_DT_MIN_EF= 12JUL2018; /*Used for testing*/
And use them in the where clause:
EFFECTIVEDATE between &Data_DT_MIN_EF and &Data_DT_MAX_EF

 

 

This code would not work in SAS or in Oracle.

 

Oracle needs (note the single quotes which are missing in the code above):

EFFECTIVEDATE between '12JUL2018' and '18JUL2018'

 

SAS needs (single or double quotes are accepted in SAS, double quotes are better is macro variables are used):

EFFECTIVEDATE between "12JUL2018"d and "18JUL2018"d

 

RAW_newbie
Obsidian | Level 7

My failure was due to the formatting of the date being passed to the where clause.  Being new to converting dates I was missing the line in bold (middle of the 3 lines).  Currently a set of date intervals are developed within the EG program and passed to SAS base code with an "include" statement.  The same EGP loops through a dataset to supply a variable also supplied to the base code to produce 9 separate date dependent reports.

 

Data_DT_MIN_EF = intnx('week1.5',&Report_Date1,-1,'End');

 

Data_DT_MIN_EF2=put(Data_DT_MIN_EF,date9.);

call symput ('Data_DT_MIN_EF', Data_DT_MIN_EF2);

 

The SAS base code now interprets the dates correctly in the "where" clause which uses the EFFECTIVEDATE field within an Oracle table.

 

Where clause:

EFFECTIVEDATE between %bquote('&Data_DT_MAX_EF') and %bquote('&Data_DT_MIN_EF')

 

Quotes are supplied within the statement above to comply with Oracle needs.  The dates without quotes as supplied by the EGP statement are used elsewhere in the reports.

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 16433 views
  • 0 likes
  • 3 in conversation