<?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: Creating subset of observations based on a varaible in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-observations-based-on-a-varaible/m-p/566003#M159008</link>
    <description>&lt;P&gt;For the 1 record, does it matter which one? I noticed for the loan status you said last two but in your example here you actually picked the first two. Can you confirm? Its much easier if it's the first two rather than last two - because you don't know how many there are until you're done.&lt;/P&gt;</description>
    <pubDate>Thu, 13 Jun 2019 19:51:12 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2019-06-13T19:51:12Z</dc:date>
    <item>
      <title>Creating subset of observations based on a varaible</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-observations-based-on-a-varaible/m-p/565921#M158957</link>
      <description>&lt;P&gt;Hello all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to create subset of observations based on a variable limiting the count to 2 or 1 depending on the value in that variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; infile want_file;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; input&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; @ 001&amp;nbsp;ln_no&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 9.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@ 010&amp;nbsp;ln_status&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; $1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; @ 011 ln_excep_stat&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;$1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ln_status can have values of '1', 2', '3', '4' or '5'&amp;nbsp;&amp;nbsp;;&lt;/P&gt;
&lt;P&gt;ln_excep_stat can have values of&amp;nbsp; '1', '2', '3' or '4';&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If ln_excep_stat is '2', '3' or '4' and ln_stat is '2', '3' or '4', need to limit the observations to 2 obs if there are more than 2 per ln_no.&lt;/P&gt;
&lt;P&gt;If ln_excep_stat is '1' or '5' and ln_stat is '1' or '5', need to limit to 1 if there are more than 2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I really appreciate&amp;nbsp;your time and effort on this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;thank you in advance,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Neal.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jun 2019 16:48:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-observations-based-on-a-varaible/m-p/565921#M158957</guid>
      <dc:creator>saslovethemost</dc:creator>
      <dc:date>2019-06-13T16:48:03Z</dc:date>
    </item>
    <item>
      <title>Re: Creating subset of observations based on a varaible</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-observations-based-on-a-varaible/m-p/565925#M158960</link>
      <description>&lt;P&gt;Please provide the data, using these instructions:&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do not provide the data in any other form.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, show us the code you have tried already.&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jun 2019 16:51:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-observations-based-on-a-varaible/m-p/565925#M158960</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-06-13T16:51:03Z</dc:date>
    </item>
    <item>
      <title>Re: Creating subset of observations based on a varaible</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-observations-based-on-a-varaible/m-p/565963#M158982</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data combdla;
 infile datalines dsd truncover;
 input ln_no:9. ln_status:$1. ln_excep_stat:$1. name:$4.;
 datalines;
 999114964 2 3 NEAL
 999114964 2 3 DAVE
 999114964 2 3 PHIL
;

data combdlq;
  set combdla;
  if ln_status=('2' or '3' or '4') and
     ln_excep_stat=('2' or '3' or '4') then
     do;
         noobs=2;
    end;
  else ln_status=('1' or '5') and
          ln_excep_stat =('1' or '5')
          do;
              noobs=2;
          end;
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I have tried the above code, not getting the right results. is there a way that SAS can do it in a different way than structured language like COBOL?&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jun 2019 18:38:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-observations-based-on-a-varaible/m-p/565963#M158982</guid>
      <dc:creator>saslovethemost</dc:creator>
      <dc:date>2019-06-13T18:38:32Z</dc:date>
    </item>
    <item>
      <title>Re: Creating subset of observations based on a varaible</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-observations-based-on-a-varaible/m-p/565965#M158983</link>
      <description>&lt;P&gt;sorry, in the second data statement should be like the following:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data combdlq;
  set combdla;
  if ln_status=('2' or '3' or '4') and
     ln_excep_stat=('2' or '3' or '4') then
     do;
         noobs=2;
    end;
  else if ln_status=('1' or '5') and
             ln_excep_stat =('1' or '5')
          do;
              noobs=1;
          end;
  else
       do;
             noobs =1;
       end; 
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 13 Jun 2019 18:41:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-observations-based-on-a-varaible/m-p/565965#M158983</guid>
      <dc:creator>saslovethemost</dc:creator>
      <dc:date>2019-06-13T18:41:59Z</dc:date>
    </item>
    <item>
      <title>Re: Creating subset of observations based on a varaible</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-observations-based-on-a-varaible/m-p/565969#M158986</link>
      <description>&lt;P&gt;It's still not clear to me what the requirement "&lt;SPAN&gt;need to limit the observations to 2 obs if there are more than 2 per ln_no" means. Show us the desired output.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jun 2019 18:47:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-observations-based-on-a-varaible/m-p/565969#M158986</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-06-13T18:47:39Z</dc:date>
    </item>
    <item>
      <title>Re: Creating subset of observations based on a varaible</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-observations-based-on-a-varaible/m-p/565971#M158987</link>
      <description>I think I am trying to get 2 observations out of 3 input datalines.&lt;BR /&gt;&lt;BR /&gt;output should be like below:&lt;BR /&gt; 999114964 2 3 NEAL&lt;BR /&gt; 999114964 2 3 DAVE</description>
      <pubDate>Thu, 13 Jun 2019 18:51:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-observations-based-on-a-varaible/m-p/565971#M158987</guid>
      <dc:creator>saslovethemost</dc:creator>
      <dc:date>2019-06-13T18:51:21Z</dc:date>
    </item>
    <item>
      <title>Re: Creating subset of observations based on a varaible</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-observations-based-on-a-varaible/m-p/565972#M158988</link>
      <description>&lt;P&gt;It's very tough to generalize from one example, so that code can be written that works in all cases. Are there rules that determine why you pick NEAL and DAVE in this case, and not two other records?&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jun 2019 18:56:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-observations-based-on-a-varaible/m-p/565972#M158988</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-06-13T18:56:53Z</dc:date>
    </item>
    <item>
      <title>Re: Creating subset of observations based on a varaible</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-observations-based-on-a-varaible/m-p/565981#M158993</link>
      <description>It would be really helpful if you could lay out your problem. Show us what you have, what you've tried and what you expect as output. &lt;BR /&gt;&lt;BR /&gt;Currently, your input looks like your output with the exception of the last record. But how did you know you need 2 vs 1?&lt;BR /&gt;&lt;BR /&gt;I would also argue that this would be much easier to first read in and then filter or at least control the output. Is it guaranteed that the ln_status will be the same across the IDs? What uniquely identifies each group?</description>
      <pubDate>Thu, 13 Jun 2019 19:20:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-observations-based-on-a-varaible/m-p/565981#M158993</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-06-13T19:20:29Z</dc:date>
    </item>
    <item>
      <title>Re: Creating subset of observations based on a varaible</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-observations-based-on-a-varaible/m-p/565994#M159000</link>
      <description>&lt;P&gt;Sounds like you want to group the data by LN_NO&amp;nbsp;variable.&lt;/P&gt;
&lt;P&gt;Then keep either the first or the first two observations in that group based on the value of&amp;nbsp;LN_STATUS and&amp;nbsp;LN_EXCEP_STAT.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is a method using the value of LN_STATUS and LN_EXCEP_STAT&amp;nbsp;on the FIRST observation in the group.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have ;
  by LN_NO;
  retain row maxrow;
  if first.ln_no then do;
      row=1;
      if ln_excep_stat in ('1' '5') and ln_stat in ('1' '5') then maxrow=1;
      else maxrow=2;
  end;
  else row+1;
  if row &amp;lt;= maxrow;
run;
   
     &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 13 Jun 2019 19:42:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-observations-based-on-a-varaible/m-p/565994#M159000</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-06-13T19:42:32Z</dc:date>
    </item>
    <item>
      <title>Re: Creating subset of observations based on a varaible</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-observations-based-on-a-varaible/m-p/565995#M159001</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data cobdla;
   infile datalines dsd truncover;
   input ln_no:9. ln_status:$1. ln_excep_stat:$1. name:$4.;
   datalines;
            999114961 2 3 NEAL
            999114961 2 3 DAVE
            999114961 2 3 PHIL
            999114962 3 4 JOHN
            999114962 3 4 RICK
            999114962 3 4 LILI
            999114963 4 5 JOHN
            999114963 4 5 RICK
            999114963 4 5 LILI
            999114964 1 5 JOHN
            999114964 1 5 RICK
;
sorry&amp;nbsp;for the confusion&amp;nbsp;I&amp;nbsp;created,&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Here&amp;nbsp;is&amp;nbsp;what&amp;nbsp;I&amp;nbsp;want:&amp;nbsp;last&amp;nbsp;two&amp;nbsp;records&amp;nbsp;for&amp;nbsp;each&amp;nbsp;ln_no&amp;nbsp;if&amp;nbsp;the&amp;nbsp;ln_status&amp;nbsp;and&amp;nbsp;ln_excep_stat&amp;nbsp;is&amp;nbsp;in&amp;nbsp;'2'&amp;nbsp;'3'&amp;nbsp;or&amp;nbsp;'4',&amp;nbsp;if&amp;nbsp;they&amp;nbsp;are&amp;nbsp;'1'&amp;nbsp;and&amp;nbsp;'5'&amp;nbsp;I&amp;nbsp;want&amp;nbsp;only&amp;nbsp;1&amp;nbsp;record.&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jun 2019 19:42:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-observations-based-on-a-varaible/m-p/565995#M159001</guid>
      <dc:creator>saslovethemost</dc:creator>
      <dc:date>2019-06-13T19:42:51Z</dc:date>
    </item>
    <item>
      <title>Re: Creating subset of observations based on a varaible</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-observations-based-on-a-varaible/m-p/565999#M159004</link>
      <description>&lt;P&gt;A hint for using in-line data in a data step.&amp;nbsp; &lt;STRONG&gt;Do not indent the data.&lt;/STRONG&gt;&amp;nbsp; Figuring out what column to read what column is causing errors, editing etc. will be much harder.&amp;nbsp; To make it easier also do not indent the DATALINES (or CARDS) statement that comes before that data or the semi-colon line (or four semi-colon line) that marks the end of the data (and the end of the data step).&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jun 2019 19:49:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-observations-based-on-a-varaible/m-p/565999#M159004</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-06-13T19:49:09Z</dc:date>
    </item>
    <item>
      <title>Re: Creating subset of observations based on a varaible</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-observations-based-on-a-varaible/m-p/566003#M159008</link>
      <description>&lt;P&gt;For the 1 record, does it matter which one? I noticed for the loan status you said last two but in your example here you actually picked the first two. Can you confirm? Its much easier if it's the first two rather than last two - because you don't know how many there are until you're done.&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jun 2019 19:51:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-observations-based-on-a-varaible/m-p/566003#M159008</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-06-13T19:51:12Z</dc:date>
    </item>
    <item>
      <title>Re: Creating subset of observations based on a varaible</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-observations-based-on-a-varaible/m-p/566010#M159010</link>
      <description>&lt;P&gt;for the 1 record, I want the bottom one, for the two records, I want to have bottom two records.&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jun 2019 19:58:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-observations-based-on-a-varaible/m-p/566010#M159010</guid>
      <dc:creator>saslovethemost</dc:creator>
      <dc:date>2019-06-13T19:58:16Z</dc:date>
    </item>
    <item>
      <title>Re: Creating subset of observations based on a varaible</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-observations-based-on-a-varaible/m-p/566011#M159011</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/51355"&gt;@saslovethemost&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;for the 1 record, I want the bottom one, for the two records, I want to have bottom two records.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Your example above was incorrect then or is there some other variable that indicates order?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Either way, here's one long winded approach, yes there are easier ways.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ata cobdla;
set cobdla;
by ln_no;
if first.ln_no then count=1;
else count+1;
run;

proc sort data=cobdla; 
by ln_no descending count;
run;

data want;
set cobdla;
by ln_no;
retain max;

if first.ln_no then max=count;

if (ln_status in ('2','3','4') or ln_excep_stat in ('2','3','4')) and count &amp;gt;= (max - 1) then flag=1;
else if (ln_status in ('1', '5') or ln_excep_stat in ('1','5'))  and count=max then flag=2;
else flag=3;

if flag in (1,2);

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 13 Jun 2019 20:01:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-observations-based-on-a-varaible/m-p/566011#M159011</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-06-13T20:01:49Z</dc:date>
    </item>
    <item>
      <title>Re: Creating subset of observations based on a varaible</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-observations-based-on-a-varaible/m-p/566023#M159016</link>
      <description>&lt;P&gt;Thanks Tom for the reply, sorry I misguided you with my code, but I do not want the no of obs values, I want the actual data mentioned in the datalines but subset of it.&amp;nbsp;For a ln_no if there are more than 2 records, I want to retain last 2 records for ln_stat and ln_excep_status '2' or '3' or '4' . same way, I want to retain&amp;nbsp;1 record&amp;nbsp;if ln_stat and ln_excep_status '1' or '5'&amp;nbsp; if there are more than 1 record. If there is only one record, I want to retain that one record. sorry for all the confussion.&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jun 2019 20:09:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-observations-based-on-a-varaible/m-p/566023#M159016</guid>
      <dc:creator>saslovethemost</dc:creator>
      <dc:date>2019-06-13T20:09:31Z</dc:date>
    </item>
    <item>
      <title>Re: Creating subset of observations based on a varaible</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-observations-based-on-a-varaible/m-p/566024#M159017</link>
      <description>&lt;P&gt;Thanks Reeza for the reply., please see the reply I sent to Tom.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I pasted it below for your reference.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;"Thanks Tom for the reply, sorry I misguided you with my code, but I do not want the no of obs values, I want the actual data mentioned in the datalines but subset of it.&amp;nbsp;For a ln_no if there are more than 2 records, I want to retain last 2 records for ln_stat and ln_excep_status '2' or '3' or '4' . same way, I want to retain&amp;nbsp;1 record&amp;nbsp;if ln_stat and ln_excep_status '1' or '5'&amp;nbsp; if there are more than 1 record. If there is only one record, I want to retain that one record. sorry for all the confusion".&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jun 2019 20:13:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-observations-based-on-a-varaible/m-p/566024#M159017</guid>
      <dc:creator>saslovethemost</dc:creator>
      <dc:date>2019-06-13T20:13:06Z</dc:date>
    </item>
    <item>
      <title>Re: Creating subset of observations based on a varaible</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-observations-based-on-a-varaible/m-p/566029#M159019</link>
      <description>&lt;P&gt;Your specs are still missing a number of details.&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Does the same id always have the same values of the two status variables?&amp;nbsp; If not then how does you logic for how many observations to keep change?&lt;/LI&gt;
&lt;LI&gt;Do you want only one row kept if BOTH status variables are 1 or 5?&amp;nbsp; Or if EITHER status variable is 1 or 5?&lt;/LI&gt;
&lt;LI&gt;If the order is important why not just sort the data in descending order do that you can pick the first one or two instead of the last one or two.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is tradition to use WANT as the output dataset name and HAVE as the input dataset name in examples.&amp;nbsp; Replace those with the names of your actual data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Don't make your life harder by trying to combine the step that converts the raw text into a dataset with the logic to subset it.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the data is really large (and what is considered large changes as computers get more powerful) you could use a data step view to read the data so that there is actually only one pass thru the data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you did want the LAST one or two then just change the logic a little.&amp;nbsp; You can use what is call DOW loops to make that easier.&amp;nbsp; The first DO loop counts the number of rows in the group.&amp;nbsp; Then second reads the group in again and writes out which records you want.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  do maxrow = 1 by 1 until (last. LN_NO);
    set have ;
    by LN_NO;
  end;
  if ln_excep_stat in ('1' '5') and ln_stat in ('1' '5') then keeprows=1;
  else keeprows=2;
  do row = 1 by 1 until (last. LN_NO);
    set have ;
    by LN_NO;
    if row &amp;gt; (maxrow - keeprows) then output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jun 2019 20:30:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-observations-based-on-a-varaible/m-p/566029#M159019</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-06-13T20:30:52Z</dc:date>
    </item>
    <item>
      <title>Re: Creating subset of observations based on a varaible</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-observations-based-on-a-varaible/m-p/566039#M159023</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/51355"&gt;@saslovethemost&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks Reeza for the reply., please see the reply I sent to Tom.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I pasted it below for your reference.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;"Thanks Tom for the reply, sorry I misguided you with my code, but I do not want the no of obs values, I want the actual data mentioned in the datalines but subset of it.&amp;nbsp;For a ln_no if there are more than 2 records, I want to retain last 2 records for ln_stat and ln_excep_status '2' or '3' or '4' . same way, I want to retain&amp;nbsp;1 record&amp;nbsp;if ln_stat and ln_excep_status '1' or '5'&amp;nbsp; if there are more than 1 record. If there is only one record, I want to retain that one record. sorry for all the confusion".&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That's exactly what my code does. Did you run it and check the output?&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jun 2019 20:41:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-observations-based-on-a-varaible/m-p/566039#M159023</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-06-13T20:41:21Z</dc:date>
    </item>
    <item>
      <title>Re: Creating subset of observations based on a varaible</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-observations-based-on-a-varaible/m-p/566156#M159068</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/51355"&gt;@saslovethemost&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;for the 1 record, I want the bottom one, for the two records, I want to have bottom two records.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;This disagrees with your earlier answer to my question about which two records you want to keep, in which you said you wanted to keep NEAL and DAVE. But the bottom two records are DAVE and PHIL. So, I still don't know what the right answer is that you want from the code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Jun 2019 12:24:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-subset-of-observations-based-on-a-varaible/m-p/566156#M159068</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-06-14T12:24:46Z</dc:date>
    </item>
  </channel>
</rss>

