<?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: dynamic lag values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/dynamic-lag-values/m-p/337151#M76523</link>
    <description>&lt;P&gt;Thanks for helping me understand issues with this code. I can fix each of the code issues that you pointed out. Instead i changed the sort order, created new variable to get away without lag&lt;/P&gt;</description>
    <pubDate>Wed, 01 Mar 2017 21:15:26 GMT</pubDate>
    <dc:creator>arunmmw</dc:creator>
    <dc:date>2017-03-01T21:15:26Z</dc:date>
    <item>
      <title>dynamic lag values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/dynamic-lag-values/m-p/337097#M76500</link>
      <description>&lt;P&gt;I have trouble understanding lag function in SAS. &amp;nbsp;In this sample code I want to update ‘Comm_Inc’ &amp;nbsp;and ‘Goal’ based on the previous month ‘Goal’.&lt;/P&gt;
&lt;P&gt;201610 ‘Goal’ is updated correctly with 15000+Comm_Income&lt;/P&gt;
&lt;P&gt;However 201611 and 201612 isnt updated with lag4 values of Goal. I can do this by looping through months multiple times. Can this can be done using lag option ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;DATA&lt;/STRONG&gt; temp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;INFILE DATALINES DSD;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;INPUT month_end $ group $ Goal Comm Comm_Inc RowNum;&lt;/P&gt;
&lt;P&gt;DATALINES;&lt;/P&gt;
&lt;P&gt;201610,East,15000,0.2,3000,1&lt;/P&gt;
&lt;P&gt;201610,North,20000,0.3,6000,2&lt;/P&gt;
&lt;P&gt;201610,South,10000,0.1,1000,3&lt;/P&gt;
&lt;P&gt;201610,West,22000,0.1,2200,4&lt;/P&gt;
&lt;P&gt;201611,East,,0.2,,5&lt;/P&gt;
&lt;P&gt;201611,North,,0.3,,6&lt;/P&gt;
&lt;P&gt;201611,South,,0.1,,7&lt;/P&gt;
&lt;P&gt;201611,West,,0.1,,8&lt;/P&gt;
&lt;P&gt;201612,East,,0.2,,9&lt;/P&gt;
&lt;P&gt;201612,North,,0.3,,10&lt;/P&gt;
&lt;P&gt;201612,South,,0.1,,11&lt;/P&gt;
&lt;P&gt;201612,West,,0.1,,12&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;proc&lt;/STRONG&gt; &lt;STRONG&gt;sort&lt;/STRONG&gt; data=temp;&lt;/P&gt;
&lt;P&gt;by month_end group;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&amp;nbsp; temp;&lt;/P&gt;
&lt;P&gt;set temp;&lt;/P&gt;
&lt;P&gt;by month_end group;&lt;/P&gt;
&lt;P&gt;retain Goal;&lt;/P&gt;
&lt;P&gt;if month_end = &lt;STRONG&gt;201610&lt;/STRONG&gt;&amp;nbsp; then do;&lt;/P&gt;
&lt;P&gt;Goal=Goal;&lt;/P&gt;
&lt;P&gt;Comm_Inc = Goal*Comm;&lt;/P&gt;
&lt;P&gt;Goal = Goal+Comm_Inc;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;else do;&lt;/P&gt;
&lt;P&gt;Goal=lag4(Goal);&lt;/P&gt;
&lt;P&gt;Comm_Inc = Goal*Comm;&lt;/P&gt;
&lt;P&gt;Goal = Goal+Comm_Inc;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Final Expected Output:&lt;/P&gt;
&lt;TABLE width="320"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="79"&gt;
&lt;P&gt;month_end&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="43"&gt;
&lt;P&gt;group&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="44"&gt;
&lt;P&gt;Goal&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="83"&gt;
&lt;P&gt;Comm&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="72"&gt;
&lt;P&gt;Comm_Inc&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="79"&gt;
&lt;P&gt;201610&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="43"&gt;
&lt;P&gt;East&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="44"&gt;
&lt;P&gt;18000&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="83"&gt;
&lt;P&gt;0.2&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="72"&gt;
&lt;P&gt;$3,000&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="79"&gt;
&lt;P&gt;201610&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="43"&gt;
&lt;P&gt;North&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="44"&gt;
&lt;P&gt;26000&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="83"&gt;
&lt;P&gt;0.3&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="72"&gt;
&lt;P&gt;$6,000&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="79"&gt;
&lt;P&gt;201610&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="43"&gt;
&lt;P&gt;South&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="44"&gt;
&lt;P&gt;11000&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="83"&gt;
&lt;P&gt;0.1&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="72"&gt;
&lt;P&gt;$1,000&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="79"&gt;
&lt;P&gt;201610&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="43"&gt;
&lt;P&gt;West&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="44"&gt;
&lt;P&gt;24200&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="83"&gt;
&lt;P&gt;0.1&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="72"&gt;
&lt;P&gt;$2,200&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="79"&gt;
&lt;P&gt;201611&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="43"&gt;
&lt;P&gt;East&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="44"&gt;
&lt;P&gt;21600&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="83"&gt;
&lt;P&gt;0.2&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="72"&gt;
&lt;P&gt;$3,600&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="79"&gt;
&lt;P&gt;201611&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="43"&gt;
&lt;P&gt;North&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="44"&gt;
&lt;P&gt;33800&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="83"&gt;
&lt;P&gt;0.3&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="72"&gt;
&lt;P&gt;$7,800&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="79"&gt;
&lt;P&gt;201611&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="43"&gt;
&lt;P&gt;South&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="44"&gt;
&lt;P&gt;12100&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="83"&gt;
&lt;P&gt;0.1&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="72"&gt;
&lt;P&gt;$1,100&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="79"&gt;
&lt;P&gt;201611&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="43"&gt;
&lt;P&gt;West&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="44"&gt;
&lt;P&gt;26620&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="83"&gt;
&lt;P&gt;0.1&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="72"&gt;
&lt;P&gt;$2,420&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="79"&gt;
&lt;P&gt;201612&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="43"&gt;
&lt;P&gt;East&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="44"&gt;
&lt;P&gt;25920&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="83"&gt;
&lt;P&gt;0.2&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="72"&gt;
&lt;P&gt;$4,320&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="79"&gt;
&lt;P&gt;201612&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="43"&gt;
&lt;P&gt;North&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="44"&gt;
&lt;P&gt;43940&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="83"&gt;
&lt;P&gt;0.3&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="72"&gt;
&lt;P&gt;$10,140&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="79"&gt;
&lt;P&gt;201612&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="43"&gt;
&lt;P&gt;South&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="44"&gt;
&lt;P&gt;13310&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="83"&gt;
&lt;P&gt;0.1&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="72"&gt;
&lt;P&gt;$1,210&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="79"&gt;
&lt;P&gt;201612&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="43"&gt;
&lt;P&gt;West&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="44"&gt;
&lt;P&gt;29282&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="83"&gt;
&lt;P&gt;0.1&lt;/P&gt;
&lt;/TD&gt;
&lt;TD width="72"&gt;
&lt;P&gt;$2,662&lt;/P&gt;
&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Mar 2017 18:34:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/dynamic-lag-values/m-p/337097#M76500</guid>
      <dc:creator>arunmmw</dc:creator>
      <dc:date>2017-03-01T18:34:13Z</dc:date>
    </item>
    <item>
      <title>Re: dynamic lag values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/dynamic-lag-values/m-p/337118#M76511</link>
      <description>&lt;P&gt;Yes, LAG is more complicated than it looks.&amp;nbsp; In particular, you need to execute it on every observation for it to get the results you expect.&amp;nbsp; It's hardly ever correct to use LAG inside a DO group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would recommend two sets of changes.&amp;nbsp; First, do not replace GOAL, the existing variable.&amp;nbsp; Instead, create a new variable CALCULATED_GOAL (or whatever you want to call it) based on GOAL and your other variables.&amp;nbsp; Because each time the SET statement brings in a new observation, it wipes out your previous GOAL value.&amp;nbsp; Use a RETAIN statement to preserve the value of that new variable as you move from observation to observation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second, change the order.&amp;nbsp; Instead of sorting by MONTH_END GROUP, sort by GROUP MONTH_END.&amp;nbsp; In that way, you can process te observations sequentially and just need to examine first.group and last.group to detect when GROUP changes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Good luck.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Mar 2017 19:52:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/dynamic-lag-values/m-p/337118#M76511</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-03-01T19:52:33Z</dc:date>
    </item>
    <item>
      <title>Re: dynamic lag values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/dynamic-lag-values/m-p/337142#M76520</link>
      <description>&lt;P&gt;There are a couple problems, both related to the fact that&amp;nbsp;&lt;EM&gt;&lt;STRONG&gt;LAGs are queue-management functions, not "lookback" like in excel.&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Using lag function inside an IF condition.&amp;nbsp; This means that the 4-deep lag queue (from lag4(goal)) is not updated with every observation.&amp;nbsp; Typically you want the queue updated even if you are not using the result.&amp;nbsp; This could be easily fixed.&lt;/LI&gt;
&lt;LI&gt;More problematic, I think, is the fact the you have&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;goal=lag4(goal)&amp;nbsp;;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR /&gt;which immediately updates the 4-item queue of goal values.&amp;nbsp; ... followed later by&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;goal=goal+comm_inc&lt;BR /&gt;which updates the value of goal in the program data vector, &lt;EM&gt;&lt;STRONG&gt;but does not update the corresponding element in the lag4 queue.&lt;/STRONG&gt;&lt;/EM&gt;&amp;nbsp; As a result that updated value of goal is not available to subsequent lag4 executions.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Mar 2017 20:49:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/dynamic-lag-values/m-p/337142#M76520</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-03-01T20:49:32Z</dc:date>
    </item>
    <item>
      <title>Re: dynamic lag values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/dynamic-lag-values/m-p/337150#M76522</link>
      <description>This is the solution I got from your recommendation. Thanks&lt;BR /&gt;&lt;BR /&gt;proc sort data=temp;&lt;BR /&gt;by group month_end ;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;data  temp1;&lt;BR /&gt;set temp;&lt;BR /&gt;by group month_end;&lt;BR /&gt;retain Calculated_Goal Comm_Inc;&lt;BR /&gt;if first.group then do;&lt;BR /&gt;Comm_Inc = Goal*Comm;&lt;BR /&gt;Calculated_Goal = Goal+Comm_Inc;&lt;BR /&gt;Goal = Calculated_Goal;&lt;BR /&gt;end;&lt;BR /&gt;else do;&lt;BR /&gt;Comm_Inc = Calculated_Goal*Comm;&lt;BR /&gt;Calculated_Goal = Calculated_Goal+Comm_Inc;&lt;BR /&gt;end;&lt;BR /&gt;&lt;BR /&gt;run;</description>
      <pubDate>Wed, 01 Mar 2017 21:08:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/dynamic-lag-values/m-p/337150#M76522</guid>
      <dc:creator>arunmmw</dc:creator>
      <dc:date>2017-03-01T21:08:46Z</dc:date>
    </item>
    <item>
      <title>Re: dynamic lag values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/dynamic-lag-values/m-p/337151#M76523</link>
      <description>&lt;P&gt;Thanks for helping me understand issues with this code. I can fix each of the code issues that you pointed out. Instead i changed the sort order, created new variable to get away without lag&lt;/P&gt;</description>
      <pubDate>Wed, 01 Mar 2017 21:15:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/dynamic-lag-values/m-p/337151#M76523</guid>
      <dc:creator>arunmmw</dc:creator>
      <dc:date>2017-03-01T21:15:26Z</dc:date>
    </item>
    <item>
      <title>Re: dynamic lag values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/dynamic-lag-values/m-p/337721#M76748</link>
      <description>&lt;P&gt;This can be done with lag, even when sorted by monthend/group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's not initialy intutitive, but it does allow for a very simple program:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=oldgoal);
  oldgoal=lag3(goal);
  set temp;
  if _n_&amp;lt;=4 then goal=goal+comm_inc;
  else do;
    comm_inc=comm*oldgoal;
    goal=oldgoal+comm_inc;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It has three main features:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;It takes&amp;nbsp;the lag of goal and puts it in another var (oldgoal), as per &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;'s suggestion.&lt;/LI&gt;
&lt;LI&gt;Because you were modifying the GOAL variable after retrieving its value, the lag queue was not getting the modified value.&amp;nbsp; To remedy this&amp;nbsp;I moved the LAG function to precede the SET statement.&amp;nbsp; This allowed it to work from the modified values (but note it's the modified value of the PRIOR obs, which is about to be replaced by the SET statement). &lt;/LI&gt;
&lt;LI&gt;Instead of lag4 (for 4 regions: east/north/south/west), I have to use LAG3.&amp;nbsp; This is because I placed the lag function prior to the SET statement, so I need to go back one less entry.&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Fri, 03 Mar 2017 06:35:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/dynamic-lag-values/m-p/337721#M76748</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-03-03T06:35:48Z</dc:date>
    </item>
  </channel>
</rss>

