<?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: Lag function with if condition in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-with-if-condition/m-p/317863#M69621</link>
    <description>&lt;P&gt;LAG is trickier than it looks. &amp;nbsp;It seems that you want the value of NCBAL&amp;nbsp;from the previous observation, but that's not what LAG does. &amp;nbsp;It actually retrieves the value from the last time that the LAG function executed. &amp;nbsp;To get the value from the previous observation, make sure that LAG executes every time. &amp;nbsp;For example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Data in1;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;set in;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;by seg;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;tempvar = lag(ncbal);&lt;/P&gt;
&lt;P&gt;drop tempvar;&lt;BR /&gt;&lt;SPAN&gt;if first.seg then count=0;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;count+1;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;if count = 2 then sum_ncbal= tempvar;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 09 Dec 2016 11:16:14 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2016-12-09T11:16:14Z</dc:date>
    <item>
      <title>Lag function with if condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-with-if-condition/m-p/317846#M69612</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below is the program i am running on my SAS Eg. In the last data step i am facing a problem.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Why the lag function is not assigning the value to sum_ncbal whenever the condition is true?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data in;&lt;BR /&gt;input seg $ period ncbal;&lt;BR /&gt;datalines;&lt;BR /&gt;A 1 10&lt;BR /&gt;A 2 20&lt;BR /&gt;A 3 30&lt;BR /&gt;A 4 40&lt;BR /&gt;A 5 50&lt;BR /&gt;A 6 60&lt;BR /&gt;A 7 70&lt;BR /&gt;A 8 80&lt;BR /&gt;A 9 90&lt;BR /&gt;A 10 100&lt;BR /&gt;A 11 110&lt;BR /&gt;A 12 120&lt;BR /&gt;B 1 1&lt;BR /&gt;B 2 2&lt;BR /&gt;B 3 3&lt;BR /&gt;B 4 4&lt;BR /&gt;B 5 5&lt;BR /&gt;B 6 6&lt;BR /&gt;B 7 7&lt;BR /&gt;B 8 8&lt;BR /&gt;B 9 9&lt;BR /&gt;B 10 10&lt;BR /&gt;B 11 11&lt;BR /&gt;B 12 12&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc sort data=in ;&lt;BR /&gt;by seg descending period;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;Data in1;&lt;BR /&gt;set in;&lt;BR /&gt;by seg;&lt;BR /&gt;if first.seg then count=0;&lt;BR /&gt;count+1;&lt;BR /&gt;if count = 2 then sum_ncbal= lag(ncbal);&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Dec 2016 10:27:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-with-if-condition/m-p/317846#M69612</guid>
      <dc:creator>e044800</dc:creator>
      <dc:date>2016-12-09T10:27:41Z</dc:date>
    </item>
    <item>
      <title>Re: Lag function with if condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-with-if-condition/m-p/317863#M69621</link>
      <description>&lt;P&gt;LAG is trickier than it looks. &amp;nbsp;It seems that you want the value of NCBAL&amp;nbsp;from the previous observation, but that's not what LAG does. &amp;nbsp;It actually retrieves the value from the last time that the LAG function executed. &amp;nbsp;To get the value from the previous observation, make sure that LAG executes every time. &amp;nbsp;For example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Data in1;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;set in;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;by seg;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;tempvar = lag(ncbal);&lt;/P&gt;
&lt;P&gt;drop tempvar;&lt;BR /&gt;&lt;SPAN&gt;if first.seg then count=0;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;count+1;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;if count = 2 then sum_ncbal= tempvar;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Dec 2016 11:16:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-with-if-condition/m-p/317863#M69621</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-12-09T11:16:14Z</dc:date>
    </item>
    <item>
      <title>Re: Lag function with if condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-with-if-condition/m-p/317864#M69622</link>
      <description>&lt;P&gt;The lag() functions only fill their queue when called. Calling them conditionally usually results in arbitrary return values.&lt;/P&gt;
&lt;P&gt;Do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data in1;
set in;
by seg;
if first.seg
then count = 1;
else count + 1;
sum_ncbal = lag(ncbal);
if count = 2 then sum_ncbal = .;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;if you already have values in sum_ncbal that need to be preserved, do this&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data in1;
set in;
by seg;
if first.seg
then count = 1;
else count + 1;
lag_ncbal = lag(ncbal);
if count = 2 then sum_ncbal = lag_ncbal;
drop lag_ncbal;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 09 Dec 2016 11:17:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-with-if-condition/m-p/317864#M69622</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-12-09T11:17:50Z</dc:date>
    </item>
  </channel>
</rss>

