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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 2 replies
  • 1373 views
  • 0 likes
  • 3 in conversation