<?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 dynamically remove records until 2 distinct values of a field are within the top 4 records in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-dynamically-remove-records-until-2-distinct-values-of-a/m-p/443507#M110959</link>
    <description>&lt;P&gt;I have a data set layout that looks something like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Category&amp;nbsp; &amp;nbsp; &amp;nbsp; Score&lt;/P&gt;&lt;P&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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;100&lt;/P&gt;&lt;P&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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;95&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;80&lt;/P&gt;&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;75&lt;/P&gt;&lt;P&gt;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; C&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;70&lt;/P&gt;&lt;P&gt;6&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;65&lt;/P&gt;&lt;P&gt;7&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;60&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The data set needs to be sorted by descending score. However, I have a requirement where after the fact the top 4 need to contain at least one "A" and at least one "C" category, so the resulting would end up like so:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Category&amp;nbsp; &amp;nbsp; &amp;nbsp; Score&lt;/P&gt;&lt;P&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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;100&lt;/P&gt;&lt;P&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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;95&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;80&lt;/P&gt;&lt;P&gt;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; C&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;70&lt;/P&gt;&lt;P&gt;6&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;65&lt;/P&gt;&lt;P&gt;7&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;60&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does anyone have advice on how to tackle this? I'm thinking potentially someething with some counters and a retain statement but I'm not sure how to keep it going until the above stated criteria.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 07 Mar 2018 20:59:22 GMT</pubDate>
    <dc:creator>jhahn3814</dc:creator>
    <dc:date>2018-03-07T20:59:22Z</dc:date>
    <item>
      <title>how to dynamically remove records until 2 distinct values of a field are within the top 4 records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-dynamically-remove-records-until-2-distinct-values-of-a/m-p/443507#M110959</link>
      <description>&lt;P&gt;I have a data set layout that looks something like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Category&amp;nbsp; &amp;nbsp; &amp;nbsp; Score&lt;/P&gt;&lt;P&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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;100&lt;/P&gt;&lt;P&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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;95&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;80&lt;/P&gt;&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;75&lt;/P&gt;&lt;P&gt;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; C&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;70&lt;/P&gt;&lt;P&gt;6&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;65&lt;/P&gt;&lt;P&gt;7&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;60&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The data set needs to be sorted by descending score. However, I have a requirement where after the fact the top 4 need to contain at least one "A" and at least one "C" category, so the resulting would end up like so:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Category&amp;nbsp; &amp;nbsp; &amp;nbsp; Score&lt;/P&gt;&lt;P&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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;100&lt;/P&gt;&lt;P&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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;95&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;80&lt;/P&gt;&lt;P&gt;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; C&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;70&lt;/P&gt;&lt;P&gt;6&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; B&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;65&lt;/P&gt;&lt;P&gt;7&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; A&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;60&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does anyone have advice on how to tackle this? I'm thinking potentially someething with some counters and a retain statement but I'm not sure how to keep it going until the above stated criteria.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Mar 2018 20:59:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-dynamically-remove-records-until-2-distinct-values-of-a/m-p/443507#M110959</guid>
      <dc:creator>jhahn3814</dc:creator>
      <dc:date>2018-03-07T20:59:22Z</dc:date>
    </item>
    <item>
      <title>Re: how to dynamically remove records until 2 distinct values of a field are within the top 4 record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-dynamically-remove-records-until-2-distinct-values-of-a/m-p/443512#M110963</link>
      <description>&lt;P&gt;What will you do if the actual data has no A category at all? or no C? or neither A or C?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have to assume that by "top" you mean largest values of score (not explicitly stated and large is not always "top").&lt;/P&gt;</description>
      <pubDate>Wed, 07 Mar 2018 21:06:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-dynamically-remove-records-until-2-distinct-values-of-a/m-p/443512#M110963</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-03-07T21:06:28Z</dc:date>
    </item>
    <item>
      <title>Re: how to dynamically remove records until 2 distinct values of a field are within the top 4 record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-dynamically-remove-records-until-2-distinct-values-of-a/m-p/443514#M110964</link>
      <description>&lt;P&gt;There must be more rules than that.&amp;nbsp; Why remove ID 4, rather than ID 2 or ID 3?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What if ID 4 was in Category B instead of A?&amp;nbsp; How would you know whether to remove ID 2 or ID 4?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What if ID 4 had received a score of 90 instead of a score of 75?&amp;nbsp; Could the only Category B observation (ID 3) be removed?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Mar 2018 21:12:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-dynamically-remove-records-until-2-distinct-values-of-a/m-p/443514#M110964</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-03-07T21:12:52Z</dc:date>
    </item>
    <item>
      <title>Re: how to dynamically remove records until 2 distinct values of a field are within the top 4 record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-dynamically-remove-records-until-2-distinct-values-of-a/m-p/443516#M110966</link>
      <description>Assume they’re already sorted by highest score and I want the highest scores possible while still having one A and one C</description>
      <pubDate>Wed, 07 Mar 2018 21:19:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-dynamically-remove-records-until-2-distinct-values-of-a/m-p/443516#M110966</guid>
      <dc:creator>jhahn3814</dc:creator>
      <dc:date>2018-03-07T21:19:50Z</dc:date>
    </item>
    <item>
      <title>Re: how to dynamically remove records until 2 distinct values of a field are within the top 4 record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-dynamically-remove-records-until-2-distinct-values-of-a/m-p/443518#M110967</link>
      <description>If the criteria is never met then nothing will be output</description>
      <pubDate>Wed, 07 Mar 2018 21:20:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-dynamically-remove-records-until-2-distinct-values-of-a/m-p/443518#M110967</guid>
      <dc:creator>jhahn3814</dc:creator>
      <dc:date>2018-03-07T21:20:51Z</dc:date>
    </item>
    <item>
      <title>Re: how to dynamically remove records until 2 distinct values of a field are within the top 4 record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-dynamically-remove-records-until-2-distinct-values-of-a/m-p/443525#M110969</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID       Category   $   Score;
datalines;
1          A                   100
2          A                   95
3          B                   80
4          A                   75
5          C                   70
6          B                   65
7          A                   60
;


proc sort data=have  out=__have nodupkey;
by Category ;
where Category in ('A','C');
run;

proc sql;
   create table want as
   (select * from __have 
         intersect
         select * from have )
   outer union corr
   (select * from have 
         except
         select * from __have);
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 07 Mar 2018 21:35:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-dynamically-remove-records-until-2-distinct-values-of-a/m-p/443525#M110969</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-03-07T21:35:37Z</dc:date>
    </item>
    <item>
      <title>Re: how to dynamically remove records until 2 distinct values of a field are within the top 4 record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-dynamically-remove-records-until-2-distinct-values-of-a/m-p/443544#M110975</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID       Category   $   Score;
datalines;
1          A                   100
2          A                   95
3          B                   80
4          A                   75
5          C                   70
6          B                   65
7          A                   60
;
data want(rename=(_id=id _score=score));
if _n_=1 then do;
if 0 then set have;
dcl hash h(dataset: 'have(where=(category in ("A","C"))',ordered:'d');
    h.definekey('Category');
   h.definedata('ID','Category', 'score');
   h.definedone();
    declare hiter iter('h');
	rc = iter.first();
do while (rc = 0);
_id=id;
_score=score;
      output;
   rc = iter.next();
end;
   end; 
 set have(rename=(id=_id score=_score)) end=last;
 rc=h.find();
 if rc ne 0 then output;
 else if rc=0 and id ne _id and score ne _score then output;
drop id score rc;
 run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 07 Mar 2018 22:58:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-dynamically-remove-records-until-2-distinct-values-of-a/m-p/443544#M110975</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-03-07T22:58:39Z</dc:date>
    </item>
    <item>
      <title>Re: how to dynamically remove records until 2 distinct values of a field are within the top 4 record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-dynamically-remove-records-until-2-distinct-values-of-a/m-p/443620#M111000</link>
      <description>&lt;P&gt;It could be done this way:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
array _c(2) $1 ("A", "C");
retain w 4 found 0;
set have end=done;
if found &amp;gt;= dim(_c) then output;
else do;
    if category in _c then do;
        output;
        found = found + 1;
        _c{whichc(category, of _c{*})} = "-";
        w = w - 1;
        end;
    else if w &amp;gt; dim(_c) - found then do;
        output;
        w = w - 1;
        end;
    end;
if done then 
    if w &amp;gt; 0 then abort;
drop w found _c:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 08 Mar 2018 05:01:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-dynamically-remove-records-until-2-distinct-values-of-a/m-p/443620#M111000</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-03-08T05:01:43Z</dc:date>
    </item>
    <item>
      <title>Re: how to dynamically remove records until 2 distinct values of a field are within the top 4 record</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-dynamically-remove-records-until-2-distinct-values-of-a/m-p/443770#M111044</link>
      <description>&lt;P&gt;Very interesting Question.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID       Category   $   Score;
datalines;
1          A                   100
2          A                   95
3          B                   80
4          A                   75
5          C                   70
6          B                   65
7          A                   60
;
data temp;
 set have;
 retain A C;
 if Category='A' then A=1;
  else if Category='C' then C=1;
 output;
 if A and C then do;call symputx('n',_n_);stop;end;
run;

%macro xx;
%if &amp;amp;n &amp;lt;= 4 %then %do;
  data want;
   set have;
  run;
%end;
%else %do;
  data want;
   set have(obs=3) have(firstobs=&amp;amp;n);
  run;
%end;
%mend;

%xx&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 08 Mar 2018 14:34:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-dynamically-remove-records-until-2-distinct-values-of-a/m-p/443770#M111044</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-03-08T14:34:35Z</dc:date>
    </item>
  </channel>
</rss>

