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).

Learn from the Experts! Check out the huge catalog of free sessions in the Ask the Expert webinar series.

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).

Learn from the Experts! Check out the huge catalog of free sessions in the Ask the Expert webinar series.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 2431 views
  • 1 like
  • 3 in conversation