Converting between Date and Datetime in SAS

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_;
  put d date.;
  put dt datetime.;
  put d date.;

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_;
  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.;

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_;
  put d date.;
  put dt datetime.;
  d =datepart(dt);
  put d date.;


  • AramT
    Posted December 15, 2009 at 8:52 pm | Permalink

    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.

    Posted November 10, 2011 at 6:46 pm | Permalink

    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.

  • Posted March 21, 2012 at 8:19 pm | Permalink

    Thanks for posting this. It worked on the first try.

  • Stefan K
    Posted April 24, 2012 at 3:10 pm | Permalink

    cool stuff. Helped out after I was trying for like an hour šŸ˜‰

  • Georgette
    Posted February 8, 2013 at 6:49 pm | Permalink

    Thanks so much. So helpful and quick!

  • lance
    Posted February 25, 2013 at 9:52 pm | Permalink

    YES!, thanks for this

  • KS
    Posted March 1, 2013 at 11:39 am | Permalink

    very useful!

  • Manas
    Posted May 27, 2013 at 2:59 pm | Permalink

    Awesome Link

  • Juan Esteban de la Calle
    Posted April 30, 2014 at 1:44 pm | Permalink

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

  • Michael Arichea
    Posted June 11, 2014 at 9:01 pm | Permalink

    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…

    • philihp
      Posted July 8, 2014 at 6:24 am | Permalink

      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.

  • jimmy
    Posted July 31, 2014 at 2:54 pm | Permalink

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

    I found this on the SAS DHMS reference:

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

  • Bettina
    Posted October 20, 2014 at 11:41 am | Permalink

    this page made my day !

  • mark y
    Posted September 18, 2017 at 11:09 am | Permalink

    I found this while googling “sas convert date9 to datetime”. Very helpful – thanks!

    • Philihp
      Posted November 8, 2017 at 6:56 pm | Permalink

      You’re welcome! That’s the point šŸ™‚ Every time I search for something and don’t find it, I make a blog post for it.

  • trusted dumps seller
    Posted September 8, 2018 at 5:32 pm | Permalink

    I am really loving the theme/design of your website. Do you ever run into any browser compatibility issues?
    A number of my blog audience have complained about my website not
    working correctly in Explorer but looks great in Firefox. Do you have
    any suggestions to help fix this issue?

Post a Comment

Your email is kept private. Required fields are marked *