<?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: How to select the distinct rows with when variables are not one-to-one with group by variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-distinct-rows-when-variables-are-not-one-to/m-p/512344#M137974</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input id code diff_range weight_evidence;
cards;
1 1 2 10 
1 2 2 8 
1 3 4 0
1 3 4 0
1 5 7 0
2 1 1 10
2 2 3 8
2 2 3 8
2 4 5 0
2 5 6 0
3 3 1 0
3 4 2 0
3 5 3 0
4 8 8 0
4 9 9 0
5 6 10 6
5 1 10 10 
5 10 12 0
6 1 2 10 
6 2 2 8
6 1 9 10
6 6 9 6 
;
proc sql;
create table want_min1 as
select *
from 
(select distinct * , min(diff_range)as min_diff from have
where id in 
(select id from have where code in (1,2,6))
group by id)
group by id,min_diff
having weight_evidence=max(weight_evidence);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;PS I will simplify the above if i can&lt;/P&gt;</description>
    <pubDate>Mon, 12 Nov 2018 21:02:10 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2018-11-12T21:02:10Z</dc:date>
    <item>
      <title>How to select the distinct rows when variables are not one-to-one with 'group by' variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-distinct-rows-when-variables-are-not-one-to/m-p/512257#M137931</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'd like to:&lt;/P&gt;
&lt;P&gt;-&amp;nbsp;Select the row associated with the smallest diff_range&lt;/P&gt;
&lt;P&gt;- Among the patients who took diagnosis codes 1 or 2 or 6&lt;/P&gt;
&lt;P&gt;- Select the row associated with the distinct smallest diff_range&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The want is below and for example why ID=1 was selected was that:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ID=1 took target diagnosis codes 1 and 2. But 1, 1, 2, row is selected because of minimum value for a diff_range.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My current codes produce&amp;nbsp;&lt;SPAN&gt;more observations than I need because code and diff_range variables are not one-to-one with the ID and min_diff.&amp;nbsp; But I really don't want min_diff repeated in the final want data and also not willing to deduplicate data using extra step afterwards.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Any solutions? please. I'll greatly&amp;nbsp;appreciate your help.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data p.have;&lt;BR /&gt;input id code diff_range;&lt;BR /&gt;cards;&lt;BR /&gt;1 1 2&lt;BR /&gt;1 2 3&lt;BR /&gt;1 3 4&lt;BR /&gt;1 3 4&lt;BR /&gt;1 5 7&lt;BR /&gt;2 1 1&lt;BR /&gt;2 2 3&lt;BR /&gt;2 2 3&lt;BR /&gt;2 4 5&lt;BR /&gt;2 5 6&lt;BR /&gt;3 3 1&lt;BR /&gt;3 4 2&lt;BR /&gt;3 5 3&lt;BR /&gt;4 8 8&lt;BR /&gt;4 9 9&lt;BR /&gt;5 6 10&lt;BR /&gt;5 10 12&lt;BR /&gt;6 2 2 &lt;BR /&gt;6 2 1&lt;BR /&gt;6 6 9&lt;BR /&gt;6 6 4&lt;BR /&gt;;&lt;/P&gt;
&lt;P&gt;data want; &lt;BR /&gt;input id code diff_range;&lt;BR /&gt;cards; &lt;BR /&gt;1 1 2&lt;BR /&gt;2 1 1&lt;BR /&gt;5 6 10&lt;BR /&gt;6 2 1&lt;BR /&gt;;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;create table p.want_min1 as&lt;BR /&gt;select distinct * , min(diff_range) as min_diff from p.have&lt;BR /&gt;where id in &lt;BR /&gt; (select id from p.have where code in (1,2,6))&lt;BR /&gt;group by id;&lt;BR /&gt;quit;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Nov 2018 22:18:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-distinct-rows-when-variables-are-not-one-to/m-p/512257#M137931</guid>
      <dc:creator>Cruise</dc:creator>
      <dc:date>2018-11-12T22:18:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to select the distinct rows with when variables are not one-to-one with group by variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-distinct-rows-when-variables-are-not-one-to/m-p/512266#M137936</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id code diff_range;
cards;
1 1 2
1 2 3
1 3 4
1 3 4
1 5 7
2 1 1
2 2 3
2 2 3
2 4 5
2 5 6
3 3 1
3 4 2
3 5 3
4 8 8
4 9 9
5 6 10
5 10 12
6 2 2 
6 2 1
6 6 9
6 6 4
;
run;

proc sort data=have(where=(code in (1,2,6)));
	by id diff_range;
run;

data want;
	set have;
	by id diff_range;

	if first.id then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 12 Nov 2018 17:04:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-distinct-rows-when-variables-are-not-one-to/m-p/512266#M137936</guid>
      <dc:creator>r_behata</dc:creator>
      <dc:date>2018-11-12T17:04:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to select the distinct rows with when variables are not one-to-one with group by variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-distinct-rows-when-variables-are-not-one-to/m-p/512269#M137939</link>
      <description>&lt;P&gt;That's happening because of remerging behaviour of proc sql&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;try this change &lt;STRONG&gt;highlighted&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;proc sql;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;create table p.want_min1 as&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;select distinct * , min(diff_range) as min_diff from p.have&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;where id in&amp;nbsp;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;(select id from p.have where code in (1,2,6))&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;group by id&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;having&amp;nbsp;diff_range=min_diff;&lt;/STRONG&gt;&lt;BR /&gt;&lt;SPAN&gt;quit;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Nov 2018 17:06:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-distinct-rows-when-variables-are-not-one-to/m-p/512269#M137939</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-11-12T17:06:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to select the distinct rows with when variables are not one-to-one with group by variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-distinct-rows-when-variables-are-not-one-to/m-p/512270#M137940</link>
      <description>&lt;P&gt;One question you would have to let know is what if there is a&amp;nbsp; tie in min_diff?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And if you do not want min_diff in your output, the code can be as simple as&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id code diff_range;
cards;
1 1 2
1 2 3
1 3 4
1 3 4
1 5 7
2 1 1
2 2 3
2 2 3
2 4 5
2 5 6
3 3 1
3 4 2
3 5 3
4 8 8
4 9 9
5 6 10
5 10 12
6 2 2 
6 2 1
6 6 9
6 6 4
;

proc sql;
create table want_min2 as
select  *  from have
where id in 
(select id from have where code in (1,2,6))
group by id

having diff_range= min(diff_range);
quit;

proc print noobs;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;P&gt;SAS Output&lt;/P&gt;
&lt;DIV class="branch"&gt;
&lt;TABLE class="systitleandfootercontainer" border="0" summary="Page Layout" width="100%" frame="void" rules="none" cellspacing="1" cellpadding="1"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="c systemtitle"&gt;The SAS System&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;BR /&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;
&lt;TABLE class="table" summary="Procedure Print: Data Set WORK.WANT_MIN2" frame="box" rules="all" cellspacing="0" cellpadding="5"&gt;&lt;COLGROUP&gt; &lt;COL /&gt; &lt;COL /&gt; &lt;COL /&gt;&lt;/COLGROUP&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="r header" scope="col"&gt;id&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;code&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;diff_range&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;5&lt;/TD&gt;
&lt;TD class="r data"&gt;6&lt;/TD&gt;
&lt;TD class="r data"&gt;10&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;6&lt;/TD&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Nov 2018 17:34:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-distinct-rows-when-variables-are-not-one-to/m-p/512270#M137940</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-11-12T17:34:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to select the distinct rows with when variables are not one-to-one with group by variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-distinct-rows-when-variables-are-not-one-to/m-p/512297#M137943</link>
      <description>&lt;P&gt;Sort and use FIRST.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Proc sort data= have;
By Id diff_range code;
Run;

Data want;
Set have; 
By id diff_range;
Where code in (1, 2, 6);

If first.id;

Run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This will not keep duplicates. You could also sort by code to ensure you always get the smallest or largest. Though this may not be necessary, it’s easier to have a consistent logic so that you can replicate this if ever needed.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Nov 2018 18:57:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-distinct-rows-when-variables-are-not-one-to/m-p/512297#M137943</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-11-12T18:57:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to select the distinct rows with when variables are not one-to-one with group by variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-distinct-rows-when-variables-are-not-one-to/m-p/512333#M137965</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;so true, it doesn't work when min_diff ties up, just like below. When min_diff are the same across the different diagnosis codes that single patient took, I have to assign the weight to each diagnosis codes in terms of the strength of the evidence to prove of the&amp;nbsp;disease. And the row associated wit the higher score for diagnosis code would be selected among the ties by min_diff.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data p.have;&lt;BR /&gt;input id code diff_range;&lt;BR /&gt;cards;&lt;BR /&gt;1 1 2&lt;BR /&gt;1 2 3&lt;BR /&gt;1 3 4&lt;BR /&gt;1 3 4&lt;BR /&gt;1 5 7&lt;BR /&gt;2 1 1&lt;BR /&gt;2 2 3&lt;BR /&gt;2 2 3&lt;BR /&gt;2 4 5&lt;BR /&gt;2 5 6&lt;BR /&gt;3 3 1&lt;BR /&gt;3 4 2&lt;BR /&gt;3 5 3&lt;BR /&gt;4 8 8&lt;BR /&gt;4 9 9&lt;BR /&gt;5 6 10&lt;BR /&gt;5 10 12&lt;BR /&gt;6 1 2 &lt;BR /&gt;6 2 2&lt;BR /&gt;6 1 9&lt;BR /&gt;6 6 9&lt;BR /&gt;;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;create table p.want_min1 as&lt;BR /&gt;select distinct * , min(diff_range) as min_diff from p.have&lt;BR /&gt;where id in &lt;BR /&gt;(select id from p.have where code in (1,2,6))&lt;BR /&gt;group by id&lt;BR /&gt;having diff_range=min_diff;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Nov 2018 20:25:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-distinct-rows-when-variables-are-not-one-to/m-p/512333#M137965</guid>
      <dc:creator>Cruise</dc:creator>
      <dc:date>2018-11-12T20:25:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to select the distinct rows with when variables are not one-to-one with group by variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-distinct-rows-when-variables-are-not-one-to/m-p/512336#M137968</link>
      <description>&lt;P&gt;Ok,so do you have weighting variable in place?&lt;/P&gt;</description>
      <pubDate>Mon, 12 Nov 2018 20:34:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-distinct-rows-when-variables-are-not-one-to/m-p/512336#M137968</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-11-12T20:34:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to select the distinct rows with when variables are not one-to-one with group by variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-distinct-rows-when-variables-are-not-one-to/m-p/512340#M137971</link>
      <description>&lt;P&gt;Then make a minor modification to the proc sort solution.&amp;nbsp; Assuming your weight variable is WGT:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data= have (where=(code in (1,2,6))) out=need;
  by Id diff_range descending wgt;
run;

data want;
  set need; 
  by id diff_range;
  if first.id;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;which will work perfectly unless you have&amp;nbsp;an ID&amp;nbsp;containing multiple records at the lowest diff_range&amp;nbsp;having tied values&amp;nbsp;at the highest&amp;nbsp;wgt.&lt;/P&gt;</description>
      <pubDate>Mon, 12 Nov 2018 20:43:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-distinct-rows-when-variables-are-not-one-to/m-p/512340#M137971</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-11-12T20:43:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to select the distinct rows with when variables are not one-to-one with group by variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-distinct-rows-when-variables-are-not-one-to/m-p/512343#M137973</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Yes, please see below:&lt;/P&gt;
&lt;P&gt;data p.have;&lt;BR /&gt;input id code diff_range weight_evidence;&lt;BR /&gt;cards;&lt;BR /&gt;1 1 2 10 &lt;BR /&gt;1 2 2 8 &lt;BR /&gt;1 3 4 0&lt;BR /&gt;1 3 4 0&lt;BR /&gt;1 5 7 0&lt;BR /&gt;2 1 1 10&lt;BR /&gt;2 2 3 8&lt;BR /&gt;2 2 3 8&lt;BR /&gt;2 4 5 0&lt;BR /&gt;2 5 6 0&lt;BR /&gt;3 3 1 0&lt;BR /&gt;3 4 2 0&lt;BR /&gt;3 5 3 0&lt;BR /&gt;4 8 8 0&lt;BR /&gt;4 9 9 0&lt;BR /&gt;5 6 10 6&lt;BR /&gt;5 1 10 10 &lt;BR /&gt;5 10 12 0&lt;BR /&gt;6 1 2 10 &lt;BR /&gt;6 2 2 8&lt;BR /&gt;6 1 9 10&lt;BR /&gt;6 6 9 6 &lt;BR /&gt;;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Nov 2018 20:47:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-distinct-rows-when-variables-are-not-one-to/m-p/512343#M137973</guid>
      <dc:creator>Cruise</dc:creator>
      <dc:date>2018-11-12T20:47:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to select the distinct rows with when variables are not one-to-one with group by variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-distinct-rows-when-variables-are-not-one-to/m-p/512344#M137974</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input id code diff_range weight_evidence;
cards;
1 1 2 10 
1 2 2 8 
1 3 4 0
1 3 4 0
1 5 7 0
2 1 1 10
2 2 3 8
2 2 3 8
2 4 5 0
2 5 6 0
3 3 1 0
3 4 2 0
3 5 3 0
4 8 8 0
4 9 9 0
5 6 10 6
5 1 10 10 
5 10 12 0
6 1 2 10 
6 2 2 8
6 1 9 10
6 6 9 6 
;
proc sql;
create table want_min1 as
select *
from 
(select distinct * , min(diff_range)as min_diff from have
where id in 
(select id from have where code in (1,2,6))
group by id)
group by id,min_diff
having weight_evidence=max(weight_evidence);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;PS I will simplify the above if i can&lt;/P&gt;</description>
      <pubDate>Mon, 12 Nov 2018 21:02:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-distinct-rows-when-variables-are-not-one-to/m-p/512344#M137974</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-11-12T21:02:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to select the distinct rows with when variables are not one-to-one with group by variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-distinct-rows-when-variables-are-not-one-to/m-p/512347#M137977</link>
      <description>Does the minimum code have to be with diagnosis 1, 2, or 6? Or just a patient with one of those diagnosis? That will help novinosrin simplify his code.</description>
      <pubDate>Mon, 12 Nov 2018 21:09:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-distinct-rows-when-variables-are-not-one-to/m-p/512347#M137977</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-11-12T21:09:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to select the distinct rows with when variables are not one-to-one with group by variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-distinct-rows-when-variables-are-not-one-to/m-p/512348#M137978</link>
      <description>Cruise, can you post the expected output as well, how the weights need to be handled is unclear. Is it just part of the selection or are you reweighting?</description>
      <pubDate>Mon, 12 Nov 2018 21:11:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-distinct-rows-when-variables-are-not-one-to/m-p/512348#M137978</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-11-12T21:11:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to select the distinct rows with when variables are not one-to-one with group by variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-distinct-rows-when-variables-are-not-one-to/m-p/512395#M137995</link>
      <description>&lt;P&gt;hI&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;WEIGHT_EVIDENCE IS CREATED BASED ON THE CODE LIKE FOLLOWING:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if code=1 then weight_evidence=10; else &lt;BR /&gt;if code=2 then weight_evidence=8; else &lt;BR /&gt;if code=6 then weight_evidence=6;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;HOWEVER, MIN_DIFF CAN RANGE 1 THRU 30.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In ideal world, where life goes our way, maximum weight of evidence&amp;nbsp;would be corresponded with the minimum difference. However, there are discordant cases, which I thought of following as shown int he image. Please help me think of more discordant scenario if seem not considered here.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The Rationale for Selection and the Selected Cases should answer&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;to understand how weight should be handled in the final output data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SELECTION_CRITERIA.jpg" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/24856i1D58BA9475E574CC/image-size/large?v=v2&amp;amp;px=999" role="button" title="SELECTION_CRITERIA.jpg" alt="SELECTION_CRITERIA.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Anything apparently incorrect? please let me know. I needed to think of what discordant cases might be. My actual data is huge and I have to resolve the issue on these hypothetical small dataset first. Hope that makes sense.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Nov 2018 22:17:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-distinct-rows-when-variables-are-not-one-to/m-p/512395#M137995</guid>
      <dc:creator>Cruise</dc:creator>
      <dc:date>2018-11-12T22:17:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to select the distinct rows with when variables are not one-to-one with group by variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-distinct-rows-when-variables-are-not-one-to/m-p/512397#M137996</link>
      <description>Rather than try and think of all circumstances, what happens if you find all the cases that don't have issues, ie where min diff and max_weight and then see how many you have to deal with and consider them case by case?</description>
      <pubDate>Mon, 12 Nov 2018 22:20:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-distinct-rows-when-variables-are-not-one-to/m-p/512397#M137996</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-11-12T22:20:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to select the distinct rows with when variables are not one-to-one with group by variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-distinct-rows-when-variables-are-not-one-to/m-p/512398#M137997</link>
      <description>&lt;P&gt;I think the my sql will do that, test and let us know. Also, if you want to make your life easy, of course sort and datastep is by far simplest.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The below makes sql my style with fancy&amp;nbsp; boolean although not much of big deal from the previous&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input id code diff_range weight_evidence;
cards;
1 1 2 10 
1 2 2 8 
1 3 4 0
1 3 4 0
1 5 7 0
2 1 1 10
2 2 3 8
2 2 3 8
2 4 5 0
2 5 6 0
3 3 1 0
3 4 2 0
3 5 3 0
4 8 8 0
4 9 9 0
5 6 10 6
5 1 10 10 
5 10 12 0
6 1 2 10 
6 2 2 8
6 1 9 10
6 6 9 6 
;
proc sql;
create table want(drop=_:) as
select *
from 
(select *, max(code in (1,2,6)) as _c,min(diff_range) as _min from have group by id)
group by id,_min
having _c and max(weight_evidence)=weight_evidence and _min=diff_range;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Nov 2018 22:30:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-distinct-rows-when-variables-are-not-one-to/m-p/512398#M137997</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-11-12T22:30:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to select the distinct rows with when variables are not one-to-one with group by variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-distinct-rows-when-variables-are-not-one-to/m-p/512401#M137998</link>
      <description>&lt;P&gt;@Reeza, I'll test my real data with both approaches suggested by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;. I'll let you know what happened.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Nov 2018 22:27:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-distinct-rows-when-variables-are-not-one-to/m-p/512401#M137998</guid>
      <dc:creator>Cruise</dc:creator>
      <dc:date>2018-11-12T22:27:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to select the distinct rows with when variables are not one-to-one with group by variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-distinct-rows-when-variables-are-not-one-to/m-p/512402#M137999</link>
      <description>why max(code in (1,2,6))? codes will be assessed selected by max(weight_evidence) later?</description>
      <pubDate>Mon, 12 Nov 2018 22:30:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-distinct-rows-when-variables-are-not-one-to/m-p/512402#M137999</guid>
      <dc:creator>Cruise</dc:creator>
      <dc:date>2018-11-12T22:30:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to select the distinct rows with when variables are not one-to-one with group by variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-distinct-rows-when-variables-are-not-one-to/m-p/512403#M138000</link>
      <description>&lt;P&gt;&lt;SPAN&gt;max(code in (1,2,6))? results in a boolean 1s for id with 1,2,6 and 0's for id's that doesn't have 1, 2,6 . and i pick the 1s as you can imagine&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;I am not far away from mastering proc sql inside out. So i love playing all forms of it.. It's almost getting to the point, as the requirement narrates to my mind, the code is in my fingers &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Nov 2018 22:33:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-distinct-rows-when-variables-are-not-one-to/m-p/512403#M138000</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-11-12T22:33:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to select the distinct rows with when variables are not one-to-one with group by variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-distinct-rows-when-variables-are-not-one-to/m-p/512440#M138022</link>
      <description>&lt;HR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/132289"&gt;@Cruise&lt;/a&gt;&amp;nbsp;wrote:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;hI&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;WEIGHT_EVIDENCE IS CREATED BASED ON THE CODE LIKE FOLLOWING:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if code=1 then weight_evidence=10; else &lt;BR /&gt;if code=2 then weight_evidence=8; else &lt;BR /&gt;if code=6 then weight_evidence=6;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You've just told us that weight is a monotonic descending function of code.&amp;nbsp; So a "by ... descending WGT" produces the&amp;nbsp;same order as "by ... CODE".&amp;nbsp;&amp;nbsp; So I believe this will work:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data= have (where=(code in (1,2,6))) out=need;
  by Id diff_range code;
run;

data want;
  set need; 
  by id diff_range;
  if first.id;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token procnames"&gt;which will give you the smallest diff_ranges for each id.&amp;nbsp; And in the case of tied smallest diff_range, it will choose the smallest code (i.e. the largest weight).&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Nov 2018 03:55:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-distinct-rows-when-variables-are-not-one-to/m-p/512440#M138022</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-11-13T03:55:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to select the distinct rows with when variables are not one-to-one with group by variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-distinct-rows-when-variables-are-not-one-to/m-p/512522#M138061</link>
      <description>it sounds like you're a poet writing a sonnet in SQL language</description>
      <pubDate>Tue, 13 Nov 2018 12:53:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-select-the-distinct-rows-when-variables-are-not-one-to/m-p/512522#M138061</guid>
      <dc:creator>Cruise</dc:creator>
      <dc:date>2018-11-13T12:53:31Z</dc:date>
    </item>
  </channel>
</rss>

