<?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 malfunction in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-malfunction/m-p/230248#M41739</link>
    <description>&lt;P&gt;Attaching&amp;nbsp;some sample data, and&amp;nbsp;preferred&amp;nbsp;outcome would help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Lag() is quite efficient, if the data is pre-sorted. But requires a little bit of coding.&lt;/P&gt;&lt;P&gt;SQL for me is easier to understand/code, and if you have the&amp;nbsp;balance data, you could just left join the balance table with itself.&lt;/P&gt;</description>
    <pubDate>Fri, 16 Oct 2015 09:13:08 GMT</pubDate>
    <dc:creator>LinusH</dc:creator>
    <dc:date>2015-10-16T09:13:08Z</dc:date>
    <item>
      <title>Lag function malfunction</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-malfunction/m-p/230246#M41738</link>
      <description>&lt;DIV class="post-text"&gt;&lt;P&gt;I am using SAS Base V9.4&lt;/P&gt;&lt;P&gt;I have daily balances each of my customers and i want to find out if they increases or decreases their balance from one day to another. So I wrote the following code but i am receiving following Error.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;"ERROR 72-185: The LAG function call has too many arguments."&lt;/P&gt;&lt;P&gt;IF LAG(CUST_ID,1)=CUST_ID AND (BALANCE - LAG(BALANCE,1))&amp;gt;0 THEN FLAG="FLAG=1"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;what could be the problem?&lt;/P&gt;&lt;/DIV&gt;&lt;DIV class="post-taglist"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class="post-taglist"&gt;i have &amp;nbsp;to calculate differences while grouping &amp;nbsp;the same cuatomer&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Fri, 16 Oct 2015 08:48:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-malfunction/m-p/230246#M41738</guid>
      <dc:creator>omerzeybek</dc:creator>
      <dc:date>2015-10-16T08:48:02Z</dc:date>
    </item>
    <item>
      <title>Re: Lag function malfunction</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-malfunction/m-p/230248#M41739</link>
      <description>&lt;P&gt;Attaching&amp;nbsp;some sample data, and&amp;nbsp;preferred&amp;nbsp;outcome would help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Lag() is quite efficient, if the data is pre-sorted. But requires a little bit of coding.&lt;/P&gt;&lt;P&gt;SQL for me is easier to understand/code, and if you have the&amp;nbsp;balance data, you could just left join the balance table with itself.&lt;/P&gt;</description>
      <pubDate>Fri, 16 Oct 2015 09:13:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-malfunction/m-p/230248#M41739</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2015-10-16T09:13:08Z</dc:date>
    </item>
    <item>
      <title>Re: Lag function malfunction</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-malfunction/m-p/230251#M41741</link>
      <description>&lt;P&gt;You haven't opened the doc to look up the lag function have you?&lt;/P&gt;&lt;P&gt;It only takes one argument.&amp;nbsp; Which happens to be what the error message tells you.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Assuming the data is sorted by CUSTOMER DATE:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data HAVE;
infile cards missover;
  input CUSTOMER $ BALANCE ;
  cards;
1 34 
1 45
1 65
2 67 
2 78
3 34         
3 35          
3 33          
run;
data WANT;
  set HAVE;
  if      lag(BALANCE)&amp;gt;BALANCE and CUSTOMER=lag(CUSTOMER) then WHAT='Less';
  else if lag(BALANCE)=BALANCE and CUSTOMER=lag(CUSTOMER) then WHAT='Same';
  else if lag(BALANCE)&amp;lt;BALANCE and CUSTOMER=lag(CUSTOMER) then WHAT='More';
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Oct 2015 10:18:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-malfunction/m-p/230251#M41741</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2015-10-16T10:18:09Z</dc:date>
    </item>
    <item>
      <title>Re: Lag function malfunction</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-malfunction/m-p/230258#M41742</link>
      <description>&lt;P&gt;Besides solving the problem, here are a couple of tools you might want to learn. &amp;nbsp;Compare the DIF function to the LAG function. &amp;nbsp;More important, learn how to use a BY statement in a DATA step. &amp;nbsp;You will definitely need to know this going forward. &amp;nbsp;One approach:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;by customer;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;change = dif(balance);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;if first.customer=0 and change &amp;gt; 0 then flag='FLAG=1';&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This assumes that your data are sorted by CUSTOMER. &amp;nbsp;You can decide whether or not to drop CHANGE from the final data set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What did you want the value of FLAG to be? &amp;nbsp;"1" or "FLAG=1"? &amp;nbsp;Right now, you're getting the longer result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Good luck.&lt;/P&gt;</description>
      <pubDate>Fri, 16 Oct 2015 11:03:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-malfunction/m-p/230258#M41742</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2015-10-16T11:03:56Z</dc:date>
    </item>
  </channel>
</rss>

