<?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>I do things, and then I tell the internet about them.</description>
	<lastBuildDate>Mon, 06 Feb 2012 05:40:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>How to get the Last Day of a Month in SAS</title>
		<link>http://www.philihp.com/blog/2011/how-to-get-the-last-day-of-a-month-in-sas/</link>
		<comments>http://www.philihp.com/blog/2011/how-to-get-the-last-day-of-a-month-in-sas/#comments</comments>
		<pubDate>Tue, 20 Sep 2011 23:15:39 +0000</pubDate>
		<dc:creator>philihp</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Dates]]></category>
		<category><![CDATA[intnx]]></category>
		<category><![CDATA[magic]]></category>
		<category><![CDATA[SAS]]></category>

		<guid isPermaLink="false">http://www.philihp.com/blog/?p=713</guid>
		<description><![CDATA[SAS has a really neat function called intnx, which will increment a date to the next of an interval. For example, if you have a date (any date. maybe the current date?), you can get the date of the first day of the next month by doing this: data _null_; d = '11JUN2011'd; format d [...]]]></description>
			<content:encoded><![CDATA[<p>SAS has a really neat function called <a href="http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000212700.htm" target="_blank">intnx</a>, which will increment a date to the next of an interval.</p>
<p>For example, if you have a date (any date. maybe the current date?), you can get the date of the first day of the next month by doing this:</p>

<div class="wp_syntax"><div class="code"><pre class="sas" style="font-family:monospace;"><span style="color: #000080; font-weight: bold;">data</span> <span style="color: #0000ff;">_null_</span>;
  d = <span style="color: #a020f0;">'11JUN2011'</span>d;
  <span style="color: #0000ff;">format</span> d date9.;
  <span style="color: #0000ff;">put</span> d;
  d = <span style="color: #0000ff;">intnx</span><span style="color: #66cc66;">&#40;</span><span style="color: #a020f0;">'month'</span>,d,<span style="color: #2e8b57; font-weight: bold;">1</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #0000ff;">put</span> d;
<span style="color: #000080; font-weight: bold;">run</span>;</pre></div></div>

<p>Which outputs</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">11JUN2011
01JUL2011</pre></div></div>

<p>But to get the &#8220;Last Day&#8221; of the current month, you have to be clever. The last day of the current month is the day before the first day of the next month. SAS Dates are internally stored as a number of days since some point, so just subtract one from it.</p>

<div class="wp_syntax"><div class="code"><pre class="sas" style="font-family:monospace;"><span style="color: #000080; font-weight: bold;">data</span> <span style="color: #0000ff;">_null_</span>;
  d = <span style="color: #a020f0;">'11JUN2011'</span>d;
  <span style="color: #0000ff;">format</span> d date9.;
  <span style="color: #0000ff;">put</span> d;
  d = <span style="color: #0000ff;">intnx</span><span style="color: #66cc66;">&#40;</span><span style="color: #a020f0;">'month'</span>,d,<span style="color: #2e8b57; font-weight: bold;">1</span><span style="color: #66cc66;">&#41;</span>-<span style="color: #2e8b57; font-weight: bold;">1</span>;
  <span style="color: #0000ff;">put</span> d;
<span style="color: #000080; font-weight: bold;">run</span>;</pre></div></div>

<p>Which outputs</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">11JUN2011
30JUN2011</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.philihp.com/blog/2011/how-to-get-the-last-day-of-a-month-in-sas/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<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 [...]]]></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>2</slash:comments>
		</item>
	</channel>
</rss>

