<?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: Remove groups based on look-ahead and look-back condition in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Remove-groups-based-on-look-ahead-and-look-back-condition/m-p/823603#M325212</link>
    <description>&lt;P&gt;Just use a double DOW loop.&amp;nbsp; The first one to find the longest run of missing prices.&lt;/P&gt;
&lt;P&gt;The second one to re-read the data for that ID and output the values for the ID's you want to keep.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use the NOTSORTED keyword on the BY statement to allow you to group by ID PRICE.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input id date :yymmdd6. code $ color $ price;
  format date yymmdd10.;
datalines;
1 121201 BC Grey 2
1 121202 BC Blue .
1 121203 BC Blue .
1 121204 BC Blue .
2 121101 CC Black 3
2 121102 CC Black 4
3 121101 DD Green 15
3 121102 DD Green .
3 121103 DD Green .
3 121104 DD Green .
3 121105 DD Green 16
3 121107 DD Green .
4 121110 KK Purple 100
4 121111 KK Purple 102
4 121111 KK Purple .
5 121110 MM Red .
5 121111 MM Red .
5 121112 MM Red .
5 121113 MM Red 30
;

data want;
  do until (last.id);
    do n=1 by 1 until (last.price) ;
      set have ;
      by id price notsorted ;
    end;
    if missing(price) then maxrun=max(maxrun,n);
  end;
  do until (last.id);
    set have ;
    by id ;
    if maxrun &amp;lt; 3 then output;
  end;
  drop n maxrun ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;Obs    id          date    code    color     price

 1      2    2012-11-01     CC     Black        3
 2      2    2012-11-02     CC     Black        4
 3      4    2012-11-10     KK     Purple     100
 4      4    2012-11-11     KK     Purple     102
 5      4    2012-11-11     KK     Purple       .
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 16 Jul 2022 03:26:07 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-07-16T03:26:07Z</dc:date>
    <item>
      <title>Remove groups based on look-ahead and look-back condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-groups-based-on-look-ahead-and-look-back-condition/m-p/823600#M325209</link>
      <description>&lt;P&gt;Hi Guys&lt;/P&gt;&lt;P&gt;I have a dataset structured in the following manner.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data have;&lt;BR /&gt;input id date yymmdd6. code $ x $ price;&lt;BR /&gt;datalines;&lt;BR /&gt;1 121201 BC Grey 2&lt;BR /&gt;1 121202 BC Blue .&lt;BR /&gt;1 121203 BC Blue .&lt;BR /&gt;1 121204 BC Blue .&lt;BR /&gt;2 121101 CC Black 3&lt;BR /&gt;2 121102 CC Black 4&lt;BR /&gt;3 121101 DD Green 15&lt;BR /&gt;3 121102 DD Green .&lt;BR /&gt;3 121103 DD Green .&lt;BR /&gt;3 121104 DD Green .&lt;BR /&gt;3 121105 DD Green 16&lt;BR /&gt;3 121107 DD Green .&lt;BR /&gt;4 121110 KK Purple 100&lt;BR /&gt;4 121111 KK Purple 102&lt;BR /&gt;4 121111 KK Purple .&lt;BR /&gt;5 121110 MM Red .&lt;BR /&gt;5 121111 MM Red .&lt;BR /&gt;5 121112 MM Red .&lt;BR /&gt;5 121113 MM Red 30&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/PRE&gt;&lt;P&gt;I would like to remove the group variable (ID ) if there are 3(arbitrary- could be any number) consecutive missing prices&amp;nbsp; for that ID (forward-looking) and backward-looking .&lt;/P&gt;&lt;P&gt;Ideally the output would look like the following&amp;nbsp; (removing all ID group that has 3 consecutive forward or backward missing prices.&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data want; 
input id date yymmdd6. code $ x $ price;
datalines;

2 121101	CC Black 3
2 121102	CC Black 4
4 121110	KK Purple 100
4 121111	KK Purple 102
4 121111	KK Purple .
;
run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Not sure how complicated it is. Any help is greatly appreciated. Thanks.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 16 Jul 2022 02:59:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-groups-based-on-look-ahead-and-look-back-condition/m-p/823600#M325209</guid>
      <dc:creator>nonlinear999</dc:creator>
      <dc:date>2022-07-16T02:59:29Z</dc:date>
    </item>
    <item>
      <title>Re: Remove groups based on look-ahead and look-back condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-groups-based-on-look-ahead-and-look-back-condition/m-p/823603#M325212</link>
      <description>&lt;P&gt;Just use a double DOW loop.&amp;nbsp; The first one to find the longest run of missing prices.&lt;/P&gt;
&lt;P&gt;The second one to re-read the data for that ID and output the values for the ID's you want to keep.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use the NOTSORTED keyword on the BY statement to allow you to group by ID PRICE.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input id date :yymmdd6. code $ color $ price;
  format date yymmdd10.;
datalines;
1 121201 BC Grey 2
1 121202 BC Blue .
1 121203 BC Blue .
1 121204 BC Blue .
2 121101 CC Black 3
2 121102 CC Black 4
3 121101 DD Green 15
3 121102 DD Green .
3 121103 DD Green .
3 121104 DD Green .
3 121105 DD Green 16
3 121107 DD Green .
4 121110 KK Purple 100
4 121111 KK Purple 102
4 121111 KK Purple .
5 121110 MM Red .
5 121111 MM Red .
5 121112 MM Red .
5 121113 MM Red 30
;

data want;
  do until (last.id);
    do n=1 by 1 until (last.price) ;
      set have ;
      by id price notsorted ;
    end;
    if missing(price) then maxrun=max(maxrun,n);
  end;
  do until (last.id);
    set have ;
    by id ;
    if maxrun &amp;lt; 3 then output;
  end;
  drop n maxrun ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;Obs    id          date    code    color     price

 1      2    2012-11-01     CC     Black        3
 2      2    2012-11-02     CC     Black        4
 3      4    2012-11-10     KK     Purple     100
 4      4    2012-11-11     KK     Purple     102
 5      4    2012-11-11     KK     Purple       .
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 16 Jul 2022 03:26:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-groups-based-on-look-ahead-and-look-back-condition/m-p/823603#M325212</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-07-16T03:26:07Z</dc:date>
    </item>
  </channel>
</rss>

