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)
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).
Datepart is just returning nulls
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).
Thanks Chris. That worked
Nearly 200 sessions are now available on demand in the Innovate Hub.
Watch Now →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.