<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Must I reset the value of lag() when using BY variables? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Must-I-reset-the-value-of-lag-when-using-BY-variables/m-p/160167#M31163</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You don't need lag.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;BY KEY ID;&lt;/P&gt;&lt;P&gt;if first.key then runs=0;&lt;/P&gt;&lt;P&gt;if first.id then runs+1;&lt;/P&gt;&lt;P&gt;if last.key then output;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 13 Aug 2014 22:14:48 GMT</pubDate>
    <dc:creator>data_null__</dc:creator>
    <dc:date>2014-08-13T22:14:48Z</dc:date>
    <item>
      <title>Must I reset the value of lag() when using BY variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Must-I-reset-the-value-of-lag-when-using-BY-variables/m-p/160166#M31162</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I would like to count the number of runs in a dataset, but I believe that my code has a hole in it.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I had thought to use:&lt;/P&gt;&lt;P&gt;proc sort data=stuff; by key id;&lt;/P&gt;&lt;P&gt;&amp;nbsp; run;&lt;/P&gt;&lt;P&gt;data runs;&lt;/P&gt;&lt;P&gt;&amp;nbsp; set stuff; by key;&lt;/P&gt;&lt;P&gt;&amp;nbsp; retain runs;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if first.key then runs=1;&lt;/P&gt;&lt;P&gt;&amp;nbsp; else if id ne lag(id) then runs+1;&lt;/P&gt;&lt;P&gt;&amp;nbsp; if last.key then output;&lt;/P&gt;&lt;P&gt;&amp;nbsp; run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;However, it appears that lag(id) is not set to missing when the BY variable key changes. Am I looking at this wrong? Is there a faster/better way to do it?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 13 Aug 2014 22:05:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Must-I-reset-the-value-of-lag-when-using-BY-variables/m-p/160166#M31162</guid>
      <dc:creator>dan_waldo</dc:creator>
      <dc:date>2014-08-13T22:05:04Z</dc:date>
    </item>
    <item>
      <title>Re: Must I reset the value of lag() when using BY variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Must-I-reset-the-value-of-lag-when-using-BY-variables/m-p/160167#M31163</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You don't need lag.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;BY KEY ID;&lt;/P&gt;&lt;P&gt;if first.key then runs=0;&lt;/P&gt;&lt;P&gt;if first.id then runs+1;&lt;/P&gt;&lt;P&gt;if last.key then output;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 13 Aug 2014 22:14:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Must-I-reset-the-value-of-lag-when-using-BY-variables/m-p/160167#M31163</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2014-08-13T22:14:48Z</dc:date>
    </item>
    <item>
      <title>Re: Must I reset the value of lag() when using BY variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Must-I-reset-the-value-of-lag-when-using-BY-variables/m-p/160168#M31164</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Just need a slight change&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data runs;&lt;/P&gt;&lt;P&gt;set stuff;&lt;/P&gt;&lt;P&gt;by key;&lt;/P&gt;&lt;P&gt;delta=id-lag(id);&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;if first.key then runs=1;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;if not first.key then do;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;if delta ne 0 then runs+1;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;end;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;if last.key then output;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 13 Aug 2014 22:29:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Must-I-reset-the-value-of-lag-when-using-BY-variables/m-p/160168#M31164</guid>
      <dc:creator>stat_sas</dc:creator>
      <dc:date>2014-08-13T22:29:55Z</dc:date>
    </item>
    <item>
      <title>Re: Must I reset the value of lag() when using BY variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Must-I-reset-the-value-of-lag-when-using-BY-variables/m-p/160169#M31165</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Never use lag() within an if condition as it only updates the lag value when executed - so the function needs to execute in every single iteration of the data step.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;and as already posted: there is no need to use lag() &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 13 Aug 2014 22:48:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Must-I-reset-the-value-of-lag-when-using-BY-variables/m-p/160169#M31165</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2014-08-13T22:48:43Z</dc:date>
    </item>
    <item>
      <title>Re: Must I reset the value of lag() when using BY variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Must-I-reset-the-value-of-lag-when-using-BY-variables/m-p/160170#M31166</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi:&lt;/P&gt;&lt;P&gt;&amp;nbsp; Patrick is correct. Please refer to this Tech Support note that demonstrates that using lag conditionally will probably result in undesired results: &lt;A href="http://support.sas.com/kb/24/665.html" title="http://support.sas.com/kb/24/665.html"&gt;24665 - Use the LAG function to conditionally carry information down a data set&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; If you run the code in that note, the approach that creates variable is the approach to use. As the note indicates (in the code), the LAG function stores values on the queue only when it is called, so you must call LAG unconditionally (outside the IF condition) to get the correct lagged value. And this paper has a longer explanation of the usage &lt;A href="http://support.sas.com/resources/papers/proceedings09/055-2009.pdf" title="http://support.sas.com/resources/papers/proceedings09/055-2009.pdf"&gt;http://support.sas.com/resources/papers/proceedings09/055-2009.pdf&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;cynthia&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 14 Aug 2014 01:27:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Must-I-reset-the-value-of-lag-when-using-BY-variables/m-p/160170#M31166</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2014-08-14T01:27:51Z</dc:date>
    </item>
    <item>
      <title>Re: Must I reset the value of lag() when using BY variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Must-I-reset-the-value-of-lag-when-using-BY-variables/m-p/160171#M31167</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;@Patrick - Thanks so much for your input on lag function. This is very helpful!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 14 Aug 2014 04:03:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Must-I-reset-the-value-of-lag-when-using-BY-variables/m-p/160171#M31167</guid>
      <dc:creator>stat_sas</dc:creator>
      <dc:date>2014-08-14T04:03:56Z</dc:date>
    </item>
    <item>
      <title>Re: Must I reset the value of lag() when using BY variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Must-I-reset-the-value-of-lag-when-using-BY-variables/m-p/160172#M31168</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks to everybody who replied -- this is very useful information.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 14 Aug 2014 18:21:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Must-I-reset-the-value-of-lag-when-using-BY-variables/m-p/160172#M31168</guid>
      <dc:creator>dan_waldo</dc:creator>
      <dc:date>2014-08-14T18:21:05Z</dc:date>
    </item>
    <item>
      <title>Re: Must I reset the value of lag() when using BY variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Must-I-reset-the-value-of-lag-when-using-BY-variables/m-p/160173#M31169</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;At the risk of being obnoxious, I dare say 'never say never' &lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://communities.sas.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;. I can't think of any practical usage of conditional lag(), except I have had an rather unique request from a customer. say I have some balance record for checking accounts, they want to know the amount of the LAST over-draft if there is an existing one. Of course, there are many ways to do it, but lag() is also handy.&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;STRONG style="color: navy; background: white; font-family: 'Courier New';"&gt;data&lt;/STRONG&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt; have;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="font-family: 'Courier New'; color: blue; background: white;"&gt;input&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt; balance @@;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="font-family: 'Courier New'; color: blue; background: white;"&gt;cards&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: #FFFFC0;"&gt;100 -120 200 210 25 -35 400 100 20 -200&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;STRONG style="color: navy; background: white; font-family: 'Courier New';"&gt;run&lt;/STRONG&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;STRONG style="color: navy; background: white; font-family: 'Courier New';"&gt;data&lt;/STRONG&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt; want;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="font-family: 'Courier New'; color: blue; background: white;"&gt;set&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt; have;&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="font-family: 'Courier New'; color: blue; background: white;"&gt;if&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt; balance &amp;lt;&lt;/SPAN&gt;&lt;STRONG style="color: teal; background: white; font-family: 'Courier New';"&gt;0&lt;/STRONG&gt; &lt;SPAN style="font-family: 'Courier New'; color: blue; background: white;"&gt;then&lt;/SPAN&gt;&lt;/P&gt;&lt;P style="margin-bottom: .0001pt;"&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; last_overdraft=lag(balance);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG style="color: navy; background: white; font-family: 'Courier New';"&gt;run&lt;/STRONG&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt;I guess the take-home message is that Being extremely cautious when using lag() under condition, use it only if you know what you are expecting. &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt;My2cents,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Courier New'; color: black; background: white;"&gt;Haikuo &lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 14 Aug 2014 18:35:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Must-I-reset-the-value-of-lag-when-using-BY-variables/m-p/160173#M31169</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2014-08-14T18:35:07Z</dc:date>
    </item>
    <item>
      <title>Re: Must I reset the value of lag() when using BY variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Must-I-reset-the-value-of-lag-when-using-BY-variables/m-p/160174#M31170</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;And just to pile on with more LAG anecdotes and literature, I offer this blog post:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="http://blogs.sas.com/content/sasdummy/2012/01/03/pitfalls-of-the-lag-function/" title="http://blogs.sas.com/content/sasdummy/2012/01/03/pitfalls-of-the-lag-function/"&gt; Pitfalls of the LAG function - The SAS Dummy&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Echoing the other responses: if thinking through LAG makes your head hurt (as it does mine) and you can avoid using it...do.&amp;nbsp; FIRST-dot and LAST-dot are powerful DATA step features that can solve many scenarios with elegance.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Chris&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 05 Sep 2014 12:01:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Must-I-reset-the-value-of-lag-when-using-BY-variables/m-p/160174#M31170</guid>
      <dc:creator>ChrisHemedinger</dc:creator>
      <dc:date>2014-09-05T12:01:14Z</dc:date>
    </item>
    <item>
      <title>Re: Must I reset the value of lag() when using BY variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Must-I-reset-the-value-of-lag-when-using-BY-variables/m-p/160175#M31171</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;No need for headaches for using lag it is more the common queue function&amp;nbsp; &lt;A href="http://en.wikipedia.org/wiki/Queue_(abstract_data_type"&gt;http://en.wikipedia.org/wiki/Queue_(abstract_data_type&lt;/A&gt;)&lt;/P&gt;&lt;P&gt;With the implementation of that you see some problems as of the pull/push and sizing. SAS development could have done this in a better way.&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 05 Sep 2014 12:16:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Must-I-reset-the-value-of-lag-when-using-BY-variables/m-p/160175#M31171</guid>
      <dc:creator>jakarman</dc:creator>
      <dc:date>2014-09-05T12:16:46Z</dc:date>
    </item>
  </channel>
</rss>

