<?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: If the sequence is not continuous i need to delete all remaining observations for particular par in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/If-the-sequence-is-not-continuous-i-need-to-delete-all-remaining/m-p/643623#M192124</link>
    <description>&lt;P&gt;You need to retain a flag variable so you know when the sequence error has appeared.&lt;/P&gt;
&lt;P&gt;Here is code that does what you want by comparing the current value to SEQ_NO to the previous value plus 1.&amp;nbsp; It resets the flag for each new PART_NO group.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  input part_no $ seq_no @@;
datalines;
1234 1 1234 2 1234 3 1234 4 1234 1 1234 2 1234 3 1234 4 1234 5 1234 6
1456 1 1456 2 1456 3 1456 4 1456 1 1456 2 1456 3 1456 4 1456 5
;
data want;
  set test;
  by part_no ;
  retain found ;
  if seq_no ne sum(lag(seq_no),1) then found=1;
  if first.part_no then found=0;
  if not found;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;If you want to also test if the sequence starts with one the change the value set on first observation.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  if first.part_no then found=(seq_no ne 1);&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 28 Apr 2020 14:51:53 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2020-04-28T14:51:53Z</dc:date>
    <item>
      <title>If the sequence is not continuous i need to delete all remaining observations for particular part_no</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-the-sequence-is-not-continuous-i-need-to-delete-all-remaining/m-p/643613#M192121</link>
      <description>&lt;P&gt;Input:&lt;/P&gt;&lt;P&gt;data test; infile datalines; input @2 part_no $4. @7 seq_no 2.; datalines; 1234 1 1234 2 1234 3 1234 4 1234 1 1234 2 1234 3 1234 4 1234 5 1234 6 1456 1 1456 2 1456 3 1456 4 1456 1 1456 2 1456 3 1456 4 1456 5 ; run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I will require output like :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;part_no seq_no&lt;BR /&gt;1234&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;BR /&gt;1234&amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;BR /&gt;1234&amp;nbsp; &amp;nbsp; &amp;nbsp; 3&lt;BR /&gt;1234&amp;nbsp; &amp;nbsp; &amp;nbsp; 4&lt;BR /&gt;1456&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;BR /&gt;1456&amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;BR /&gt;1456&amp;nbsp; &amp;nbsp; &amp;nbsp; 3&amp;nbsp;&lt;BR /&gt;1456&amp;nbsp; &amp;nbsp; &amp;nbsp; 4&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 28 Apr 2020 14:12:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-the-sequence-is-not-continuous-i-need-to-delete-all-remaining/m-p/643613#M192121</guid>
      <dc:creator>vidyasagar1</dc:creator>
      <dc:date>2020-04-28T14:12:05Z</dc:date>
    </item>
    <item>
      <title>Re: If the sequence is not continuous i need to delete all remaining observations for particular par</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-the-sequence-is-not-continuous-i-need-to-delete-all-remaining/m-p/643620#M192122</link>
      <description>&lt;P&gt;For your example data, once the data step is cleaned up a bit, this gets the desired result.&lt;/P&gt;
&lt;PRE&gt;data test; 
infile datalines; 
input part_no :$4. seq_no :2.; 
datalines; 
1234 1 
1234 2 
1234 3 
1234 4 
1234 1 
1234 2 
1234 3 
1234 4 
1234 5 
1234 6 
1456 1 
1456 2 
1456 3 
1456 4 
1456 1 
1456 2 
1456 3 
1456 4 
1456 5 
;
run;

/* assumes test is sorted by Part_no*/
data want;
   set test;
   by part_no;
   retain counter;
   if first.part_no then counter=1;
   else counter+1;
   if counter ne seq_no then delete;
   drop counter;
run;&lt;/PRE&gt;
&lt;P&gt;However this is very subject to the actual order of values and if the blocks of seq_no do not behave as shown then this won't work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your data step does not execute because 1) the @2 and such get scrambled into uselessness because the message windows on the forum will remove much white space (blanks and such). 2) Datalines must be on a line all by itself. The data starts on the following line. 3) the ending ; or ;;;; for a data lines block must be on a line by itself as well.&lt;/P&gt;
&lt;P&gt;And the data in one line doesn't work with the given input statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is best to post code into a code box opened with the &amp;lt;/&amp;gt; or "running man" icons to preserve formatting.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Apr 2020 14:38:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-the-sequence-is-not-continuous-i-need-to-delete-all-remaining/m-p/643620#M192122</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-04-28T14:38:26Z</dc:date>
    </item>
    <item>
      <title>Re: If the sequence is not continuous i need to delete all remaining observations for particular par</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-the-sequence-is-not-continuous-i-need-to-delete-all-remaining/m-p/643623#M192124</link>
      <description>&lt;P&gt;You need to retain a flag variable so you know when the sequence error has appeared.&lt;/P&gt;
&lt;P&gt;Here is code that does what you want by comparing the current value to SEQ_NO to the previous value plus 1.&amp;nbsp; It resets the flag for each new PART_NO group.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
  input part_no $ seq_no @@;
datalines;
1234 1 1234 2 1234 3 1234 4 1234 1 1234 2 1234 3 1234 4 1234 5 1234 6
1456 1 1456 2 1456 3 1456 4 1456 1 1456 2 1456 3 1456 4 1456 5
;
data want;
  set test;
  by part_no ;
  retain found ;
  if seq_no ne sum(lag(seq_no),1) then found=1;
  if first.part_no then found=0;
  if not found;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;If you want to also test if the sequence starts with one the change the value set on first observation.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  if first.part_no then found=(seq_no ne 1);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 28 Apr 2020 14:51:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-the-sequence-is-not-continuous-i-need-to-delete-all-remaining/m-p/643623#M192124</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-04-28T14:51:53Z</dc:date>
    </item>
    <item>
      <title>Re: If the sequence is not continuous i need to delete all remaining observations for particular par</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-the-sequence-is-not-continuous-i-need-to-delete-all-remaining/m-p/643629#M192126</link>
      <description>Awesome .. Thank you so much ...&lt;BR /&gt;Thank you for the input to post code on code box ..I will do it from next time .. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Tue, 28 Apr 2020 14:54:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-the-sequence-is-not-continuous-i-need-to-delete-all-remaining/m-p/643629#M192126</guid>
      <dc:creator>vidyasagar1</dc:creator>
      <dc:date>2020-04-28T14:54:56Z</dc:date>
    </item>
  </channel>
</rss>

