<?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 in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/469159#M119957</link>
    <description>&lt;P&gt;Thanks I understand that the input data would help, just haven't have time as yet.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It looks like the code is matching all the missing values and thinking that its a match, when really its just that there is no value (blank).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 11 Jun 2018 10:27:30 GMT</pubDate>
    <dc:creator>Selli5</dc:creator>
    <dc:date>2018-06-11T10:27:30Z</dc:date>
    <item>
      <title>Lag Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/469111#M119936</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What is the easiest way to compare 30 variables (lag_debit1 to lag_debit30) against another 30 variables (lag_credit1 to lag_credit30) already created per account.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I only want the accounts which have an exact match between one or more.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks&lt;/P&gt;&lt;P&gt;Sally&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jun 2018 07:02:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/469111#M119936</guid>
      <dc:creator>Selli5</dc:creator>
      <dc:date>2018-06-11T07:02:17Z</dc:date>
    </item>
    <item>
      <title>Re: Lag Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/469113#M119937</link>
      <description>&lt;P&gt;Maybe the only things required is a loop, but because you forgot to post input data, the expected result and an explanation of what "compare" means, it is nearly impossible to suggest something solving your issue.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jun 2018 07:25:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/469113#M119937</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2018-06-11T07:25:53Z</dc:date>
    </item>
    <item>
      <title>Re: Lag Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/469115#M119939</link>
      <description>&lt;P&gt;Post test data in the form of a datastep, otherwise we are just guessing.&amp;nbsp; If you have a list of variables with a numeric suffix then you will be talking about arrays or lists of values, you can define as:&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  array lag_debit(30);
  array lag_credit(30);
  do i=1 to 30;
    ...
  end;
run;

/* Or use a list directly as */
data want;
  set have;
  low=min(of lag_debit:);
run;&lt;/PRE&gt;
&lt;P&gt;You can also remodel the data so you don't have a wide table, that would likely be far easier to manage and work with.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jun 2018 07:36:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/469115#M119939</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-06-11T07:36:56Z</dc:date>
    </item>
    <item>
      <title>Re: Lag Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/469132#M119948</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need a bit more help.&amp;nbsp; If I use the code suggested, it returns 100% match, however this is not true. What do I need to change?&lt;/P&gt;&lt;P&gt;thks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;&amp;nbsp; set rule7;&lt;BR /&gt;&amp;nbsp; array lag_debit(30);&lt;BR /&gt;&amp;nbsp; array lag_credit(30);&lt;BR /&gt;&amp;nbsp; do i=1 to 30;&lt;BR /&gt;if lag_debit(30) = lag_credit(30) then match=1;&lt;BR /&gt;&amp;nbsp; end;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jun 2018 08:45:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/469132#M119948</guid>
      <dc:creator>Selli5</dc:creator>
      <dc:date>2018-06-11T08:45:46Z</dc:date>
    </item>
    <item>
      <title>Re: Lag Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/469140#M119950</link>
      <description>&lt;P&gt;Your code compares the 30th variables 30 times. Using "i" instead of "30" in the if-statement should help.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do i=1 to 30;
  if lag_debit(i) = lag_credit(i) then match=1;
end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 11 Jun 2018 08:52:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/469140#M119950</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2018-06-11T08:52:26Z</dc:date>
    </item>
    <item>
      <title>Re: Lag Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/469142#M119951</link>
      <description>&lt;P&gt;Again, &lt;U&gt;&lt;STRONG&gt;seeing test data in a datastep&lt;/STRONG&gt;&lt;/U&gt; really helps us understand your question, and showing what you want out.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want;
  set rule7;
  array lag_debit{30};
  array lag_credit{30};&lt;BR /&gt;  array results{30};
  do i=1 to 30;
    results{i}=ifn(lag_debit{i}=lag_credit{i},1,0);
  end;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;You will note (use of code window, its the {i} above post area), instead of putting 30 in the if part, i use the inceremental variable i, so each element is checked rather than just element 30.&amp;nbsp; Also, if you want a result for each pairing you need a result for each pairing, hence another array, otherwise you will get 1 if any of lag_debit{i} matches the same element in credit.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just to add, I find using curly braces around array's makes it easier to spot them amongst function calls, which is why I change them in the code.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jun 2018 08:54:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/469142#M119951</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-06-11T08:54:25Z</dc:date>
    </item>
    <item>
      <title>Re: Lag Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/469157#M119956</link>
      <description>&lt;P&gt;Thanks but it looks like its incorrectly matching missing lag_debits to missing lag_credits.&amp;nbsp; How can i specify that I only want matches when there is a value present?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jun 2018 10:24:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/469157#M119956</guid>
      <dc:creator>Selli5</dc:creator>
      <dc:date>2018-06-11T10:24:42Z</dc:date>
    </item>
    <item>
      <title>Re: Lag Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/469159#M119957</link>
      <description>&lt;P&gt;Thanks I understand that the input data would help, just haven't have time as yet.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It looks like the code is matching all the missing values and thinking that its a match, when really its just that there is no value (blank).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jun 2018 10:27:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/469159#M119957</guid>
      <dc:creator>Selli5</dc:creator>
      <dc:date>2018-06-11T10:27:30Z</dc:date>
    </item>
    <item>
      <title>Re: Lag Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/469161#M119958</link>
      <description>&lt;P&gt;Add a check to the condition for missing. Compare with . (representation of a missing numeric value), or use the missing() function.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jun 2018 10:28:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/469161#M119958</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-06-11T10:28:38Z</dc:date>
    </item>
    <item>
      <title>Re: Lag Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/469164#M119959</link>
      <description>&lt;P&gt;None of us have time, we are only volunteering on here - we don't work for SAS.&amp;nbsp; As such we have had this back and forth wasting time, where test data up front would have been quicker.&lt;/P&gt;
&lt;PRE&gt;data want;
  set rule7;
  array lag_debit{30};
  array lag_credit{30};&lt;BR /&gt;  array results{30};
  do i=1 to 30;
    results{i}=ifn(lag_debit{i}=lag_credit{i} and lag_debit{i} ne .,1,0);
  end;
run;&lt;/PRE&gt;</description>
      <pubDate>Mon, 11 Jun 2018 10:37:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/469164#M119959</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-06-11T10:37:09Z</dc:date>
    </item>
    <item>
      <title>Re: Lag Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/469175#M119960</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/134523"&gt;@Selli5&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks I understand that the input data would help, just haven't have time as yet.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Then your issue can't be urgent at all. Otherwise, you would make sure to help &lt;EM&gt;us&lt;/EM&gt; as much as you can in helping &lt;EM&gt;you&lt;/EM&gt;.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jun 2018 11:18:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/469175#M119960</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-06-11T11:18:24Z</dc:date>
    </item>
    <item>
      <title>Re: Lag Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/469489#M120114</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have attached some input data and code below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to identify when there is an exact match between any lag_debits to any lag_credit (and exclude blanks).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So in this case account number 10002 should produce a match on the $2100 amount and account number 10003 should produce a match on $2450.10.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jun 2018 02:32:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/469489#M120114</guid>
      <dc:creator>Selli5</dc:creator>
      <dc:date>2018-06-12T02:32:38Z</dc:date>
    </item>
    <item>
      <title>Re: Lag Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/469532#M120131</link>
      <description>&lt;P&gt;So this has nothing to do with arrays at all then.&amp;nbsp; Ok, we see here why some test data, and your missing what the output should look like, does to help illustrate - note how I post code in a code window in the post:&lt;/P&gt;
&lt;PRE&gt;data test;
informat trandate ANYDTDTE9. debit 8.2 credit 8.2 ;
input accountnum trandate debit credit count ;
format trandate DATE9.;
datalines;
100001 30APR2018 2000.00 . 1
100002 23MAY2018 3000.00 . 1
100002 26MAY2018 2500.50 . 2
100002 27MAY2018 . 2100.00 3
100002 28MAY2018 2100.00 . 4
100002 29MAY2018 2500.10 . 5
100003 23MAY2018 2450.10 . 1
100003 24MAY2018 . 2450.10 2
;
run;&lt;/PRE&gt;
&lt;P&gt;So basically to flag the row if previous is like it then:&lt;/P&gt;
&lt;PRE&gt;data want;
  set test;
  debit_chk=ifn(lag(debit)=debit,1,0);
  credit_chk=ifn(lag(credit)=credit,1,0);
run;&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 Jun 2018 07:55:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/469532#M120131</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-06-12T07:55:05Z</dc:date>
    </item>
    <item>
      <title>Re: Lag Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/469540#M120134</link>
      <description>&lt;P&gt;Thanks, but that code would only allow comparing to the previous row of data.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was using the arrays as I am trying to identify any matching debits (lag_debit1 to lag_debit5) to any credits (lag_credit1 to lag_credit5).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the final dataset (result) Account 10002 should produce a match for 2100 Amount (lag_debit4 = lag_credit3) and Account 10003 should produce a match for 2450.10 (lag_debit1 to lag_credit2).&amp;nbsp; However the results dataset is currently not identifying these matches and returns 0 for results 1 to results 5.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data test;&lt;BR /&gt;informat trandate ANYDTDTE9. debit 8.2 credit 8.2 ;&lt;BR /&gt;input accountnum trandate debit credit count ;&lt;BR /&gt;format trandate DATE9.;&lt;BR /&gt;datalines;&lt;BR /&gt;100001 30APR2018 2000.00 . 1&lt;BR /&gt;100002 23MAY2018 3000.00 . 1&lt;BR /&gt;100002 26MAY2018 2500.50 . 2&lt;BR /&gt;100002 27MAY2018 . 2100.00 3&lt;BR /&gt;100002 28MAY2018 2100.00 . 4&lt;BR /&gt;100002 29MAY2018 2500.10 . 5&lt;BR /&gt;100003 23MAY2018 2450.10 . 1&lt;BR /&gt;100003 24MAY2018 . 2450.10 2&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data want;&lt;BR /&gt;format lag_debit1-lag_debit5 8.2 lag_credit1-lag_credit5 8.2;&lt;BR /&gt;do until(last.accountnum);&lt;BR /&gt;set test;&lt;BR /&gt;by &amp;nbsp;&amp;nbsp; &amp;nbsp;accountnum trandate;&lt;BR /&gt;array lag_debit{5};&lt;BR /&gt;array lag_credit{5};&lt;BR /&gt;if first.accountnum then call missing(of lag_debit(*));&lt;BR /&gt;if first.accountnum then call missing(of lag_credit(*));&lt;BR /&gt;lag_debit(count)=debit;&lt;BR /&gt;lag_credit(count)=credit;&lt;BR /&gt;output;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data result;&lt;BR /&gt;&amp;nbsp; set want;&lt;BR /&gt;&amp;nbsp; array lag_debit{5};&lt;BR /&gt;&amp;nbsp; array lag_credit{5};&lt;BR /&gt;&amp;nbsp; array results{5};&lt;BR /&gt;&amp;nbsp; do i=1 to 5;&lt;BR /&gt;&lt;BR /&gt;results{i}= ifn(lag_debit{i}=lag_credit{i} and lag_debit{i} ne ., 1, 0);&lt;BR /&gt;&amp;nbsp;end;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc print data=result;&lt;BR /&gt;var accountnum lag_debit1-lag_debit5 lag_credit1-lag_credit5&amp;nbsp; results1 - results5 i ;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jun 2018 09:07:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/469540#M120134</guid>
      <dc:creator>Selli5</dc:creator>
      <dc:date>2018-06-12T09:07:32Z</dc:date>
    </item>
    <item>
      <title>Re: Lag Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/469543#M120135</link>
      <description>&lt;P&gt;Whilst you could do it with arrays, that wouldn't be optimal, especially if you get more rows.&amp;nbsp; What you want to do is to merge on any rows (distinct to get just one per value) which = debit, then flag those:&lt;/P&gt;
&lt;PRE&gt;data test;
  informat trandate ANYDTDTE9. debit 8.2 credit 8.2 ;
  input accountnum trandate debit credit count ;
  format trandate DATE9.;
datalines;
100001 30APR2018 2000.00 . 1
100002 23MAY2018 3000.00 . 1
100002 26MAY2018 2500.50 . 2
100002 27MAY2018 . 2100.00 3
100002 28MAY2018 2100.00 . 4
100002 29MAY2018 2500.10 . 5
100003 23MAY2018 2450.10 . 1
100003 24MAY2018 . 2450.10 2
;
run;

proc sql;
  create table want as
  select a.*,
         case when b.credit ne . then "Y" else "" end as flag
  from   test a
  left join (select distinct credit from test) b
  on     a.debit=b.credit
  order by accountnum,count;
quit;&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 Jun 2018 09:28:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/469543#M120135</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-06-12T09:28:20Z</dc:date>
    </item>
    <item>
      <title>Re: Lag Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/469577#M120148</link>
      <description>&lt;P&gt;Thank you it all works fine now.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Jun 2018 11:07:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/469577#M120148</guid>
      <dc:creator>Selli5</dc:creator>
      <dc:date>2018-06-12T11:07:38Z</dc:date>
    </item>
  </channel>
</rss>

