In SAS, fields are either character of varying length, or numeric. No exceptions. Temporal values such as Date and Date/Time are stored as either the number of days or seconds since 1960 January 1st.

In order to convert from Date/Time to Date, and from Date to Date/Time, you could divide or multiply respectively by 86400 (the number of seconds in a day).

data _null_;
  d='29FEB1984'd;
  put d date.;
  dt=d*24*60*60;
  put dt datetime.;
  d=dt/24/60/60;
  put d date.;
run;

But unless someone deals with dates every day, it might not be obvious to them by reading this code what those magic numbers are doing.

Another method would be to use put() and input() using functions to go back and fourth.

data _null_;
  d='29FEB1984'd;
  put d date.;
  dt=input(put( d  ,date7.) || ':00:00:00', datetime.);
  put dt datetime.;
  d =input(substr(put(dt,datetime.),1,7),date7.);
  put d date.;
run;

But I don’t consider this any better. It’s wordy. Functions are nested within functions. And you still have a magic number for the call to substr. Also, since it isn’t uncommon for SAS datasets to get into the billions of rows, this probably isn’t the fastest way to do it.

SAS has built-in functions called dhms() (mnemonic: date,hour,minute,second) and datepart() that should make things a lot easier.

data _null_;
  d='29FEB1984'd;
  put d date.;
  dt=dhms(d,0,0,0);
  put dt datetime.;
  d =datepart(dt);
  put d date.;
run;

13 thoughts on “Converting between Date and Datetime in SAS

  1. This was surprisingly helpful, thank you! I spent hours trying to convert between the two and only now do I realize that the DT field is nothing more than a number of seconds where as the date is the number of days.

    Reply
  2. This post is so helpful. I have spent three hours trying to figure out how to do subtractions between datetime and date. This post was the bingo.

    Reply
  3. The article was very helpful. I have never found an article on sas functions that worked so fast.

    Reply
  4. THANK YOU! Concise, and wonderfully clear. (I actually immediately understood “24*60*60” in the first code snippet, without reading anything first.) Makes you wonder what those technical writers are smoking in Cary, when there’s absolutely nothing in their documentation (that I can find) that explicitly states what the difference between a SAS date and datetime are!! ugh…

    Reply
    • You’re welcome :) I agree, the core SAS foundation documentation is about 20 years overdue for some love. As a SAS customer and user, you really should tell them this. I promise, you won’t hurt any feelings; they need to hear it.

      Reply
  5. I landed here looking for a way to convert a date and time into a datetime.

    I found this on the SAS DHMS reference:
    day=date();
    time=time();
    sasdt=dhms(day,0,0,time);

    This works because ‘time’ is just the number of seconds since 00:00:00.

    Reply

Leave a reply

required

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>