<?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: Select records based on value sequence in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Select-records-based-on-value-sequence/m-p/480012#M124011</link>
    <description>&lt;P&gt;One approach:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;start_here = 0;&lt;/P&gt;
&lt;P&gt;recnum = 0;&lt;/P&gt;
&lt;P&gt;do until (last.id);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;by id value notsorted;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;recnum + 1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;if value ne 0 then start_here = 0;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;else if first.value then start_here = recnum;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;recnum = 0;&lt;/P&gt;
&lt;P&gt;do until (last.id);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;by id;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;recnum + 1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;if start_here &amp;gt; 0 and recnum &amp;gt;= start_here then output;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;drop recnum start_here;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's untested code, but looks like it should work.&lt;/P&gt;</description>
    <pubDate>Fri, 20 Jul 2018 18:29:56 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2018-07-20T18:29:56Z</dc:date>
    <item>
      <title>Select records based on value sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-records-based-on-value-sequence/m-p/479991#M124002</link>
      <description>&lt;P&gt;I want to select records from an existing dataset based on this logic: going through each record from top to bottom, locate the record with value 0 such that all ensuing records always have value of 0, select that record and everything before that record. In the following dataset, I'm making such selection for each type value, and my desired results are records with id 4,5,8,9,10. How do I achieve this? Thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;id&amp;nbsp; &amp;nbsp;type&amp;nbsp; &amp;nbsp;value&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;2&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;0&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;3&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;4&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;0&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;5&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;0&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;6&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;0&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;7&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;8&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;0&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;9&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;0&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;10&amp;nbsp; B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Jul 2018 17:56:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-records-based-on-value-sequence/m-p/479991#M124002</guid>
      <dc:creator>apolitical</dc:creator>
      <dc:date>2018-07-20T17:56:05Z</dc:date>
    </item>
    <item>
      <title>Re: Select records based on value sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-records-based-on-value-sequence/m-p/480012#M124011</link>
      <description>&lt;P&gt;One approach:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;start_here = 0;&lt;/P&gt;
&lt;P&gt;recnum = 0;&lt;/P&gt;
&lt;P&gt;do until (last.id);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;by id value notsorted;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;recnum + 1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;if value ne 0 then start_here = 0;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;else if first.value then start_here = recnum;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;recnum = 0;&lt;/P&gt;
&lt;P&gt;do until (last.id);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;by id;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;recnum + 1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;if start_here &amp;gt; 0 and recnum &amp;gt;= start_here then output;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;drop recnum start_here;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's untested code, but looks like it should work.&lt;/P&gt;</description>
      <pubDate>Fri, 20 Jul 2018 18:29:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-records-based-on-value-sequence/m-p/480012#M124011</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-07-20T18:29:56Z</dc:date>
    </item>
    <item>
      <title>Re: Select records based on value sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-records-based-on-value-sequence/m-p/480014#M124013</link>
      <description>&lt;P&gt;same idea, another variant:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id   type $  value;
cards;
1    A        1
2    A        0
3    A        1
4    A        0
5    A        0
6    B        0
7    B        1
8    B        0
9    B        0
10  B        0
;

data want;
_k=.;
do _n=1 by 1 until(last.type);
set have;
by type;
if value then _k=_n;
end;
do _n_=1 by 1 until(last.type);
set have;
by type;
if  _n_&amp;gt;_k then output;
end;
drop _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 20 Jul 2018 18:35:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-records-based-on-value-sequence/m-p/480014#M124013</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-07-20T18:35:41Z</dc:date>
    </item>
    <item>
      <title>Re: Select records based on value sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-records-based-on-value-sequence/m-p/480022#M124015</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id   type $  value;
cards;
1    A        1
2    A        0
3    A        1
4    A        0
5    A        0
6    B        0
7    B        1
8    B        0
9    B        0
10  B        0
;
proc sql;
create table want(drop=_:) as
select * 
from (select *,value=1 as _v, max(id) as _m from have group by type,_v)
group by type
having id&amp;gt;max((_v=1)*_m)
order by id, type;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 20 Jul 2018 18:54:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-records-based-on-value-sequence/m-p/480022#M124015</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-07-20T18:54:00Z</dc:date>
    </item>
    <item>
      <title>Re: Select records based on value sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-records-based-on-value-sequence/m-p/480139#M124058</link>
      <description>&lt;P&gt;If I understood what you mean.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id   type $  value;
cards;
1    A        1
2    A        0
3    A        1
4    A        0
5    A        0
6    B        0
7    B        1
8    B        0
9    B        0
10  B        0
;
run;

data temp;
 set have;
 by type value notsorted;
 group+first.value;
 if value=0;
run;
data want;
 set temp;
 by type group;
 if first.type then _group=0;
 _group+first.group;
 if _group ne 1 ;
 drop group _group;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 21 Jul 2018 11:43:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-records-based-on-value-sequence/m-p/480139#M124058</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-07-21T11:43:20Z</dc:date>
    </item>
    <item>
      <title>Re: Select records based on value sequence</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-records-based-on-value-sequence/m-p/480580#M124216</link>
      <description>&lt;P&gt;I'm sure all suggested answers work fine. I chose the one I could understand the most easily as the answer. Thank you all.&lt;/P&gt;</description>
      <pubDate>Mon, 23 Jul 2018 18:31:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-records-based-on-value-sequence/m-p/480580#M124216</guid>
      <dc:creator>apolitical</dc:creator>
      <dc:date>2018-07-23T18:31:28Z</dc:date>
    </item>
  </channel>
</rss>

