<?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: How do I use a previous row to evaluate the current row iteratively? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-a-previous-row-to-evaluate-the-current-row/m-p/650219#M194999</link>
    <description>&lt;P&gt;I agree with &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt; comments, this is not a task for SQL. I would prefer however not to use date arithmetics (date1 - date2) to calculate number of days since SAS provides the date function INTCK to do that calculation more explicitly. Also, I would avoid using the automatic variable _n_ as temporary storage because it only creates confusion for future programmers for the sake of saving a few keystrokes. I would prefer:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
anchorDate = "01jan1930"d; /* A date anterior to my dates */
do until(last.id);
 set have;
 by id;
 if intck("day", anchorDate, date) &amp;gt; 30 then do;
  output;
  anchorDate = date;
  end;
 end;
drop anchorDate;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sun, 24 May 2020 20:56:46 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2020-05-24T20:56:46Z</dc:date>
    <item>
      <title>How do I use a previous row to evaluate the current row iteratively?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-a-previous-row-to-evaluate-the-current-row/m-p/650215#M194997</link>
      <description>&lt;P&gt;I am looking at a data set of Emergency Room visits. I only want to keep visits per ID that are 30 days apart. So as an example say I have this below. If I start with ID=1. In&amp;nbsp; Row 1 I can see that the lag between row 1 and 2 is 15 days so I will exclude, or for now flag, row 2. Then I will continue to use Row 1 to evaluate Row 3. Again this is only 17 days so I will exclude Row 3 and look at Row 4. Row 4 is 30 days away so I keep it and then use Row 4 to evaluate Row 5....and so on. I have been trying to do this with the lag function but I can't figure out how to utilize the lag when I have to continue to use the 'anchor' row to evaluate several rows. Top is what I have and bottom is what I want. Any ideas??? If anyone knows how to do this using Proc SQL that would be even better. Much appreciated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="196"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="64"&gt;HAVE&lt;/TD&gt;
&lt;TD width="64"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="68"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;Row #&lt;/TD&gt;
&lt;TD&gt;ID&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;1/1/2020&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;1/15/2020&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;1/17/2020&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;4&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;2/4/2020&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;5&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;3/15/2020&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;6&lt;/TD&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;1/15/2020&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;7&lt;/TD&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;3/15/2020&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;8&lt;/TD&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;3/18/2020&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;WANT&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;Row #&lt;/TD&gt;
&lt;TD&gt;ID&lt;/TD&gt;
&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;1/1/2020&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;4&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;2/4/2020&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;5&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;3/15/2020&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;6&lt;/TD&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;1/15/2020&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;7&lt;/TD&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;3/15/2020&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Sun, 24 May 2020 19:56:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-a-previous-row-to-evaluate-the-current-row/m-p/650215#M194997</guid>
      <dc:creator>LOLO</dc:creator>
      <dc:date>2020-05-24T19:56:27Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use a previous row to evaluate the current row iteratively?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-a-previous-row-to-evaluate-the-current-row/m-p/650218#M194998</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/36059"&gt;@LOLO&lt;/a&gt;&amp;nbsp; It's a straight forward datastep solution. In my humble opinion Proc SQL approach&amp;nbsp;would be very inefficient involving more I/O , join , grouping and beyond. I'd recommend to stick to&amp;nbsp; more appropriate solution&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

data have;
input row id date :mmddyy10.;
format date mmddyy10.;
cards;
1	1	1/1/2020
2	1	1/15/2020
3	1	1/17/2020
4	1	2/4/2020
5	1	3/15/2020
6	2	1/15/2020
7	2	3/15/2020
8	2	3/18/2020
;

data want;
 do until(last.id);
  set have;
  by id;
  if first.id or date-_n_&amp;gt;30 then do;
   output;
   _n_=date;
  end;
 end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 24 May 2020 20:15:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-a-previous-row-to-evaluate-the-current-row/m-p/650218#M194998</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-05-24T20:15:55Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use a previous row to evaluate the current row iteratively?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-a-previous-row-to-evaluate-the-current-row/m-p/650219#M194999</link>
      <description>&lt;P&gt;I agree with &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt; comments, this is not a task for SQL. I would prefer however not to use date arithmetics (date1 - date2) to calculate number of days since SAS provides the date function INTCK to do that calculation more explicitly. Also, I would avoid using the automatic variable _n_ as temporary storage because it only creates confusion for future programmers for the sake of saving a few keystrokes. I would prefer:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
anchorDate = "01jan1930"d; /* A date anterior to my dates */
do until(last.id);
 set have;
 by id;
 if intck("day", anchorDate, date) &amp;gt; 30 then do;
  output;
  anchorDate = date;
  end;
 end;
drop anchorDate;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 24 May 2020 20:56:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-a-previous-row-to-evaluate-the-current-row/m-p/650219#M194999</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-05-24T20:56:46Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use a previous row to evaluate the current row iteratively?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-a-previous-row-to-evaluate-the-current-row/m-p/650449#M195030</link>
      <description>Are you asking about SQL connected to say, SQL Server ?  A SQL Server solution would likely require a recursive common table expression (CTE)</description>
      <pubDate>Mon, 25 May 2020 13:40:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-a-previous-row-to-evaluate-the-current-row/m-p/650449#M195030</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-05-25T13:40:36Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use a previous row to evaluate the current row iteratively?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-a-previous-row-to-evaluate-the-current-row/m-p/650534#M195073</link>
      <description>&lt;P&gt;Yes you are correct. I am a SAS programmer but I need to write some code in AZURE Data Studio. So Yes I have to use SQL Server. So I am trying to figure this out in SAS first and then apply it in SQL. Any ideas?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 May 2020 21:18:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-a-previous-row-to-evaluate-the-current-row/m-p/650534#M195073</guid>
      <dc:creator>LOLO</dc:creator>
      <dc:date>2020-05-25T21:18:02Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use a previous row to evaluate the current row iteratively?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-a-previous-row-to-evaluate-the-current-row/m-p/650730#M195160</link>
      <description>&lt;P&gt;This works! And it's so simple. Thank you.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 May 2020 13:22:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-a-previous-row-to-evaluate-the-current-row/m-p/650730#M195160</guid>
      <dc:creator>LOLO</dc:creator>
      <dc:date>2020-05-26T13:22:18Z</dc:date>
    </item>
  </channel>
</rss>

