BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
bobbym
Fluorite | Level 6

Hi All,

   I have a sql function to convert a date-timestamp field that is numeric to a valid date format. I am trying to find if there is an equivalent function/script in SAS that could get me the same results like the following SQL server stmt:

 

cast(dateadd(s, convert(bigint, CREATED_TIME) / 1000, convert(datetime, '1-1-1970 00:00:00')) as char)

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisHemedinger
Community Manager

If the stored value is a datetime in epoch format (or Unix datetime), you need to first convert to a SAS datetime value.  Basically, that's adjusting the value by 10 years worth of seconds -- or milliseconds depending on how the data is stored.  See How to convert a Unix datetime to SAS.

 

/* Number of seconds between 01JAN1960 and 01JAN1970: 315619200 */
sasDT = unixDT + 315619200;

/* OR */

/* DHMS function calculates datetime when you provide values for */
/*    date, hour, minute, and seconds                            */
/* In this case, "seconds" is a very high value!                 */
sasDT = dhms('01jan1970'd, 0, 0, unixDT);

 

Once in a SAS datetime, you can use DATEPART() to get just the date value, and INTNX to add/subtract timespans at any interval (days, hours, seconds, months, years, and more).

SAS For Dummies 3rd Edition! Check out the new edition, covering SAS 9.4, SAS Viya, and all of the modern ways to use SAS!

View solution in original post

4 REPLIES 4
mbuchecker
Quartz | Level 8
use either the datepart function for extracting just the date. IE: datepart(variable) there is also the INTNX for adding dates.
Michelle
bobbym
Fluorite | Level 6

Datepart is just returning nulls

ChrisHemedinger
Community Manager

If the stored value is a datetime in epoch format (or Unix datetime), you need to first convert to a SAS datetime value.  Basically, that's adjusting the value by 10 years worth of seconds -- or milliseconds depending on how the data is stored.  See How to convert a Unix datetime to SAS.

 

/* Number of seconds between 01JAN1960 and 01JAN1970: 315619200 */
sasDT = unixDT + 315619200;

/* OR */

/* DHMS function calculates datetime when you provide values for */
/*    date, hour, minute, and seconds                            */
/* In this case, "seconds" is a very high value!                 */
sasDT = dhms('01jan1970'd, 0, 0, unixDT);

 

Once in a SAS datetime, you can use DATEPART() to get just the date value, and INTNX to add/subtract timespans at any interval (days, hours, seconds, months, years, and more).

SAS For Dummies 3rd Edition! Check out the new edition, covering SAS 9.4, SAS Viya, and all of the modern ways to use SAS!

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 4 replies
  • 3145 views
  • 1 like
  • 3 in conversation