BookmarkSubscribeRSS Feed
JDMTX
Fluorite | Level 6
I am working with data from MS SQL Server that is surfaced in SAS as a data library via SAS Access for ODBC and wanted to know if there is a system option that treats the MS SQL data type of DATETIME as a SAS date instead of a SAS DATETIME. Right now I am having to use the datepart function to extract the date prior to doing any date comparisons. ANy ideas or am I stuck with using the datepart function?
5 REPLIES 5
Doc_Duke
Rhodochrosite | Level 12
Stuck with Datetime. Welcome to the club.
Peter_C
Rhodochrosite | Level 12
I understand how datepart() can damage performance, and so, why you want to avoid it: here are some tips
1 compare datetime columns with date constants extended into datetime constants like[pre] where SQL_date_column between "&date1:0:0:0"dt and "&date2:0:0:0"dt[/pre] with just date constants in &date1 and &date2

2 for the INTNX() function there are DTxxxxx intervals

3 for formatting there are DTxxxxx formats like DTDATE9 to display just the date part of a datetime value. There are also some NLxxxxx formats which might help with datetime values.

It is not a complete solution but deals with some of the trouble.

PeterC
JDMTX
Fluorite | Level 6
Peter -

Based upon your earlier reply, is the following statement true: "Even if I apply a format like DTDATE9. it is necessary to treat the underlying value as a datetime value when applying a subseting if statment in a data step as it is not possible to use a date constant short of creating a new variable that contains only the datepart or extend a date constant into a datetime constant (example: "&date2:0:0:0"dt)."
Peter_C
Rhodochrosite | Level 12
> Peter -
>
> Based upon your earlier reply, is the following
> statement true: "Even if I apply a format like
> DTDATE9. it is necessary to treat the underlying
> value as a datetime value when applying a subseting
> if statment in a data step as it is not possible to
> use a date constant short of creating a new variable
> that contains only the datepart or extend a date
> constant into a datetime constant (example:
> "&date2:0:0:0"dt)."

I consider this true, as I think "&date2:0:0:0"dt will use that date constant correctly.

I think this extract
> ............................. it is not possible to
> use a date constant
is false
My reasoning for working with datetime values in data step or sql programming :
avoid functions (like datepart) that might cause an entire teradata or oracle table to be passed to SAS for processing.
It is neither difficult nor unclear (imho) to extend a sas date constant into a datetime constant.
If it is possible that the dbms column of datetime type might hold a time value as well as a date value, rather than seek equality with &date2:0:0:0"dt, we can use a range like
where dbms_datetime between "&sasdate:00:0"dt and "&sasdate:24:0:0"dt
in place of
where datepart( dbms_datetime) = "&sasdate"d

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 5 replies
  • 1720 views
  • 0 likes
  • 4 in conversation