<?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>Dammit Jim! &#187; caution</title>
	<atom:link href="http://scott.sherrillmix.com/blog/tag/caution/feed/" rel="self" type="application/rss+xml" />
	<link>http://scott.sherrillmix.com/blog</link>
	<description>I'm a biologist not a...</description>
	<lastBuildDate>Mon, 06 Feb 2012 05:19:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>SAS lag problems</title>
		<link>http://scott.sherrillmix.com/blog/programmer/sas-lag-problems/</link>
		<comments>http://scott.sherrillmix.com/blog/programmer/sas-lag-problems/#comments</comments>
		<pubDate>Wed, 27 Sep 2006 23:07:23 +0000</pubDate>
		<dc:creator>ScottS-M</dc:creator>
				<category><![CDATA[Programmer]]></category>
		<category><![CDATA[SAS]]></category>
		<category><![CDATA[Statistician]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[caution]]></category>
		<category><![CDATA[conditional]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[difference]]></category>
		<category><![CDATA[if statement]]></category>
		<category><![CDATA[lag]]></category>
		<category><![CDATA[problem]]></category>

		<guid isPermaLink="false">http://scott.sherrillmix.com/blog/programmer/sas-lag-problems/</guid>
		<description><![CDATA[I just found an interesting (that's interesting in the 'I just spent an hour debugging that?' sense) characteristic in SAS. If you have a variable, x, and are using the lag of x, do NOT put the lag(x) inside a conditional statement. This can apparently cause some pretty strange results. It is probably easier to [...]]]></description>
			<content:encoded><![CDATA[<p>I just found an interesting (that's interesting in the 'I just spent an hour debugging <em>that</em>?' sense) characteristic in SAS. If you have a variable, <code>x</code>, and are using the <code>lag</code> of <code>x</code>, do NOT put the <code>lag(x)</code> inside a conditional statement. This can apparently cause some pretty strange results. It is probably easier to see through code than to explain:</p>

<div class="syntax_hilite"><span class="langName">SAS:</span><br /><div id="sas-4">
<div class="sas"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000080; font-weight: bold;">data</span> test;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">input</span> id$<span style="color: #2e8b57; font-weight: bold;color:#800000;">1</span> <span style="color: #0000ff;">x</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">cards;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; a <span style="color: #2e8b57; font-weight: bold;color:#800000;">1</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; a <span style="color: #2e8b57; font-weight: bold;color:#800000;">2</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; a <span style="color: #2e8b57; font-weight: bold;color:#800000;">3</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; b <span style="color: #2e8b57; font-weight: bold;color:#800000;">12</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; b <span style="color: #2e8b57; font-weight: bold;color:#800000;">13</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; b <span style="color: #2e8b57; font-weight: bold;color:#800000;">15</span></div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000080; font-weight: bold;">run</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000080; font-weight: bold;">data</span> test;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">set</span> test;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; lagid=<span style="color: #0000ff;">lag</span><span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #0000ff;">if</span> lagid=id <span style="color: #0000ff;">then</span> <span style="color: #0000ff;">do</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; lagx=<span style="color: #0000ff;">lag</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">x</span><span style="color: #66cc66;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; duration=x-lagx;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #0000ff;">end</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000080; font-weight: bold;">run</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000080; font-weight: bold;">proc print</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000080; font-weight: bold;">run</span>; </div></li></ol></div>
</div></div><br />

<p>And here are the results:</p>

<div class="syntax_hilite"><span class="langName">SAS:</span><br /><div id="sas-5">
<div class="sas"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Obs&nbsp; &nbsp; id&nbsp; &nbsp; &nbsp;<span style="color: #0000ff;">x</span>&nbsp; &nbsp; lagid&nbsp; &nbsp; lagx&nbsp; &nbsp; duration</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #2e8b57; font-weight: bold;color:#800000;">1</span>&nbsp; &nbsp; &nbsp;a&nbsp; &nbsp; &nbsp; <span style="color: #2e8b57; font-weight: bold;color:#800000;">1</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #2e8b57; font-weight: bold;color:#800000;">2</span>&nbsp; &nbsp; &nbsp;a&nbsp; &nbsp; &nbsp; <span style="color: #2e8b57; font-weight: bold;color:#800000;">2</span>&nbsp; &nbsp; &nbsp; a&nbsp; &nbsp; &nbsp; &nbsp; .&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #2e8b57; font-weight: bold;color:#800000;">3</span>&nbsp; &nbsp; &nbsp;a&nbsp; &nbsp; &nbsp; <span style="color: #2e8b57; font-weight: bold;color:#800000;">3</span>&nbsp; &nbsp; &nbsp; a&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #2e8b57; font-weight: bold;color:#800000;">2</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #2e8b57; font-weight: bold;color:#800000;">1</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #2e8b57; font-weight: bold;color:#800000;">4</span>&nbsp; &nbsp; &nbsp;b&nbsp; &nbsp; &nbsp;<span style="color: #2e8b57; font-weight: bold;color:#800000;">12</span>&nbsp; &nbsp; &nbsp; a&nbsp; &nbsp; &nbsp; &nbsp; .&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #2e8b57; font-weight: bold;color:#800000;">5</span>&nbsp; &nbsp; &nbsp;b&nbsp; &nbsp; &nbsp;<span style="color: #2e8b57; font-weight: bold;color:#800000;">13</span>&nbsp; &nbsp; &nbsp; b&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #2e8b57; font-weight: bold;color:#800000;">3</span>&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #2e8b57; font-weight: bold;color:#800000;">10</span></div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #2e8b57; font-weight: bold;color:#800000;">6</span>&nbsp; &nbsp; &nbsp;b&nbsp; &nbsp; &nbsp;<span style="color: #2e8b57; font-weight: bold;color:#800000;">15</span>&nbsp; &nbsp; &nbsp; b&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #2e8b57; font-weight: bold;color:#800000;">13</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #2e8b57; font-weight: bold;color:#800000;">2</span> </div></li></ol></div>
</div></div><br />

<p>There are obviously some strange things going on here. I have no idea what is going on in the 2nd row of the results where <code>lagx</code> is missing when it should be 1. And in the 5th row, <code>lagx</code> is the 3 from 2 rows above when it should be 12. Luckily, the solution is easy. Just execute the lag every time:</p>

<div class="syntax_hilite"><span class="langName">SAS:</span><br /><div id="sas-6">
<div class="sas"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000080; font-weight: bold;">data</span> test;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0000ff;">set</span> test;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; lagid=<span style="color: #0000ff;">lag</span><span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; lagx=<span style="color: #0000ff;">lag</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">x</span><span style="color: #66cc66;">&#41;</span>;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #0000ff;">if</span> lagid=id <span style="color: #0000ff;">then</span> <span style="color: #0000ff;">do</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; duration=x-lagx;</div></li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #0000ff;">end</span>;</div></li>
<li style="font-weight: bold;color:#26536A;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000080; font-weight: bold;">run</span>; </div></li></ol></div>
</div></div><br />

<p>Anyway, it took me two years to run into this so I guess this isn't a common mistake but if you're ever having trouble with a <code>lag</code>, check to make sure it is executed for every line of the data.</p>]]></content:encoded>
			<wfw:commentRss>http://scott.sherrillmix.com/blog/programmer/sas-lag-problems/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
	</channel>
</rss>

