<?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 comapre previous rows and keep or drop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/comapre-previous-rows-and-keep-or-drop/m-p/608088#M176918</link>
    <description>&lt;P&gt;Hi!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can i solve this with sas data step?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What i want:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;if 1. row (id=782139) and&amp;nbsp; -64803 &amp;lt; prev row amt (-43504) then keep the row else drop ... and so.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your help!&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA work.sample;
	INPUT ID DATE AMT;
	DATALINES; 
	782139	20191025	-64803
	782139	20190925	-43504
	782139	20190913	-21775
	782139	20190826	-44775
	782139	20190725	-22620
	782138	20191025	-99803
	782138	20190925	-43504
	782138	20190913	-21775
	782138	20190826	-10775
	782139	20190725	-22620
;
RUN;

DATA work.want;
	INPUT ID DATE AMT;
	DATALINES; 
	782139	20191025	-64803
	782139	20190925	-43504
	782139	20190913	-21775
	782138	20191025	-99803
	782138	20190925	-43504
	782138	20190913	-21775
	782138	20190826	-10775
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 28 Nov 2019 16:48:42 GMT</pubDate>
    <dc:creator>ger15xxhcker</dc:creator>
    <dc:date>2019-11-28T16:48:42Z</dc:date>
    <item>
      <title>comapre previous rows and keep or drop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/comapre-previous-rows-and-keep-or-drop/m-p/608088#M176918</link>
      <description>&lt;P&gt;Hi!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can i solve this with sas data step?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What i want:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;if 1. row (id=782139) and&amp;nbsp; -64803 &amp;lt; prev row amt (-43504) then keep the row else drop ... and so.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your help!&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA work.sample;
	INPUT ID DATE AMT;
	DATALINES; 
	782139	20191025	-64803
	782139	20190925	-43504
	782139	20190913	-21775
	782139	20190826	-44775
	782139	20190725	-22620
	782138	20191025	-99803
	782138	20190925	-43504
	782138	20190913	-21775
	782138	20190826	-10775
	782139	20190725	-22620
;
RUN;

DATA work.want;
	INPUT ID DATE AMT;
	DATALINES; 
	782139	20191025	-64803
	782139	20190925	-43504
	782139	20190913	-21775
	782138	20191025	-99803
	782138	20190925	-43504
	782138	20190913	-21775
	782138	20190826	-10775
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 28 Nov 2019 16:48:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/comapre-previous-rows-and-keep-or-drop/m-p/608088#M176918</guid>
      <dc:creator>ger15xxhcker</dc:creator>
      <dc:date>2019-11-28T16:48:42Z</dc:date>
    </item>
    <item>
      <title>Re: comapre previous rows and keep or drop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/comapre-previous-rows-and-keep-or-drop/m-p/608101#M176927</link>
      <description>&lt;P&gt;One way&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   array a{999999} _temporary_;
   array b{999999} _temporary_;

   set sample;

   if amt &amp;gt; a[id] &amp;amp; b[id] ne 1 then do;
      output;
      a[id]=amt;
   end;
   else b[id]=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;ID      DATE        AMT 
782139  25/10/2019  -64803 
782139  25/09/2019  -43504 
782139  13/09/2019  -21775 
782138  25/10/2019  -99803 
782138  25/09/2019  -43504 
782138  13/09/2019  -21775 
782138  26/08/2019  -10775 
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Nov 2019 17:40:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/comapre-previous-rows-and-keep-or-drop/m-p/608101#M176927</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-11-28T17:40:51Z</dc:date>
    </item>
    <item>
      <title>Re: comapre previous rows and keep or drop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/comapre-previous-rows-and-keep-or-drop/m-p/608108#M176928</link>
      <description>&lt;P&gt;Your description of the rule does not match the results you want.&lt;/P&gt;
&lt;P&gt;First it looks like you want to keep the&amp;nbsp;observation where the value is GREATER than the previous value since all of your values are negative. And also you are keeping the first observation per ID.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So why do you not keep the last observation for ID=782139 ?&amp;nbsp; Its value of&amp;nbsp;-22,620 is greater than the value on the previous observation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Are you instead checking for the changes in the maximum value for the id?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can we assume the data is sorted by ID? Your example data is not sorted. Your result data is sort be DESCENDING values of ID.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=sample;
  by descending id ;
run;

data want;
  set sample;
  by descending id;
  if first.id or amt &amp;gt; max_amt then do;
    max_amt=amt;
    output;
  end;
  retain max_amt;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 370px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/34316i0AEB2EEC9960C294/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Nov 2019 18:05:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/comapre-previous-rows-and-keep-or-drop/m-p/608108#M176928</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-11-28T18:05:10Z</dc:date>
    </item>
  </channel>
</rss>

