<?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 a group based on conditions in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Remove-a-group-based-on-conditions/m-p/467840#M119440</link>
    <description>&lt;P&gt;is this&lt;/P&gt;&lt;P&gt;2 2000 12 1&amp;nbsp;25&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;2 2000 12 2 310&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;correct in your expected output&lt;/P&gt;&lt;P&gt;or should it be&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;B 2000 1 2 280 ?&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;

input id $ year period span;

datalines;
A 2000 1 40
A 2000 2 375
A 2000 1 10
A 2000 2 355 
A 2001 1 -15
A 2001 2 290
A 2001 1 39
A 2001 2 320
B 2000 1 43
B 2000 2 350
B 2000 1 25
B 2000 2 310
B 2000 1 -40
B 2000 2 280
;

run;

proc sql;
create table want as
select *
from test
group by id,year,period
having min(case  when span&amp;lt;0 then . else span    end)=span
order by id, year, period;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 05 Jun 2018 20:24:54 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2018-06-05T20:24:54Z</dc:date>
    <item>
      <title>Remove a group based on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-a-group-based-on-conditions/m-p/467830#M119438</link>
      <description>&lt;P&gt;Hoping to receive help with the following problem; data set immediately below followed by problem.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The actual data set is much more complex, but here's simplified data:&lt;/P&gt;
&lt;P&gt;data test;&lt;/P&gt;
&lt;P&gt;input id year date period span;&lt;/P&gt;
&lt;P&gt;datalines;&lt;/P&gt;
&lt;P&gt;1 2000 9 1 40&lt;/P&gt;
&lt;P&gt;1 2000 9 2 375&lt;/P&gt;
&lt;P&gt;1 2000 10 1 10&lt;/P&gt;
&lt;P&gt;1 2000 10 2 355&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1 2001 1 1 -15&lt;/P&gt;
&lt;P&gt;1 2001 1 2 290&lt;/P&gt;
&lt;P&gt;1 2001 9 1&amp;nbsp;39&lt;/P&gt;
&lt;P&gt;1 2001 9 2 320&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2 2000 11 1&amp;nbsp;43&lt;/P&gt;
&lt;P&gt;2 2000 11 2 350&lt;/P&gt;
&lt;P&gt;2 2000 12 1&amp;nbsp;25&lt;/P&gt;
&lt;P&gt;2 2000 12 2 310&lt;/P&gt;
&lt;P&gt;2 2000 1 1 -40&lt;/P&gt;
&lt;P&gt;2 2000 1 2 280&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For each id-year group (the above data set is spaced based upon the id-year groupings), I need to keep a single id-year-date group (i.e., a set of period 1-2 pairs) where&amp;nbsp;the observation from period 1 (of the period 1-2 pair) has a value of span which meets two criterion: 1) span is greater than 0, and 2) the value of span is closest to 0 of all other period 1 observations for the id-year group.&amp;nbsp; Note, I do not want the period 2 observation that meets those 2 criterion, rather I want to keep period 2 based upon its period 1 counterpart that best meets those criterion.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Therefore, the final data set should look like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRIKE&gt;1 2000 9 1 40&lt;/STRIKE&gt; (eliminated because 40 is farther away from 0 than 10)&lt;/P&gt;
&lt;P&gt;&lt;STRIKE&gt;1 2000 9 2 375&lt;/STRIKE&gt; (eliminated because its period 1 pair was eliminated due to its paired period 1's span being at a greater distance)&lt;/P&gt;
&lt;P&gt;1 2000 10 1 10&lt;/P&gt;
&lt;P&gt;1 2000 10 2 355&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRIKE&gt;1 2001 1 1 -15&lt;/STRIKE&gt; (eliminated because the value of span is negative: -15)&lt;/P&gt;
&lt;P&gt;&lt;STRIKE&gt;1 2001 1 2 290&lt;/STRIKE&gt;&amp;nbsp;&lt;SPAN&gt;(eliminated because its period 1 &lt;/SPAN&gt;&lt;SPAN&gt;pair was eliminated due to a negative span)&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;1 2001 9 1&amp;nbsp;39&lt;/P&gt;
&lt;P&gt;1 2001 9 2 320&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRIKE&gt;2 2000 11 1&amp;nbsp;43&lt;/STRIKE&gt;&amp;nbsp;&lt;SPAN&gt;(eliminated because 43 is farther away from 0 than 25)&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRIKE&gt;2 2000 11 2 350&lt;/STRIKE&gt; (&lt;SPAN&gt;eliminated because its period 1 pair &lt;/SPAN&gt;&lt;SPAN&gt;was eliminated due to&amp;nbsp;its paired period 1's span being at a greater distance)&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;2 2000 12 1&amp;nbsp;25&lt;/P&gt;
&lt;P&gt;2 2000 12 2 310&lt;/P&gt;
&lt;P&gt;&lt;STRIKE&gt;2 2000 1 1 -40&lt;/STRIKE&gt; (eliminated because the value of span was negative: -40)&lt;/P&gt;
&lt;P&gt;&lt;STRIKE&gt;2 2000 1 2 280&lt;/STRIKE&gt;&amp;nbsp;&lt;SPAN&gt;(eliminated because its period 1 pair was eliminated due to a negative span&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The actual dataset has more than just periods 1 and 2 and can contain any number of period groupings per id-year.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;An acceptable solution could possibly copy the value of span from period 1 of each id-year-date group to the other periods within the group and then do a two-step process of first, eliminating all observations with span less than zero, and then second, sorting on id-year-period-span in descending order and then retain only the first observation of each id-year-period group...?&lt;/P&gt;</description>
      <pubDate>Tue, 05 Jun 2018 20:54:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-a-group-based-on-conditions/m-p/467830#M119438</guid>
      <dc:creator>yeaforme</dc:creator>
      <dc:date>2018-06-05T20:54:32Z</dc:date>
    </item>
    <item>
      <title>Re: Remove a group based on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-a-group-based-on-conditions/m-p/467840#M119440</link>
      <description>&lt;P&gt;is this&lt;/P&gt;&lt;P&gt;2 2000 12 1&amp;nbsp;25&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;2 2000 12 2 310&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;correct in your expected output&lt;/P&gt;&lt;P&gt;or should it be&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;B 2000 1 2 280 ?&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;

input id $ year period span;

datalines;
A 2000 1 40
A 2000 2 375
A 2000 1 10
A 2000 2 355 
A 2001 1 -15
A 2001 2 290
A 2001 1 39
A 2001 2 320
B 2000 1 43
B 2000 2 350
B 2000 1 25
B 2000 2 310
B 2000 1 -40
B 2000 2 280
;

run;

proc sql;
create table want as
select *
from test
group by id,year,period
having min(case  when span&amp;lt;0 then . else span    end)=span
order by id, year, period;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 05 Jun 2018 20:24:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-a-group-based-on-conditions/m-p/467840#M119440</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-06-05T20:24:54Z</dc:date>
    </item>
    <item>
      <title>Re: Remove a group based on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-a-group-based-on-conditions/m-p/467841#M119441</link>
      <description>The 2 in ID should be B, but the correct grouping for ID B should be the 1-2 pair with values 25-310.  I added an additional date variable to the original data to hopefully make the span sets more obvious.</description>
      <pubDate>Tue, 05 Jun 2018 20:20:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-a-group-based-on-conditions/m-p/467841#M119441</guid>
      <dc:creator>yeaforme</dc:creator>
      <dc:date>2018-06-05T20:20:18Z</dc:date>
    </item>
    <item>
      <title>Re: Remove a group based on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-a-group-based-on-conditions/m-p/467842#M119442</link>
      <description>&lt;P&gt;Please test your example data datastep before posting it. ID is missing in your example&amp;nbsp;as you do not use: input id $ so id is treated as numeric;&lt;/P&gt;
&lt;P&gt;If ID or date&amp;nbsp;has any role in the result, which I think it does, then you should show it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think your "want" example is incorrect: You show&lt;/P&gt;
&lt;P&gt;1 2001 9 2 320&amp;nbsp;&lt;/P&gt;
&lt;P&gt;but there is a row of data&lt;/P&gt;
&lt;P&gt;A 2001 1 2 290&lt;/P&gt;
&lt;P&gt;which is the same year and period but 290 is closest to 0.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sort data=test;
   by id year period  span;
run;
data want;
   set test;
   where span&amp;gt;0;
   by id year period ;
   if first.period;
run;&lt;/PRE&gt;</description>
      <pubDate>Tue, 05 Jun 2018 20:21:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-a-group-based-on-conditions/m-p/467842#M119442</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-06-05T20:21:31Z</dc:date>
    </item>
    <item>
      <title>Re: Remove a group based on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-a-group-based-on-conditions/m-p/467845#M119443</link>
      <description>&lt;P&gt;I apologize for my test data, I posted it and then continually made revisions trying to make it more clear, but just caused everyone more problems.&lt;BR /&gt;&lt;BR /&gt;That being said, my want data is not incorrect. Each period 1-2 pair comes as a pair based on the date value. It is imperative that the period 1 value in span of each 1-2 pair not be negative, therefore, A 2001 1 2 290 is disqualified because its period 1 pair based on date is A 2001 1 1 -15.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Jun 2018 20:28:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-a-group-based-on-conditions/m-p/467845#M119443</guid>
      <dc:creator>yeaforme</dc:creator>
      <dc:date>2018-06-05T20:28:04Z</dc:date>
    </item>
    <item>
      <title>Re: Remove a group based on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-a-group-based-on-conditions/m-p/467858#M119448</link>
      <description>Please see updated inquiry.</description>
      <pubDate>Tue, 05 Jun 2018 21:04:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-a-group-based-on-conditions/m-p/467858#M119448</guid>
      <dc:creator>yeaforme</dc:creator>
      <dc:date>2018-06-05T21:04:01Z</dc:date>
    </item>
    <item>
      <title>Re: Remove a group based on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-a-group-based-on-conditions/m-p/467864#M119450</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;

input id $ year period span;

datalines;
A 2000 1 40
A 2000 2 375
A 2000 1 10
A 2000 2 355 
A 2001 1 -15
A 2001 2 290
A 2001 1 39
A 2001 2 320
B 2000 1 43
B 2000 2 350
B 2000 1 25
B 2000 2 310
B 2000 1 -40
B 2000 2 280
;

run;
data grp;
set test;
by id  year period;
if period =1 then 
grp+1;
run;
data temp;
f=1;
do until(last.grp);
set grp;
by  grp notsorted;
if period =1 and span&amp;lt;0 then f=.;
if not missing(f) then output;
end;
drop grp;
run;

proc sql;
create table want as
select *
from temp
group by id, year,period
having min(span)=span
order by id, year, period;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 05 Jun 2018 21:47:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-a-group-based-on-conditions/m-p/467864#M119450</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-06-05T21:47:02Z</dc:date>
    </item>
    <item>
      <title>Re: Remove a group based on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-a-group-based-on-conditions/m-p/467871#M119455</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10975"&gt;@yeaforme&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I apologize for my test data, I posted it and then continually made revisions trying to make it more clear, but just caused everyone more problems.&lt;BR /&gt;&lt;BR /&gt;That being said, my want data is not incorrect. Each period 1-2 pair comes as a pair based on the date value. It is imperative that the period 1 value in span of each 1-2 pair not be negative, therefore, A 2001 1 2 290 is disqualified because its period 1 pair based on date is A 2001 1 1 -15.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Since you have changed the data at least twice and the requirements as well since I started to answer this request I have no further response.&lt;/P&gt;</description>
      <pubDate>Tue, 05 Jun 2018 22:12:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-a-group-based-on-conditions/m-p/467871#M119455</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-06-05T22:12:39Z</dc:date>
    </item>
    <item>
      <title>Re: Remove a group based on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-a-group-based-on-conditions/m-p/467872#M119456</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;Oh my gosh, i didn't notice that&lt;/P&gt;</description>
      <pubDate>Tue, 05 Jun 2018 22:04:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-a-group-based-on-conditions/m-p/467872#M119456</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-06-05T22:04:13Z</dc:date>
    </item>
    <item>
      <title>Re: Remove a group based on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-a-group-based-on-conditions/m-p/467873#M119457</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;

input id year date period span;

datalines;
1 2000 9 1 40
1 2000 9 2 375
1 2000 10 1 10
1 2000 10 2 355 
1 2001 1 1 -15
1 2001 1 2 290
1 2001 9 1 39
1 2001 9 2 320
2 2000 11 1 43
2 2000 11 2 350
2 2000 12 1 25
2 2000 12 2 310
2 2000 1 1 -40
2 2000 1 2 280
;

run;

data grp;
set test;
by id  year period notsorted;
if period =1 then 
grp+1;
run;
data temp;
f=1;
do until(last.grp);
set grp;
by  grp notsorted;
if period =1 and span&amp;lt;0 then f=.;
if not missing(f) then output;
end;
drop grp f;
run;

proc sql;
create table want as
select *
from temp
group by id, year,period
having min(span)=span
order by id, year, period;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 05 Jun 2018 22:08:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-a-group-based-on-conditions/m-p/467873#M119457</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-06-05T22:08:19Z</dc:date>
    </item>
    <item>
      <title>Re: Remove a group based on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-a-group-based-on-conditions/m-p/467879#M119461</link>
      <description>&lt;P&gt;The requirements have not changed, though how detailed your understanding of it may have.&lt;/P&gt;</description>
      <pubDate>Tue, 05 Jun 2018 22:40:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-a-group-based-on-conditions/m-p/467879#M119461</guid>
      <dc:creator>yeaforme</dc:creator>
      <dc:date>2018-06-05T22:40:19Z</dc:date>
    </item>
    <item>
      <title>Re: Remove a group based on conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Remove-a-group-based-on-conditions/m-p/467882#M119464</link>
      <description>&lt;P&gt;I've figured out a solution.&amp;nbsp; It involves: 1) using the retain statement to first spread the period 1's span value to the rest of group, 2) sort based on the retained span value, 3) eliminate group with negative retained span, and then 4) retain only the first group based on retained span.&amp;nbsp; It may not be the most elegant, but fits the bill.&amp;nbsp; Thanks to all for trying.&lt;/P&gt;</description>
      <pubDate>Tue, 05 Jun 2018 22:47:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Remove-a-group-based-on-conditions/m-p/467882#M119464</guid>
      <dc:creator>yeaforme</dc:creator>
      <dc:date>2018-06-05T22:47:07Z</dc:date>
    </item>
  </channel>
</rss>

