<?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: Deleting records conditionally (before some value) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Deleting-records-conditionally-before-some-value/m-p/828669#M327349</link>
    <description>&lt;PRE&gt;data have;
input Sub  category  $ date   : $20.        value;
cards;
1          a            3/8/2022        0
1          a            3/9/2022        0
1          a            3/10/2022      1
1          c            3/11/2022      0
1          c            3/12/2022      1
2          a            3/8/2022        0
2          a           3/9/2022        1
2          a            3/10/2022      0
2          b            3/11/2022     1
2          b            3/12/2022      0
; 

data want;
 set have;
 by  Sub  category;
 retain found;
 if first.category then found=0;
 if value then found=1;
 if  found;
 drop found;
run;&lt;/PRE&gt;</description>
    <pubDate>Mon, 15 Aug 2022 12:45:10 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2022-08-15T12:45:10Z</dc:date>
    <item>
      <title>Deleting records conditionally (before some value)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Deleting-records-conditionally-before-some-value/m-p/828606#M327321</link>
      <description>&lt;P&gt;I have data like this and I want to flag or delete below highlighted records, i.e. value is&amp;nbsp; 0 before 1 per each category and each subject.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Sub&amp;nbsp; category&amp;nbsp; &amp;nbsp;date&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;value&lt;/STRONG&gt;&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; a&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3/8/2022&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;U&gt;&lt;EM&gt; &lt;STRONG&gt;0&lt;/STRONG&gt;&lt;/EM&gt;&lt;/U&gt;&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; a&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3/9/2022&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;U&gt;&lt;EM&gt;&lt;STRONG&gt;0&lt;/STRONG&gt;&lt;/EM&gt;&lt;/U&gt;&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; a&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3/10/2022&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; c&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3/11/2022&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;U&gt;&lt;EM&gt;&lt;STRONG&gt;0&lt;/STRONG&gt;&lt;/EM&gt;&lt;/U&gt;&lt;BR /&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; c&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3/12/2022&amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;BR /&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; a&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3/8/2022&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;U&gt;&lt;EM&gt;&lt;STRONG&gt;0&lt;/STRONG&gt;&lt;/EM&gt;&lt;/U&gt;&lt;BR /&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; a&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3/9/2022&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&lt;BR /&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; a&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3/10/2022&amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;BR /&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; b&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3/11/2022&amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;BR /&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; b&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3/12/2022&amp;nbsp; &amp;nbsp; &amp;nbsp; 0&lt;BR /&gt;;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 14 Aug 2022 11:22:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Deleting-records-conditionally-before-some-value/m-p/828606#M327321</guid>
      <dc:creator>KalaBhairava</dc:creator>
      <dc:date>2022-08-14T11:22:02Z</dc:date>
    </item>
    <item>
      <title>Re: Deleting records conditionally (before some value)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Deleting-records-conditionally-before-some-value/m-p/828607#M327322</link>
      <description>&lt;P&gt;I'm not understanding the condition to create a flag. It seems like you want to flag all zeros. When would you not flag a zero? Please explain further.&lt;/P&gt;</description>
      <pubDate>Sun, 14 Aug 2022 11:18:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Deleting-records-conditionally-before-some-value/m-p/828607#M327322</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-08-14T11:18:55Z</dc:date>
    </item>
    <item>
      <title>Re: Deleting records conditionally (before some value)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Deleting-records-conditionally-before-some-value/m-p/828608#M327323</link>
      <description>Can you check Now, updated my question.&lt;BR /&gt;</description>
      <pubDate>Sun, 14 Aug 2022 11:22:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Deleting-records-conditionally-before-some-value/m-p/828608#M327323</guid>
      <dc:creator>KalaBhairava</dc:creator>
      <dc:date>2022-08-14T11:22:40Z</dc:date>
    </item>
    <item>
      <title>Re: Deleting records conditionally (before some value)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Deleting-records-conditionally-before-some-value/m-p/828609#M327324</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data intermediate;
    set have;
    by sub category;
    if first.category then count=0;
    count+1;
    if value=1 then one_location=count;
run;
proc summary data=intermediate nway;
	class sub category;
	var one_location;
	output out=_max_ max=max_one_location;
run;
data want;
    merge intermediate _max_(drop=_:);
    by sub category;
    if value=0 and count&amp;lt;max_one_location then delete;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 14 Aug 2022 12:43:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Deleting-records-conditionally-before-some-value/m-p/828609#M327324</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-08-14T12:43:54Z</dc:date>
    </item>
    <item>
      <title>Re: Deleting records conditionally (before some value)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Deleting-records-conditionally-before-some-value/m-p/828610#M327325</link>
      <description>When you come across a 0, you obviously don't know whether a 1 will appear later.  So you have to go through the data twice.  Here is one approach.  The top loop goes through a sub/category combination and counts the 1's.  Then the bottom loop examines the same observations and chooses which observations to output.&lt;BR /&gt;&lt;BR /&gt;data want;&lt;BR /&gt;last1 = 0;&lt;BR /&gt;n_1 = 0;&lt;BR /&gt;Obsno = 0;&lt;BR /&gt;do until (last.category);&lt;BR /&gt;  set have;&lt;BR /&gt;  by sub category;&lt;BR /&gt;  if value = 1 then last1 + 1;&lt;BR /&gt;end;&lt;BR /&gt;do until  (last.category);&lt;BR /&gt;  set have;&lt;BR /&gt;  by sub category;&lt;BR /&gt;  if value = 1 then n_1 + 1;&lt;BR /&gt;  if value = 1 or n_1 &amp;lt; last_1 then output;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;Working from a cell phone screen so I will need to look this over after I post it.</description>
      <pubDate>Sun, 14 Aug 2022 13:34:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Deleting-records-conditionally-before-some-value/m-p/828610#M327325</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2022-08-14T13:34:02Z</dc:date>
    </item>
    <item>
      <title>Re: Deleting records conditionally (before some value)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Deleting-records-conditionally-before-some-value/m-p/828611#M327326</link>
      <description>&lt;P&gt;You apparently want to delete "leading zeroes" for each sub/category combination.&amp;nbsp; If so then:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  set have;
  by sub category;
  if first.category=1 then _n_of_ones=value;
  else _n_of_ones+value;
  if _n_of_ones&amp;gt;0;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This assumes that data are already sorted by sub/category (and presumably you require the data to be sorted by date within each sub/category).&amp;nbsp; So assume data sorted by sub/category/date.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What you haven't explained is what you want to do if a given sub/category has no observations with value^=0.&amp;nbsp; In such a case, the program above will drop all obs for that sub/category.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Additional comment:&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You haven't answer&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;'s question (or my comment above).&amp;nbsp; I.e. what do you want to do if a given sub/category has only zeroes?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to delete them all, then above program works.&amp;nbsp; If you want to keep them all, then the following program will do:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  set have (where=(value=1) in=firstpass) have (in=secondpass);
  by sub category;
  if first.category then _cutoff_date=date;
  retain _cutoff_date;
  if secondpass and date&amp;gt;=_cutoff_date;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If there are no 1's, then the _cutoff_date is the first value=0 date.&amp;nbsp; But if there are some 1's, then it is the first value=1 date.&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;P&gt;&amp;nbsp;&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;</description>
      <pubDate>Sun, 14 Aug 2022 20:28:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Deleting-records-conditionally-before-some-value/m-p/828611#M327326</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-08-14T20:28:25Z</dc:date>
    </item>
    <item>
      <title>Re: Deleting records conditionally (before some value)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Deleting-records-conditionally-before-some-value/m-p/828612#M327327</link>
      <description>&lt;P&gt;What if a sub has only zeroes?&lt;/P&gt;</description>
      <pubDate>Sun, 14 Aug 2022 14:42:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Deleting-records-conditionally-before-some-value/m-p/828612#M327327</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-08-14T14:42:18Z</dc:date>
    </item>
    <item>
      <title>Re: Deleting records conditionally (before some value)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Deleting-records-conditionally-before-some-value/m-p/828615#M327330</link>
      <description>&lt;P&gt;It should be there, I just want to delete 0 before 1 per each subject per category.&lt;BR /&gt;&lt;BR /&gt;i.e. if subject has 1 then only I want to delete 0 which are prior to 1, and 0 value at post 1 or only 0 shouldn't be affect.&lt;/P&gt;</description>
      <pubDate>Sun, 14 Aug 2022 15:57:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Deleting-records-conditionally-before-some-value/m-p/828615#M327330</guid>
      <dc:creator>KalaBhairava</dc:creator>
      <dc:date>2022-08-14T15:57:56Z</dc:date>
    </item>
    <item>
      <title>Re: Deleting records conditionally (before some value)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Deleting-records-conditionally-before-some-value/m-p/828617#M327331</link>
      <description>&lt;P&gt;You have not answered my question: what if a sub has only 0's and no 1 at all? Should all observations of such a group be deleted or kept?&lt;/P&gt;</description>
      <pubDate>Sun, 14 Aug 2022 16:39:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Deleting-records-conditionally-before-some-value/m-p/828617#M327331</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-08-14T16:39:12Z</dc:date>
    </item>
    <item>
      <title>Re: Deleting records conditionally (before some value)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Deleting-records-conditionally-before-some-value/m-p/828642#M327339</link>
      <description>It should be there</description>
      <pubDate>Mon, 15 Aug 2022 04:26:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Deleting-records-conditionally-before-some-value/m-p/828642#M327339</guid>
      <dc:creator>KalaBhairava</dc:creator>
      <dc:date>2022-08-15T04:26:49Z</dc:date>
    </item>
    <item>
      <title>Re: Deleting records conditionally (before some value)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Deleting-records-conditionally-before-some-value/m-p/828645#M327340</link>
      <description>&lt;P&gt;So we need an additional check for the "non-1" cases.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by sub;
if _n_ = 1
then do;
  declare hash w1 (dataset:"have (where=(value = 1))");
  w1.definekey("sub");
  w1.definedone(),
end;
retain keep_flag;
if first.sub
then if w1.check() ne 0
  then keep_flag = 1;
  else keep_flag = 0;
if value = 1 then keep_flag = 1;
if keep_flag;
drop keep_flag;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 Aug 2022 04:57:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Deleting-records-conditionally-before-some-value/m-p/828645#M327340</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-08-15T04:57:10Z</dc:date>
    </item>
    <item>
      <title>Re: Deleting records conditionally (before some value)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Deleting-records-conditionally-before-some-value/m-p/828669#M327349</link>
      <description>&lt;PRE&gt;data have;
input Sub  category  $ date   : $20.        value;
cards;
1          a            3/8/2022        0
1          a            3/9/2022        0
1          a            3/10/2022      1
1          c            3/11/2022      0
1          c            3/12/2022      1
2          a            3/8/2022        0
2          a           3/9/2022        1
2          a            3/10/2022      0
2          b            3/11/2022     1
2          b            3/12/2022      0
; 

data want;
 set have;
 by  Sub  category;
 retain found;
 if first.category then found=0;
 if value then found=1;
 if  found;
 drop found;
run;&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 Aug 2022 12:45:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Deleting-records-conditionally-before-some-value/m-p/828669#M327349</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-08-15T12:45:10Z</dc:date>
    </item>
  </channel>
</rss>

