<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Philihp.com &#187; Dates</title>
	<atom:link href="http://www.philihp.com/blog/tag/dates/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.philihp.com/blog</link>
	<description>Yes, it's a palindrome.</description>
	<lastBuildDate>Tue, 31 Aug 2010 19:40:45 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Converting between Date and Datetime in SAS</title>
		<link>http://www.philihp.com/blog/2009/converting-a-date-to-a-datetime-in-sas/</link>
		<comments>http://www.philihp.com/blog/2009/converting-a-date-to-a-datetime-in-sas/#comments</comments>
		<pubDate>Mon, 01 Jun 2009 21:01:10 +0000</pubDate>
		<dc:creator>philihp</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Dates]]></category>
		<category><![CDATA[SAS]]></category>

		<guid isPermaLink="false">http://www.philihp.com/blog/?p=430</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>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.</p>
<p>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).</p>
<pre>data _null_;
  d='29FEB1984'd;
  put d date.;
  <strong>dt=d*24*60*60;</strong>
  put dt datetime.;
  <strong>d=dt/24/60/60;</strong>
  put d date.;
run;</pre>
<p>But unless someone deals with dates every day, it might not be obvious to them by reading this code what those <a href="http://en.wikipedia.org/wiki/Magic_number_(programming)">magic numbers</a> are doing.</p>
<p>Another method would be to use <a href="http://support.sas.com/onlinedoc/913/getDoc/en/lrdict.hlp/a000199354.htm">put</a>() and <a href="http://support.sas.com/onlinedoc/913/getDoc/en/lrdict.hlp/a000180357.htm">input</a>() using functions to go back and fourth.</p>
<pre>data _null_;
  d='29FEB1984'd;
  put d date.;
  <strong>dt=input(put( d  ,date7.) || ':00:00:00', datetime.);</strong>
  put dt datetime.;
  <strong>d =input(substr(put(dt,datetime.),1,7),date7.);</strong>
  put d date.;
run;</pre>
<p>But I don&#8217;t consider this any better. It&#8217;s wordy. Functions are nested within functions. And you still have a magic number for the call to substr. Also, since it isn&#8217;t uncommon for SAS datasets to get into the billions of rows, this probably isn&#8217;t the fastest way to do it.</p>
<p>SAS has built-in functions called <a href="http://support.sas.com/onlinedoc/913/getDoc/en/lrdict.hlp/a000179419.htm">dhms</a>() (mnemonic: date,hour,minute,second) and <a href="http://support.sas.com/onlinedoc/913/getDoc/en/lrdict.hlp/a000245883.htm">datepart</a>() that should make things a lot easier.</p>
<pre>data _null_;
  d='29FEB1984'd;
  put d date.;
  <strong>dt=dhms(d,0,0,0);</strong>
  put dt datetime.;
  <strong>d =datepart(dt);</strong>
  put d date.;
run;</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.philihp.com/blog/2009/converting-a-date-to-a-datetime-in-sas/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
