<?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 Remove group observations if occur before conditions in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Remove-group-observations-if-occur-before-conditions/m-p/594236#M170683</link>
    <description>&lt;P&gt;I have data:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&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 have;
	input id type $ date $;
datalines;
1	c 2019-08-01
1	a 2019-08-04
1 	b 2019-08-05
1 	c 2019-08-11
2	c 2019-10-13
2	c 2019-10-14
2	a 2019-10-20
2       b 2019-10-25
;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to remove observations that occur before type a, if they are not type a. Therefore, each ID should have their first observation being equal to type a.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Desired output:&lt;/P&gt;&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 want;
	input id type $ date $;
datalines;
1	a 2019-08-04
1 	b 2019-08-05
1 	c 2019-08-11
2	a 2019-10-20
2      b 2019-10-25
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 04 Oct 2019 21:17:41 GMT</pubDate>
    <dc:creator>MB_Analyst</dc:creator>
    <dc:date>2019-10-04T21:17:41Z</dc:date>
    <item>
      <title>Remove group observations if occur before conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-group-observations-if-occur-before-conditions/m-p/594236#M170683</link>
      <description>&lt;P&gt;I have data:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&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 have;
	input id type $ date $;
datalines;
1	c 2019-08-01
1	a 2019-08-04
1 	b 2019-08-05
1 	c 2019-08-11
2	c 2019-10-13
2	c 2019-10-14
2	a 2019-10-20
2       b 2019-10-25
;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to remove observations that occur before type a, if they are not type a. Therefore, each ID should have their first observation being equal to type a.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Desired output:&lt;/P&gt;&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 want;
	input id type $ date $;
datalines;
1	a 2019-08-04
1 	b 2019-08-05
1 	c 2019-08-11
2	a 2019-10-20
2      b 2019-10-25
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Oct 2019 21:17:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-group-observations-if-occur-before-conditions/m-p/594236#M170683</guid>
      <dc:creator>MB_Analyst</dc:creator>
      <dc:date>2019-10-04T21:17:41Z</dc:date>
    </item>
    <item>
      <title>Re: Remove group observations if occur before conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-group-observations-if-occur-before-conditions/m-p/594238#M170685</link>
      <description>&lt;P&gt;Assuming your data set is already sorted by ID:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   del_flag = 'N';
   do until (last.id);
      set have;
      by ID;
      if type='a' then del_flag = 'Y';
   end;
   do until (last.id);
      set have;
      by ID;
      if type='a' then del_flag = 'N';
      if del_flag = 'N' then output;
   end;
   drop del_flag;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This assumes that if an ID has no type "a" observations, you want to keep all its observations.&amp;nbsp; It's actually simpler if you want to delete them all instead:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   del_flag = 'N';
   do until (last.id);
      set have;
      by ID;
      if type='a' then del_flag = 'N';
      if del_flag = 'N' then output;
   end;
   drop del_flag;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 04 Oct 2019 21:29:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-group-observations-if-occur-before-conditions/m-p/594238#M170685</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-10-04T21:29:10Z</dc:date>
    </item>
    <item>
      <title>Re: Remove group observations if occur before conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-group-observations-if-occur-before-conditions/m-p/594241#M170688</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/182623"&gt;@MB_Analyst&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;Try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                                                                                                                                                                                                                                                     
  input id type $ date :$10. ;                                                                                                                                                                                                                                  
datalines;                                                                                                                                                                                                                                                      
1  c  2019-08-01                                                                                                                                                                                                                                                
1  a  2019-08-04                                                                                                                                                                                                                                                
1  b  2019-08-05                                                                                                                                                                                                                                                
1  c  2019-08-11                                                                                                                                                                                                                                                
2  c  2019-10-13                                                                                                                                                                                                                                                
2  c  2019-10-14                                                                                                                                                                                                                                                
2  a  2019-10-20                                                                                                                                                                                                                                                
2  b  2019-10-25                                                                                                                                                                                                                                                
;                                                                                                                                                                                                                                                               
run ;                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                
data want ;                                                                                                                                                                                                                                                     
  do _n_ = 0 by 0 until (last.id) ;                                                                                                                                                                                                                             
    set have ;                                                                                                                                                                                                                                                  
    by id ;                                                                                                                                                                                                                                                     
    if type = "a" then _n_ = 1 ;                                                                                                                                                                                                                                
    if _n_ then output ;                                                                                                                                                                                                                                        
  end ;                                                                                                                                                                                                                                                         
run ;                     
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Oct 2019 21:34:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-group-observations-if-occur-before-conditions/m-p/594241#M170688</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-10-04T21:34:09Z</dc:date>
    </item>
  </channel>
</rss>

