<?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: Remove observations by date with if condition in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/Remove-observations-by-date-with-if-condition/m-p/531466#M16430</link>
    <description>&lt;P&gt;What does it mean to say OilCount &amp;gt; 8 for a given date?&amp;nbsp; Is that OilCount on any observation for that date?&amp;nbsp; Is it the sum of all OilCounts for that date?&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is a framework you can use ... you can modify the conditions and add more if you would like.&amp;nbsp; Assuming you want to delete observations for the date when the total OilCount for that date is 8 or more:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;total_oil_count = 0;&lt;/P&gt;
&lt;P&gt;do until (last.date);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;by date;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;total_oil_count + oil_count;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;do until (last.date);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;by date;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;if total_oil_count &amp;lt; 8 then output;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;drop total_oil_count;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Some of the tricky aspects:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL style="list-style-position: inside;"&gt;
&lt;LI&gt;Use an OUTPUT statement to output the observations you want (not a DELETE statement to remove observations).&lt;/LI&gt;
&lt;LI&gt;The top loop reads all observations for a date and computes the conditions needed to determine output vs. delete.&lt;/LI&gt;
&lt;LI&gt;The bottom loop then re-reads the exact same observations, and uses the results of the top loop to output just the desired observations.&lt;/LI&gt;
&lt;LI&gt;If you have computed multiple conditions in the top loop, the bottom loop can still use the results ... but must still use an OUTPUT statement, not a DELETE statement.&amp;nbsp; Adjust the logic accordingly.&lt;/LI&gt;
&lt;/UL&gt;</description>
    <pubDate>Wed, 30 Jan 2019 19:12:03 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2019-01-30T19:12:03Z</dc:date>
    <item>
      <title>Remove observations by date with if condition</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Remove-observations-by-date-with-if-condition/m-p/531439#M16427</link>
      <description>&lt;P&gt;Hi, I'd like to remove all the observations of one date whenever the If condition meets. For example, if 'OilCount &amp;gt;= 8' then all the observations of that date are removed (rather than the observations that meet 'OilCount &amp;gt;= 24' within that date.) I appreciate any advice. Thank you.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input OilCount date ：ddmmyy10. ;
datalines;
0	03/09/2012
6	03/09/2012
0	04/09/2012
5	04/09/2012
1	04/09/2012
4	05/09/2012
5	06/09/2012
3	06/09/2012
6	06/09/2012
8	06/09/2012
11	07/09/2012
3	08/09/2012
2	08/09/2012
4	10/09/2012


run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
input OilCount date ：ddmmyy10. ;
datalines;
0	03/09/2012
6	03/09/2012
0	04/09/2012
5	04/09/2012
1	04/09/2012
4	05/09/2012
3	08/09/2012
2	08/09/2012
4	10/09/2012

run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 31 Jan 2019 16:16:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Remove-observations-by-date-with-if-condition/m-p/531439#M16427</guid>
      <dc:creator>Xusheng</dc:creator>
      <dc:date>2019-01-31T16:16:37Z</dc:date>
    </item>
    <item>
      <title>Re: Remove observations by date with if condition</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Remove-observations-by-date-with-if-condition/m-p/531451#M16428</link>
      <description>&lt;P&gt;You could make a simpler/smaller sample for dumb me to understand your requirement. Also post an output sample i.e expected requirement for the smaller input sample you may while explaining the logic&lt;/P&gt;</description>
      <pubDate>Wed, 30 Jan 2019 17:54:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Remove-observations-by-date-with-if-condition/m-p/531451#M16428</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-01-30T17:54:27Z</dc:date>
    </item>
    <item>
      <title>Re: Remove observations by date with if condition</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Remove-observations-by-date-with-if-condition/m-p/531454#M16429</link>
      <description>Ok, I will update with a smaller sample. Thank you. ##- Please type your&lt;BR /&gt;reply above this line. No attachments. -##</description>
      <pubDate>Wed, 30 Jan 2019 17:59:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Remove-observations-by-date-with-if-condition/m-p/531454#M16429</guid>
      <dc:creator>Xusheng</dc:creator>
      <dc:date>2019-01-30T17:59:12Z</dc:date>
    </item>
    <item>
      <title>Re: Remove observations by date with if condition</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Remove-observations-by-date-with-if-condition/m-p/531466#M16430</link>
      <description>&lt;P&gt;What does it mean to say OilCount &amp;gt; 8 for a given date?&amp;nbsp; Is that OilCount on any observation for that date?&amp;nbsp; Is it the sum of all OilCounts for that date?&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is a framework you can use ... you can modify the conditions and add more if you would like.&amp;nbsp; Assuming you want to delete observations for the date when the total OilCount for that date is 8 or more:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;total_oil_count = 0;&lt;/P&gt;
&lt;P&gt;do until (last.date);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;by date;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;total_oil_count + oil_count;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;do until (last.date);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;by date;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;if total_oil_count &amp;lt; 8 then output;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;drop total_oil_count;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Some of the tricky aspects:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL style="list-style-position: inside;"&gt;
&lt;LI&gt;Use an OUTPUT statement to output the observations you want (not a DELETE statement to remove observations).&lt;/LI&gt;
&lt;LI&gt;The top loop reads all observations for a date and computes the conditions needed to determine output vs. delete.&lt;/LI&gt;
&lt;LI&gt;The bottom loop then re-reads the exact same observations, and uses the results of the top loop to output just the desired observations.&lt;/LI&gt;
&lt;LI&gt;If you have computed multiple conditions in the top loop, the bottom loop can still use the results ... but must still use an OUTPUT statement, not a DELETE statement.&amp;nbsp; Adjust the logic accordingly.&lt;/LI&gt;
&lt;/UL&gt;</description>
      <pubDate>Wed, 30 Jan 2019 19:12:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Remove-observations-by-date-with-if-condition/m-p/531466#M16430</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-01-30T19:12:03Z</dc:date>
    </item>
    <item>
      <title>Re: Remove observations by date with if condition</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Remove-observations-by-date-with-if-condition/m-p/531691#M16434</link>
      <description>&lt;P&gt;Hi Novinosrin, I have updated the question with smaller sample of 'have' and 'want'. Please kindly let me know whether this sample makes sense or not.&lt;/P&gt;</description>
      <pubDate>Thu, 31 Jan 2019 16:18:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Remove-observations-by-date-with-if-condition/m-p/531691#M16434</guid>
      <dc:creator>Xusheng</dc:creator>
      <dc:date>2019-01-31T16:18:00Z</dc:date>
    </item>
    <item>
      <title>Re: Remove observations by date with if condition</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Remove-observations-by-date-with-if-condition/m-p/531694#M16435</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/114336"&gt;@Xusheng&lt;/a&gt;&amp;nbsp;, kind of you. Just waking up. Will have a coffee and take a look shortly&lt;/P&gt;</description>
      <pubDate>Thu, 31 Jan 2019 16:24:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Remove-observations-by-date-with-if-condition/m-p/531694#M16435</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-01-31T16:24:54Z</dc:date>
    </item>
    <item>
      <title>Re: Remove observations by date with if condition</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Remove-observations-by-date-with-if-condition/m-p/531698#M16436</link>
      <description>Thank you. Take your time##- Please type your reply above this line. No&lt;BR /&gt;attachments. -##</description>
      <pubDate>Thu, 31 Jan 2019 16:27:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Remove-observations-by-date-with-if-condition/m-p/531698#M16436</guid>
      <dc:creator>Xusheng</dc:creator>
      <dc:date>2019-01-31T16:27:12Z</dc:date>
    </item>
    <item>
      <title>Re: Remove observations by date with if condition</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Remove-observations-by-date-with-if-condition/m-p/531750#M16437</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/114336"&gt;@Xusheng&lt;/a&gt;&amp;nbsp; &amp;nbsp;The condition isn't clear.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;condition:&amp;nbsp;&amp;nbsp;&lt;SPAN&gt;, if 'OilCount &amp;gt;= 8'&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;Please take a look at the below that has the cumulative count on the right. do you mean this?&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Oil&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;Count date cumulative_count&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;0 03/09/2012 0&lt;BR /&gt;6 03/09/2012 6&lt;BR /&gt;0 04/09/2012 0&lt;BR /&gt;5 04/09/2012 5&lt;BR /&gt;1 04/09/2012 6&lt;BR /&gt;4 05/09/2012 4&lt;BR /&gt;&lt;STRONG&gt;5 06/09/2012 5&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;3 06/09/2012 8&amp;nbsp; &amp;nbsp; &amp;nbsp;Here, the cumulative oil count which is 5+3 satisfies &amp;gt;=8&amp;nbsp;&lt;/STRONG&gt;&lt;BR /&gt;6 06/09/2012 14&lt;BR /&gt;8 06/09/2012 22&lt;BR /&gt;&lt;STRONG&gt;11 07/09/2012 11&amp;nbsp; and likewise the same here 11&amp;gt;=8 satisfies the condition&lt;/STRONG&gt;&lt;BR /&gt;3 08/09/2012 3&lt;BR /&gt;2 08/09/2012 5&lt;BR /&gt;4 10/09/2012 4&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, what do you mean by-&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;rather than the observations that meet 'OilCount &amp;gt;= 24' within that date?&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 31 Jan 2019 18:44:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Remove-observations-by-date-with-if-condition/m-p/531750#M16437</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-01-31T18:44:20Z</dc:date>
    </item>
    <item>
      <title>Re: Remove observations by date with if condition</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Remove-observations-by-date-with-if-condition/m-p/532268#M16450</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/114336"&gt;@Xusheng&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Not sure why below row is not in your want table. Is this a typo?&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;&lt;SPAN class="token number"&gt;11&lt;/SPAN&gt;	&lt;SPAN class="token number"&gt;07&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;/&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;09&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;/&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;2012&lt;/SPAN&gt;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming your actual selection conditions could be of higher complexity than what you've posted:&lt;/P&gt;
&lt;P&gt;- You could first create an intermediary table with rows meeting your condition.&lt;/P&gt;
&lt;P&gt;- You then use this intermediary table as a lookup table to only select rows in your source data which don't have a match to the lookup table.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
 input OilCount date ddmmyy10.;
 format date date9.;
 datalines;
0 03/09/2012
6 03/09/2012
0 04/09/2012
5 04/09/2012
1 04/09/2012
4 05/09/2012
5 06/09/2012
3 06/09/2012
6 06/09/2012
8 06/09/2012
11 07/09/2012
3 08/09/2012
2 08/09/2012
4 10/09/2012
;
run;

data DelObs/view=DelObs;
  set have;
  keep date;
  if OilCount&amp;gt;=8;
run;

data want;
  if _n_=1 then
    do;
      dcl hash h1(dataset:'DelObs');
      h1.defineKey('date');
      h1.defineDone();  
    end;

  set have;
  if h1.check() ne 0 then output;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 02 Feb 2019 07:16:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Remove-observations-by-date-with-if-condition/m-p/532268#M16450</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-02-02T07:16:36Z</dc:date>
    </item>
  </channel>
</rss>

