BookmarkSubscribeRSS Feed
MikePHFEWIC
Calcite | Level 5
I am trying to retrieve SmallDateTime and DateTime data from SQL Server and having trouble getting it to useful formats. Sometimes I need just the dates, sometimes I need the full datetime, sometimes I need to use the SQL Server DateDiff to get hours difference, or minutes or something. Help!

Anyway to do something like:
PROC SQL;
CREATE TABLE XXX AS
SELECT DateDiff(hh,Date1,Date2) as HoursDifference
FROM mySQLTable;

Or can I read the raw dates and apply some inFormat or Format right in the SQL? I can get the difference in a second data step?

Or can I read the raw dates as text then convert them somehow to a datetime format in a second data step?

How do people normal work with SQL Server DateTime values?????
2 REPLIES 2
LinusH
Tourmaline | Level 20
If you want t use SQL Server functions, you need to enclose your query using explicit SQL pass-thru.
You could also use SAS functions/formats, even in the SQL that is reading your SQL Server table(s).
No need for a subsequent data step, unless if you feel more comfortable by doing so.
Use the DATEPART function to convert datetime values to dates.

/Linus
Data never sleeps
ChrisNZ
Tourmaline | Level 20
Alternatively, run the SQL in sas and use sas functions:
[pre]
PROC SQL;
CREATE TABLE XXX AS
SELECT intck('hour',Datetime1,Datetime2) as HoursDifference
FROM mySQLTable;

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
  • 2 replies
  • 790 views
  • 0 likes
  • 3 in conversation