<?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: assigning previous eff_date to current row for each acct_no in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/assigning-previous-eff-date-to-current-row-for-each-acct-no/m-p/916873#M361142</link>
    <description>&lt;P&gt;The lag function is NOT a "lookback".&amp;nbsp; It is a queue management function.&amp;nbsp; What you need is to always update the queue, but not always keep the results.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The IFN function lets you do this, as in:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ACCT_NO EFF_DATE : date9. ;
format EFF_DATE date9.;
datalines;
1 21JUL2023
1 25JUL2023
2 30JUN2023
2 10JUL2023
2 01AUG2023
;
data want;
    set have;
    by acct_no;
    previous_date=ifn(first.acct_no,.,lag(eff_date));
    format previous_date date9.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The advantage of the IFN function is that both results (the 2nd and 3rd arguments) are executed, and then one of them is chosed based in the first argument.&amp;nbsp; So the lag is always updated, but not always returned to previous_date.&lt;/P&gt;</description>
    <pubDate>Mon, 19 Feb 2024 21:55:55 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2024-02-19T21:55:55Z</dc:date>
    <item>
      <title>assigning previous eff_date to current row for each acct_no</title>
      <link>https://communities.sas.com/t5/SAS-Programming/assigning-previous-eff-date-to-current-row-for-each-acct-no/m-p/916841#M361133</link>
      <description>&lt;P&gt;Hi, i need to create new column previous_date for each acct_no, if current row is the first observation for particular acct_no then previous_date should be null, otherwise it should be the&amp;nbsp;&lt;/P&gt;
&lt;P&gt;observation(eff_date) from previous row, on the screenshot of results on previous_date, the value should be 21Jul2023 on row3 instead of null and row4 should be 30Jun2023&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="results.PNG" style="width: 548px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/93916i03AB92492747DEB5/image-size/large?v=v2&amp;amp;px=999" role="button" title="results.PNG" alt="results.PNG" /&gt;&lt;/span&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
input ACCT_NO EFF_DATE : date9. ;
format EFF_DATE date9.;
datalines;
1 21JUL2023
1 25JUL2023
2 30JUN2023
2 10JUL2023
2 01AUG2023
;
run;


proc sort data=have;
by ACCT_NO EFF_DATE;
run;


data want;
set have;
by ACCT_NO;

if first.ACCT_NO then previous_date = .;

else previous_date = lag(EFF_DATE);

format previous_date date9.;

run;


&lt;/PRE&gt;</description>
      <pubDate>Mon, 19 Feb 2024 18:34:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/assigning-previous-eff-date-to-current-row-for-each-acct-no/m-p/916841#M361133</guid>
      <dc:creator>Solly7</dc:creator>
      <dc:date>2024-02-19T18:34:00Z</dc:date>
    </item>
    <item>
      <title>Re: assigning previous eff_date to current row for each acct_no</title>
      <link>https://communities.sas.com/t5/SAS-Programming/assigning-previous-eff-date-to-current-row-for-each-acct-no/m-p/916844#M361134</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ACCT_NO EFF_DATE : date9. ;
format EFF_DATE date9.;
datalines;
1 21JUL2023
1 25JUL2023
2 30JUN2023
2 10JUL2023
2 01AUG2023
;
data want;
    set have;
    by acct_no;
    lag_date=lag(eff_date);
    if first.acct_no then previous_date=.;
    else previous_date=lag_date;
    drop lag_date;
    format previous_date date9.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 19 Feb 2024 18:40:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/assigning-previous-eff-date-to-current-row-for-each-acct-no/m-p/916844#M361134</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-02-19T18:40:24Z</dc:date>
    </item>
    <item>
      <title>Re: assigning previous eff_date to current row for each acct_no</title>
      <link>https://communities.sas.com/t5/SAS-Programming/assigning-previous-eff-date-to-current-row-for-each-acct-no/m-p/916873#M361142</link>
      <description>&lt;P&gt;The lag function is NOT a "lookback".&amp;nbsp; It is a queue management function.&amp;nbsp; What you need is to always update the queue, but not always keep the results.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The IFN function lets you do this, as in:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ACCT_NO EFF_DATE : date9. ;
format EFF_DATE date9.;
datalines;
1 21JUL2023
1 25JUL2023
2 30JUN2023
2 10JUL2023
2 01AUG2023
;
data want;
    set have;
    by acct_no;
    previous_date=ifn(first.acct_no,.,lag(eff_date));
    format previous_date date9.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The advantage of the IFN function is that both results (the 2nd and 3rd arguments) are executed, and then one of them is chosed based in the first argument.&amp;nbsp; So the lag is always updated, but not always returned to previous_date.&lt;/P&gt;</description>
      <pubDate>Mon, 19 Feb 2024 21:55:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/assigning-previous-eff-date-to-current-row-for-each-acct-no/m-p/916873#M361142</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-02-19T21:55:55Z</dc:date>
    </item>
    <item>
      <title>Re: assigning previous eff_date to current row for each acct_no</title>
      <link>https://communities.sas.com/t5/SAS-Programming/assigning-previous-eff-date-to-current-row-for-each-acct-no/m-p/916925#M361175</link>
      <description>thank you for clarity and noted</description>
      <pubDate>Tue, 20 Feb 2024 08:08:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/assigning-previous-eff-date-to-current-row-for-each-acct-no/m-p/916925#M361175</guid>
      <dc:creator>Solly7</dc:creator>
      <dc:date>2024-02-20T08:08:15Z</dc:date>
    </item>
  </channel>
</rss>

