<?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 How to get the observations for subsequent values based on a condition in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-observations-for-subsequent-values-based-on-a/m-p/602734#M174537</link>
    <description>&lt;P&gt;Can anyone help how to get the observations based on the below condition:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If 3 consecutive "N" is found in column C for a same value in A column&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Eg: A1B4 A1B5 and A1B6 has 3 consecutive N's and I need all three in the output&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;A B C
1 1 N
1 2 N
1 3 Y
1 4 N
1 5 N
1 6 N
1 7 Y
1 8 Y
1 9 N
2 1 Y
2 2 N
2 3 N
2 4 N
2 5 N
2 6 N
2 7 N
2 8 Y&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Expected output:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;A B C
1 4 N
1 5 N
1 6 N
2 2 N
2 3 N
2 4 N
2 5 N
2 6 N
2 7 N&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 08 Nov 2019 14:28:36 GMT</pubDate>
    <dc:creator>jksthomas</dc:creator>
    <dc:date>2019-11-08T14:28:36Z</dc:date>
    <item>
      <title>How to get the observations for subsequent values based on a condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-observations-for-subsequent-values-based-on-a/m-p/602734#M174537</link>
      <description>&lt;P&gt;Can anyone help how to get the observations based on the below condition:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If 3 consecutive "N" is found in column C for a same value in A column&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Eg: A1B4 A1B5 and A1B6 has 3 consecutive N's and I need all three in the output&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;A B C
1 1 N
1 2 N
1 3 Y
1 4 N
1 5 N
1 6 N
1 7 Y
1 8 Y
1 9 N
2 1 Y
2 2 N
2 3 N
2 4 N
2 5 N
2 6 N
2 7 N
2 8 Y&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Expected output:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;A B C
1 4 N
1 5 N
1 6 N
2 2 N
2 3 N
2 4 N
2 5 N
2 6 N
2 7 N&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Nov 2019 14:28:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-observations-for-subsequent-values-based-on-a/m-p/602734#M174537</guid>
      <dc:creator>jksthomas</dc:creator>
      <dc:date>2019-11-08T14:28:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the observations for subsequent values based on a condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-observations-for-subsequent-values-based-on-a/m-p/602749#M174541</link>
      <description>One approach:&lt;BR /&gt;&lt;BR /&gt;data want;&lt;BR /&gt;k=0;&lt;BR /&gt;do until (last.c) ;&lt;BR /&gt;  set have;&lt;BR /&gt;  by a c notsorted;&lt;BR /&gt;  k + 1;&lt;BR /&gt;end; &lt;BR /&gt;do until (last.c) ;&lt;BR /&gt;  set have;&lt;BR /&gt;  by a c notsorted;&lt;BR /&gt;  if k &amp;gt;= 3 and c='N' then output;&lt;BR /&gt;end;&lt;BR /&gt;drop k;&lt;BR /&gt;run;</description>
      <pubDate>Fri, 08 Nov 2019 15:09:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-observations-for-subsequent-values-based-on-a/m-p/602749#M174541</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-11-08T15:09:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the observations for subsequent values based on a condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-observations-for-subsequent-values-based-on-a/m-p/602750#M174542</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/294624"&gt;@jksthomas&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input A B C $1.;
cards;
1 1 N
1 2 N
1 3 Y
1 4 N
1 5 N
1 6 N
1 7 Y
1 8 Y
1 9 N
2 1 Y
2 2 N
2 3 N
2 4 N
2 5 N
2 6 N
2 7 N
2 8 Y
;


data want;
 if _n_=1 then do;
 dcl hash H ();
 h.definekey  ("_n_");
 h.definedata ("_s",'_e');
 h.definedone ();
 dcl hiter hi('h');
 end;
 do _n_=1 by 1 until(last.a);
  set have;
  by a;
  if c='N' then	do;
   _count+1;
   if _count=1 then _s=B;
   _e=b;
  end;
  else do;
   if _count&amp;gt;=3 then do;
   h.add();
   end;
   _count=0;
  end;
 end;
 c='N';
 do while(hi.next()=0);
  do b=_s to _e;
   output;
  end;
 end;
 h.clear();
 drop _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 08 Nov 2019 15:11:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-observations-for-subsequent-values-based-on-a/m-p/602750#M174542</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-11-08T15:11:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the observations for subsequent values based on a condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-observations-for-subsequent-values-based-on-a/m-p/602751#M174543</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
input A B C $;
cards;
1 1 N
1 2 N
1 3 Y
1 4 N
1 5 N
1 6 N
1 7 Y
1 8 Y
1 9 N
2 1 Y
2 2 N
2 3 N
2 4 N
2 5 N
2 6 N
2 7 N
2 8 Y
;
run;

data want;
	cnt=0;
	do until(last.c);
		set have;
		by c notsorted;
		cnt+1;
	end;

	do until(last.c);
		set have;
		by c notsorted;
		if cnt &amp;gt;=3 then output;
	end;

	drop cnt;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 08 Nov 2019 15:23:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-observations-for-subsequent-values-based-on-a/m-p/602751#M174543</guid>
      <dc:creator>r_behata</dc:creator>
      <dc:date>2019-11-08T15:23:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the observations for subsequent values based on a condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-observations-for-subsequent-values-based-on-a/m-p/602977#M174644</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
input A B C $;
cards;
1 1 N
1 2 N
1 3 Y
1 4 N
1 5 N
1 6 N
1 7 Y
1 8 Y
1 9 N
2 1 Y
2 2 N
2 3 N
2 4 N
2 5 N
2 6 N
2 7 N
2 8 Y
;
run;
data temp;
 set have;
 by a c notsorted;
 group+first.c;
run;
proc sql;
create table want as
select * from temp group by group having count(*)&amp;gt;2 and sum(c='Y')=0;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 09 Nov 2019 12:35:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-observations-for-subsequent-values-based-on-a/m-p/602977#M174644</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-11-09T12:35:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to get the observations for subsequent values based on a condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-observations-for-subsequent-values-based-on-a/m-p/603068#M174701</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/294624"&gt;@jksthomas&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;You've got plenty of good offers, of which I'd prefer those based on the double DoW-loop and grouping by A C.&lt;/P&gt;
&lt;P&gt;Another approach can be:&lt;/P&gt;
&lt;P&gt;While passing through the file, memorize the observation number endpoints of each consecutive "N" sequence&lt;/P&gt;
&lt;P&gt;At the break of the sequence (i.e. when C="N" or LAST.A), if the distance between the endpoints GE 2, use them to output the needed records. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that below, I've included an extra BY-group (A=2) to test the condition when a qualifying "N" sequence ends at the end of a BY A group.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                                                                                                             
  input (A B C) (:$1.) ;                                                                                                                
  cards ;                                                                                                                               
1 1 N                                                                                                                                   
1 2 N                                                                                                                                   
1 3 Y                                                                                                                                   
1 4 N                                                                                                                                   
1 5 N                                                                                                                                   
1 6 N                                                                                                                                   
1 7 Y                                                                                                                                   
1 8 Y                                                                                                                                   
1 9 N                                                                                                                                   
2 0 N                                                                                                                                   
2 1 N                                                                                                                                   
2 2 Y                                                                                                                                   
2 3 N                                                                                                                                   
2 4 N                                                                                                                                   
2 5 N                                                                                                                                   
2 6 Y                                                                                                                                   
2 7 N                                                                                                                                   
2 8 N                                                                                                                                   
2 9 N                                                                                                                                   
3 1 Y                                                                                                                                   
3 2 N                                                                                                                                   
3 3 N                                                                                                                                   
3 4 N                                                                                                                                   
3 5 N                                                                                                                                   
3 6 N                                                                                                                                   
3 7 N                                                                                                                                   
3 8 Y                                                                                                                                   
3 8 Y                                                                                                                                   
;                                                                                                                                       
run ;                                                                                                                                   
                                                                                                                                        
data want ;                                                                                                                             
  set have ;                                                                                                                            
  by a ;                                                                                                                                
  if c eq "N" and not _iorc_ then _iorc_ = _n_ ;                                                                                        
  if (c ne "N" or last.a) and _iorc_ ;                                                                                                  
  if _n_ - not last.a - _iorc_ =&amp;gt; 2 then do p = _iorc_ to _n_ - not last.a ;                                                            
    set have point = p ;                                                                                                                
    output ;                                                                                                                            
  end ;                                                                                                                                 
  _iorc_ = 0 ;                                                                                                                          
run ;                                      
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;_IORC_ (auto-retained, auto-dropped, and set to 0 at compile) tracks the record numbers where the "N" sequences begin; _N_ tracks the record numbers where they terminate. This approach can be advantageous performance-wise if the number and size of the qualifying "N" sequences are small compared to the size of the file, as it thus avoids a full second read.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 10 Nov 2019 20:12:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-get-the-observations-for-subsequent-values-based-on-a/m-p/603068#M174701</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-11-10T20:12:46Z</dc:date>
    </item>
  </channel>
</rss>

