<?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/467942#M119479</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/134523"&gt;@Selli5&lt;/a&gt;&amp;nbsp;if i understand you correctly, arrays should suffice&lt;/P&gt;&lt;P&gt;increase the subscript to 1000s if you want&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input accountnum trandate :DATE9. debit  count ;
cards;
100001 30APR2018 2000.00 1
100002 23MAY2018 3000.00 1
100002 26MAY2018 2500.50 2
100002 27MAY2018 2100.00 3
100002 28MAY2018 2400.00 4
100002 29MAY2018 2500.10 5
100003 23MAY2018 2450.10 1
100003 24MAY2018 2500.50 2
;
run;

data want;
do until(last.accountnum);
set test;
by 	accountnum;
array lag_debit(100);
if first.accountnum then call missing(of lag_debit(*));
lag_debit(count)=debit;
output;
end;
run;


&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 06 Jun 2018 03:13:26 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2018-06-06T03:13:26Z</dc:date>
    <item>
      <title>Lag Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/467933#M119475</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to apply the lag function to the debit variable, the exact same number of times as the count variable (except for when there is only one accountnum, then there would be no lag).&amp;nbsp; For some accounts the count variable will go into the thousands.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data test;&lt;BR /&gt;input accountnum trandate DATE9. debit 8.2 count ;&lt;BR /&gt;cards;&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 2400.00 4&lt;BR /&gt;100002 29MAY2018 2500.10 5&lt;BR /&gt;100003 23MAY2018 2450.10 1&lt;BR /&gt;100003 24MAY2018 2500.50 2&lt;BR /&gt;;&lt;BR /&gt;run;&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;</description>
      <pubDate>Wed, 06 Jun 2018 02:02:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/467933#M119475</guid>
      <dc:creator>Selli5</dc:creator>
      <dc:date>2018-06-06T02:02:31Z</dc:date>
    </item>
    <item>
      <title>Re: Lag Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/467941#M119478</link>
      <description>&lt;P&gt;Please clarify your request by showing what the desired result should look like.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Jun 2018 03:12:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/467941#M119478</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-06-06T03:12:37Z</dc:date>
    </item>
    <item>
      <title>Re: Lag Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/467942#M119479</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/134523"&gt;@Selli5&lt;/a&gt;&amp;nbsp;if i understand you correctly, arrays should suffice&lt;/P&gt;&lt;P&gt;increase the subscript to 1000s if you want&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input accountnum trandate :DATE9. debit  count ;
cards;
100001 30APR2018 2000.00 1
100002 23MAY2018 3000.00 1
100002 26MAY2018 2500.50 2
100002 27MAY2018 2100.00 3
100002 28MAY2018 2400.00 4
100002 29MAY2018 2500.10 5
100003 23MAY2018 2450.10 1
100003 24MAY2018 2500.50 2
;
run;

data want;
do until(last.accountnum);
set test;
by 	accountnum;
array lag_debit(100);
if first.accountnum then call missing(of lag_debit(*));
lag_debit(count)=debit;
output;
end;
run;


&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 06 Jun 2018 03:13:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/467942#M119479</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-06-06T03:13:26Z</dc:date>
    </item>
    <item>
      <title>Re: Lag Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/467947#M119480</link>
      <description>&lt;P&gt;If you want the lag but not for the first record you can set it to missing using FIRST.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Lag is kind of tricky to understand, so I highly recommend reading this paper to understand how it works.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://support.sas.com/resources/papers/proceedings09/055-2009.pdf" target="_blank"&gt;http://support.sas.com/resources/papers/proceedings09/055-2009.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Making some massive assumptions, I think this is what you're looking for. Note the BY statement which may mean you need to sort the data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set test;

by accountNum;

lag_debit=lag(debit);

if first.accountNum then lag_debit = . ;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&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;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to apply the lag function to the debit variable, the exact same number of times as the count variable (except for when there is only one accountnum, then there would be no lag).&amp;nbsp; For some accounts the count variable will go into the thousands.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;data test;&lt;BR /&gt;input accountnum trandate DATE9. debit 8.2 count ;&lt;BR /&gt;cards;&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 2400.00 4&lt;BR /&gt;100002 29MAY2018 2500.10 5&lt;BR /&gt;100003 23MAY2018 2450.10 1&lt;BR /&gt;100003 24MAY2018 2500.50 2&lt;BR /&gt;;&lt;BR /&gt;run;&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;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Jun 2018 03:22:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/467947#M119480</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-06-06T03:22:00Z</dc:date>
    </item>
    <item>
      <title>Re: Lag Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/467969#M119488</link>
      <description>&lt;P&gt;%MACRO LAGLOOP ;&lt;BR /&gt;&amp;nbsp; data temp3 ;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set temp2 ;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; by accountnum trandate;&lt;BR /&gt;&amp;nbsp; %DO J = 1 %TO 3000 ;&lt;BR /&gt;&amp;nbsp;/* %DO J = 1 %TO J le count;*/&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lag_&amp;amp;J = lag&amp;amp;J(debit) ;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; if first.accountnum then do;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lag_&amp;amp;J = .;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;BR /&gt;%END;&lt;BR /&gt;&amp;nbsp; run ;&lt;BR /&gt;%MEND ;&lt;BR /&gt;%LAGLOOP ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the closest I have got to the answer, however the lag_1 etc is not unique to accountnum, its just looking at the previous debit regardless of accountnum:&lt;/P&gt;&lt;P&gt;Obs count count2 accountnum trancode trandate Debit lag_1 lag_2 lag_3 lag_4 lag_5 etc&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;100001&lt;/TD&gt;&lt;TD&gt;26&lt;/TD&gt;&lt;TD&gt;30APR2018&lt;/TD&gt;&lt;TD&gt;$-2,000.00&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;100004&lt;/TD&gt;&lt;TD&gt;26&lt;/TD&gt;&lt;TD&gt;02MAY2018&lt;/T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;-22.50&lt;/TD&gt;&lt;TD&gt;-112.25&lt;/TD&gt;&lt;TD&gt;-51.25&lt;/TD&gt;&lt;TD&gt;-1200.00&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;35&lt;/TD&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;TD&gt;100006&lt;/TD&gt;&lt;TD&gt;780&lt;/TD&gt;&lt;TD&gt;23MAY2018&lt;/TD&gt;&lt;TD&gt;$-69.30&lt;/TD&gt;&lt;TD&gt;-100.65&lt;/TD&gt;&lt;TD&gt;-26.34&lt;/TD&gt;&lt;TD&gt;-22.50&lt;/TD&gt;&lt;TD&gt;-112.25&lt;/TD&gt;&lt;TD&gt;-51.25&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Jun 2018 06:03:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/467969#M119488</guid>
      <dc:creator>Selli5</dc:creator>
      <dc:date>2018-06-06T06:03:46Z</dc:date>
    </item>
    <item>
      <title>Re: Lag Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/467993#M119496</link>
      <description>&lt;P&gt;Hi,&amp;nbsp; Below is the first 10 obs.&amp;nbsp; When I run the code it lag_1 is referring to the current debit instead of the previous debit and the lag function is also excluded from the last account (10006) altogether: How do you fix this?&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;count &amp;nbsp; &amp;nbsp; &amp;nbsp; accountnum trandate&amp;nbsp;&amp;nbsp;&amp;nbsp; Debit&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&am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t;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;100006&lt;/TD&gt;&lt;TD&gt;17MAY2018&lt;/TD&gt;&lt;TD&gt;$-100.65&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;TD&gt;100006&lt;/TD&gt;&lt;TD&gt;23MAY2018&lt;/TD&gt;&lt;TD&gt;$-69.30&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Jun 2018 08:30:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/467993#M119496</guid>
      <dc:creator>Selli5</dc:creator>
      <dc:date>2018-06-06T08:30:00Z</dc:date>
    </item>
    <item>
      <title>Re: Lag Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/468038#M119508</link>
      <description>&lt;P&gt;You don't need a macro at all.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input accountnum trandate DATE9. debit 8.2 count ;
cards;
100001 30APR2018 2000.00 1
100002 23MAY2018 3000.00 1
100002 26MAY2018 2500.50 2
100002 27MAY2018 2100.00 3
100002 28MAY2018 2400.00 4
100002 29MAY2018 2500.10 5
100003 23MAY2018 2450.10 1
100003 24MAY2018 2500.50 2
;
run;

proc sql noprint;
select max(count) - 1 into :maxcount trimmed from test;
quit;

data want;
set test;
array debits {&amp;amp;maxcount} debit_1-debit_&amp;amp;maxcount;
retain debits;
by accountnum;
prevdebit = lag(debit);
if first.accountnum
then do;
  do index = 1 to &amp;amp;maxcount;
    debits{index} = .;
  end;
end;
else do;
  do index = &amp;amp;maxcount to 2 by -1;
    debits{index} = debits{index-1};
  end;
  debits{1} = prevdebit;
end;
drop prevdebit index;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The only "macro" thing here is the macrovar maxcount, to dynamically determine the number of columns needed.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Jun 2018 13:20:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/468038#M119508</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-06-06T13:20:17Z</dc:date>
    </item>
    <item>
      <title>Re: Lag Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/468061#M119520</link>
      <description>&lt;P&gt;Please post a sample of your expected output for the input sample you provided. Thank you&lt;/P&gt;</description>
      <pubDate>Wed, 06 Jun 2018 14:09:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/468061#M119520</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-06-06T14:09:33Z</dc:date>
    </item>
    <item>
      <title>Re: Lag Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/468228#M119566</link>
      <description>&lt;P&gt;Thanks, but I get this error message:&lt;/P&gt;&lt;PRE class="sasLog"&gt; ERROR: Array subscript out of range at line 69 column 1.&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Jun 2018 01:26:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/468228#M119566</guid>
      <dc:creator>Selli5</dc:creator>
      <dc:date>2018-06-07T01:26:06Z</dc:date>
    </item>
    <item>
      <title>Re: Lag Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/468231#M119567</link>
      <description>&lt;P&gt;Thanks its working now.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there anyway to instead of writing an exact number...eg (array lag_debit(&lt;STRONG&gt;3000&lt;/STRONG&gt;);,)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;it can do the exact number of lags = the last.accountnum count variable?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;eg if there are 6 occurances of an account and the last accountnum has count=6 then do 6 lags and if an accountnum has count=3000 on the last.accountnum then do 3000 lags?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Sally&lt;/P&gt;</description>
      <pubDate>Thu, 07 Jun 2018 01:40:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/468231#M119567</guid>
      <dc:creator>Selli5</dc:creator>
      <dc:date>2018-06-07T01:40:16Z</dc:date>
    </item>
    <item>
      <title>Re: Lag Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/468256#M119579</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 its working now.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there anyway to instead of writing an exact number...eg (array lag_debit(&lt;STRONG&gt;3000&lt;/STRONG&gt;);,)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;it can do the exact number of lags = the last.accountnum count variable?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;eg if there are 6 occurances of an account and the last accountnum has count=6 then do 6 lags and if an accountnum has count=3000 on the last.accountnum then do 3000 lags?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sally&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;See this slight adaptation of my code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input accountnum trandate DATE9. debit 8.2 count ;
cards;
100001 30APR2018 2000.00 1
100002 23MAY2018 3000.00 1
100002 26MAY2018 2500.50 2
100002 27MAY2018 2100.00 3
100002 28MAY2018 2400.00 4
100002 29MAY2018 2500.10 5
100003 23MAY2018 2450.10 1
100003 24MAY2018 2500.50 2
;
run;

proc sql noprint;
select max(count) - 1 into :maxcount trimmed from test;
quit;

data want;
set test;
array debits {&amp;amp;maxcount} debit_1-debit_&amp;amp;maxcount;
retain debits;
by accountnum;
prevdebit = lag(debit);
if first.accountnum
then do;
  do index = 1 to &amp;amp;maxcount;
    debits{index} = .;
  end;
end;
else do;
  do index = count - 1 to 2 by -1;
    debits{index} = debits{index-1};
  end;
  debits{1} = prevdebit;
end;
drop prevdebit index;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;accountnum    trandate     debit    count    debit_1    debit_2    debit_3    debit_4

  100001        21304     2000.0      1           .          .          .          . 
  100002        21327     3000.0      1           .          .          .          . 
  100002        21330     2500.5      2       3000.0         .          .          . 
  100002        21331     2100.0      3       2500.5     3000.0         .          . 
  100002        21332     2400.0      4       2100.0     2500.5     3000.0         . 
  100002        21333     2500.1      5       2400.0     2100.0     2500.5      3000 
  100003        21327     2450.1      1           .          .          .          . 
  100003        21328     2500.5      2       2450.1         .          .          . 
&lt;/PRE&gt;</description>
      <pubDate>Thu, 07 Jun 2018 06:02:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/468256#M119579</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-06-07T06:02:14Z</dc:date>
    </item>
    <item>
      <title>Re: Lag Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/468462#M119657</link>
      <description>&lt;P&gt;Clearly the resolution of this problem is much more amenable to arrays than use of multiple lag functions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Although &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;'s solution is straightforward, it is burdened with the time&amp;nbsp;needed to execute loops of increasing size as the count variable grows.&amp;nbsp; This is an excellent problem to consider a more sophisticated way (#2 below) of handling the procession of long arrays of values.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I suggest taking advantage of arrays in these additional ways:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Use the call missing(of debits{*}) to reset the entire array to missing while avoiding the construction and execution of a loop.&amp;nbsp; That's in the "if first.accountnum" statement.&lt;/LI&gt;
&lt;LI&gt;In the "else" statement, take advantage of the fact that array elements are stored in contiguous locations in memory, with each numeric element occupying 8 bytes.&amp;nbsp; So if you make provision for an array of 3000 elements, it is stored in 24,000 contiguous bytes in memory.&amp;nbsp;&amp;nbsp; As a result,&amp;nbsp;if&amp;nbsp;you need to shift 1000 elements (when variable COUNT=1001) to the right by one position in the array, you could use one instruction to move 8000 bytes (1000 elements * 8 bytes/element) to the right by 8 bytes, instead of 1000 iterations of copying a single element one position to the right.&amp;nbsp;The bigger the COUNT variable, the more efficient this approach will be.&amp;nbsp; It uses the ADDRLONG, PEEKCLONG, and CALL POKE functions, in the program below.&amp;nbsp;&amp;nbsp;I've added a few notes about them afterwards.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input accountnum trandate DATE9. debit 8.2 count ;
cards;
100001 30APR2018 2000.00 1
100002 23MAY2018 3000.00 1
100002 26MAY2018 2500.50 2
100002 27MAY2018 2100.00 3
100002 28MAY2018 2400.00 4
100002 29MAY2018 2500.10 5
100003 23MAY2018 2450.10 1
100003 24MAY2018 2500.50 2
;
run;

proc sql noprint;
select max(count) - 1 into :maxcount trimmed from test;
quit;
%put &amp;amp;=maxcount;


data want;
  set test;
  by accountnum;

  array debits {*} prevdebit debit_1-debit_&amp;amp;maxcount;
  retain debits;

  if _n_=1 then do;
    addrprev=addrlong(prevdebit);&lt;BR /&gt;    addr1=addrlong(debit_1);
  end;
  retain addr: ;
  prevdebit = lag(debit);

  if first.accountnum then call missing (of debits{*});
  else call pokelong(peekclong(addrprev,8*(count-1)),addr1);

drop prevdebit addr: ;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Notes:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;I include PREVDEBIT at the beginning of the array DEBITS, so it is included in the array procession operations.&lt;/LI&gt;
&lt;LI&gt;The &lt;STRONG&gt;ADDRLONG&lt;/STRONG&gt; function gets the memory address of PREVDEBIT (the left-end of the source of the transfer) and DEBIT_1 (left-end of the destination).&amp;nbsp; I put the address generation&amp;nbsp;in an &lt;EM&gt;&lt;STRONG&gt;"IF _N_=1" do group&lt;/STRONG&gt;&lt;/EM&gt;, because the addresses are fixed, and need to be calculated only once, instead of for each incoming observation.&amp;nbsp; By retaining the results (addrprev and addr1) they are available for use in the call poke function for every record.&lt;/LI&gt;
&lt;LI&gt;The &lt;STRONG&gt;PEEKCLONG(addrprev,8*(count-1))&lt;/STRONG&gt;&amp;nbsp; function retrieves a contiguous string of 8*(count-1) bytes starting at addrprev, which is the leftmost location of the array.&lt;/LI&gt;
&lt;LI&gt;The peekclong is embedded in a &lt;STRONG&gt;CALL POKE&lt;/STRONG&gt; routine which takes the content of the first argument (i.e. the bytes retrieved in #3 above) and stores them at the address specified in the 2nd argument, i.e. addr1.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'd be very interested in the speed comparisons of this program versus the do-loop approach.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edited additional notes:&lt;/P&gt;
&lt;P&gt;It's possible the "call missing(of debits{*})" is also a bit inefficient.&amp;nbsp; It will always reset to missing the entire array even if only 10% of it has non-missing values.&amp;nbsp; That could be solved by (1) adding a temporary array of missing values, (2) getting lag of count, and (3) using call pokelong instead of call missing, as in:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set test;
  by accountnum;

  array debits {*} prevdebit debit_1-debit_&amp;amp;maxcount;
  array misvals {0:&amp;amp;maxcount} _temporary_;   /* New statement */
  retain debits;

  if _n_=1 then do;
    addrprev=addrlong(prevdebit);
    addr1=addrlong(debit_1);
    addrmis=addrlong(misvals{0});     /* New statement */
  end;
  retain addr: ;
  prevdebit = lag(debit);
  prevcount = lag(count);

  if first.accountnum then call pokelong(peekclong(addrmis,8*prevcount),addrprev);    /* Modified statement */
  else call pokelong(peekclong(addrprev,8*(count-1)),addr1);

drop prev: addr: ;
run;

proc print data=want noobs;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 07 Jun 2018 17:46:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/468462#M119657</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-06-07T17:46:01Z</dc:date>
    </item>
    <item>
      <title>Re: Lag Function</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/468605#M119717</link>
      <description>&lt;P&gt;While I strongly suggest that you study &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;'s suggested solution in-depth (to see how some quite arcane SAS functions can be used), I probably would not use it in practice/production.&lt;/P&gt;
&lt;P&gt;Why?&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;most performance can be gained by reducing I/O; both methods require the same number of reads through the dataset&lt;/LI&gt;
&lt;LI&gt;using the peek/poke functions this way is equivalent to C pointer math, which a prominent hacker I admire quite a lot has recently called a "defect attractor".&lt;/LI&gt;
&lt;LI&gt;Such code meets my definition of "clever" (see Maxim 29), and can cause unnecessary confusion when it needs to be maintained by a novice. If you constantly write code that only you can understand, you'll never be able to delegate work.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;So it all ends up as a clear case for Maxims 4 &amp;amp; 30. Only if a significant performance gain is observed (say, reducing runtime from 5 to 3 hours), should a more complex method be implemented.&lt;/P&gt;
&lt;P&gt;Even if you shave off 90%, but only gain 9 seconds because the original run took just 10, I'd stay with the simple algorithm. Unless you know that the job has to scale up to much larger data in the future.&lt;/P&gt;</description>
      <pubDate>Fri, 08 Jun 2018 06:22:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-Function/m-p/468605#M119717</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-06-08T06:22:43Z</dc:date>
    </item>
  </channel>
</rss>

