<?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: Getting a subset of a dataset and including previous record for each in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Getting-a-subset-of-a-dataset-and-including-previous-record-for/m-p/956247#M373425</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/264750"&gt;@RandoDando&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;That seemed to work and will work for the time being, but is there a way to modify it to avoid records being duplicated?&amp;nbsp; Seems as though if a record matches the criteria AND is prior to a record which does it shows up twice.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;In that case it might just be easier to do it two steps.&amp;nbsp; One to flag the records and then a second step to make the subset.&amp;nbsp; You can use one of the various methods for "looking ahead" to see if the next record matched the criteria.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data make_flag;
  set sashelp.class;
  match = (age=12);
run;

data want ;
  obsno+1;
  merge make_flag make_flag(firstobs=2 keep=match rename=(match=next_match));
  * DO NOT USE A BY STATEMENT ;
  if match or next_match;
run;

proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the criteria depends only a few variables then just use the look ahead trick on those variables and you can do it in one pass.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  obsno+1;
  merge sashelp.class sashelp.class(firstobs=2 keep=age rename=(age=next_age));
  * DO NOT USE A BY STATEMENT ;
  if (age=12) or (next_age=12);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 15 Jan 2025 21:21:41 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2025-01-15T21:21:41Z</dc:date>
    <item>
      <title>Getting a subset of a dataset and including previous record for each</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-a-subset-of-a-dataset-and-including-previous-record-for/m-p/956241#M373421</link>
      <description>&lt;P&gt;I want to create a subset from a data set to find records which meet a certain set of criteria (e.g. fieldA &amp;gt; 1,000 AND fieldB &amp;lt;1, etc...) AND include the record prior to those for each according to how the dataset is currently sorted.&amp;nbsp; How do I do that? In other words, if record number 100 fits the criteria, I want to include record 99, etc...&amp;nbsp; I want to retain the order of the data.&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jan 2025 19:15:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-a-subset-of-a-dataset-and-including-previous-record-for/m-p/956241#M373421</guid>
      <dc:creator>RandoDando</dc:creator>
      <dc:date>2025-01-15T19:15:10Z</dc:date>
    </item>
    <item>
      <title>Re: Getting a subset of a dataset and including previous record for each</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-a-subset-of-a-dataset-and-including-previous-record-for/m-p/956243#M373422</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
   set sashelp.class(keep=age);
   if age eq 12 then do;
      grp = _n_;
      do point=_n_-1 to _n_;
         obs = point;
         set sashelp.class point=point;
         output;
         end;
      end;
   run;
proc print;
   run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 300px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/103734i1FF64A95061FB9ED/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jan 2025 19:25:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-a-subset-of-a-dataset-and-including-previous-record-for/m-p/956243#M373422</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2025-01-15T19:25:27Z</dc:date>
    </item>
    <item>
      <title>Re: Getting a subset of a dataset and including previous record for each</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-a-subset-of-a-dataset-and-including-previous-record-for/m-p/956245#M373423</link>
      <description>That did it.  Thanks.</description>
      <pubDate>Wed, 15 Jan 2025 19:39:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-a-subset-of-a-dataset-and-including-previous-record-for/m-p/956245#M373423</guid>
      <dc:creator>RandoDando</dc:creator>
      <dc:date>2025-01-15T19:39:46Z</dc:date>
    </item>
    <item>
      <title>Re: Getting a subset of a dataset and including previous record for each</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-a-subset-of-a-dataset-and-including-previous-record-for/m-p/956246#M373424</link>
      <description>&lt;P&gt;That seemed to work and will work for the time being, but is there a way to modify it to avoid records being duplicated?&amp;nbsp; Seems as though if a record matches the criteria AND is prior to a record which does it shows up twice.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jan 2025 19:44:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-a-subset-of-a-dataset-and-including-previous-record-for/m-p/956246#M373424</guid>
      <dc:creator>RandoDando</dc:creator>
      <dc:date>2025-01-15T19:44:34Z</dc:date>
    </item>
    <item>
      <title>Re: Getting a subset of a dataset and including previous record for each</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-a-subset-of-a-dataset-and-including-previous-record-for/m-p/956247#M373425</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/264750"&gt;@RandoDando&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;That seemed to work and will work for the time being, but is there a way to modify it to avoid records being duplicated?&amp;nbsp; Seems as though if a record matches the criteria AND is prior to a record which does it shows up twice.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;In that case it might just be easier to do it two steps.&amp;nbsp; One to flag the records and then a second step to make the subset.&amp;nbsp; You can use one of the various methods for "looking ahead" to see if the next record matched the criteria.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data make_flag;
  set sashelp.class;
  match = (age=12);
run;

data want ;
  obsno+1;
  merge make_flag make_flag(firstobs=2 keep=match rename=(match=next_match));
  * DO NOT USE A BY STATEMENT ;
  if match or next_match;
run;

proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the criteria depends only a few variables then just use the look ahead trick on those variables and you can do it in one pass.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  obsno+1;
  merge sashelp.class sashelp.class(firstobs=2 keep=age rename=(age=next_age));
  * DO NOT USE A BY STATEMENT ;
  if (age=12) or (next_age=12);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Jan 2025 21:21:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-a-subset-of-a-dataset-and-including-previous-record-for/m-p/956247#M373425</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-01-15T21:21:41Z</dc:date>
    </item>
    <item>
      <title>Re: Getting a subset of a dataset and including previous record for each</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Getting-a-subset-of-a-dataset-and-including-previous-record-for/m-p/956259#M373427</link>
      <description>&lt;P&gt;You could keep track of whether the previous record has been output.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data test;
  set sashelp.class;
  if age eq 12 then do;
    if prevBeenOutput then output;
    else do;
      do point = max(_n_-1, 1) to _n_;
        set sashelp.class point=point;
        output;
      end;
      prevBeenOutput = 1;
    end;
  end;
  else prevBeenOutput = 0;

  retain prevBeenOutput;
  drop prevBeenOutput;
run;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Jan 2025 22:34:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Getting-a-subset-of-a-dataset-and-including-previous-record-for/m-p/956259#M373427</guid>
      <dc:creator>simonbun</dc:creator>
      <dc:date>2025-01-15T22:34:27Z</dc:date>
    </item>
  </channel>
</rss>

