<?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 Select subjects based on the combination of values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Select-subjects-based-on-the-combination-of-values/m-p/549368#M152430</link>
    <description>&lt;P&gt;Hi folks,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm trying to subset patients who takes combination of two specific values which means:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Select patients who takes both value '1 and 2' for "weight" variable such as ID=2: 4 in the data 'have'. Ignore patients who takes '1 or 2', or none such as ID=1: 3: 5.&lt;/P&gt;
&lt;P&gt;2. If subjects tie on "weight" then take the row associated with the least value of 'diff_range' as shown in data want.&lt;/P&gt;
&lt;P&gt;3. If data ties on diff_range then I can proc sort nodupkey by extra step, so don't worry about that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The current SAS code keeps ID 1:3:5 in the 'want' data which I'm trying to eliminate.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Could you please help just to retrieve ID 2:4 in the final data as described step-wise above? &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id weight diff_range;
cards;
1	1	2
1	1	21
1	1	54
1	1	87
2	1	80
2	1	90
2	2	100
2	2	120
2	3	100
3	1	21
3	4	3
3	5	0
3	6	2
3	6	2
4	1	200
4	1	250
4	2	30
4	2	50
4	4	23
4	5	56
4	5	89
5	2	23
5	2	56
5	2	78
;
run;

data want;
input id weight diff_range;
cards;
2	1	80
2	2	100
4	1	200
4	2	30
;
run;

proc sql;
create table want(drop=_:) as
select *
from 
(select *, max(weight in (1,2)) as _c,min(diff_range) as _min from have group by id, weight)
group by id
having _c and _min=diff_range;
quit;
proc print; run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 08 Apr 2019 18:16:56 GMT</pubDate>
    <dc:creator>Cruise</dc:creator>
    <dc:date>2019-04-08T18:16:56Z</dc:date>
    <item>
      <title>Select subjects based on the combination of values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-subjects-based-on-the-combination-of-values/m-p/549368#M152430</link>
      <description>&lt;P&gt;Hi folks,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm trying to subset patients who takes combination of two specific values which means:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Select patients who takes both value '1 and 2' for "weight" variable such as ID=2: 4 in the data 'have'. Ignore patients who takes '1 or 2', or none such as ID=1: 3: 5.&lt;/P&gt;
&lt;P&gt;2. If subjects tie on "weight" then take the row associated with the least value of 'diff_range' as shown in data want.&lt;/P&gt;
&lt;P&gt;3. If data ties on diff_range then I can proc sort nodupkey by extra step, so don't worry about that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The current SAS code keeps ID 1:3:5 in the 'want' data which I'm trying to eliminate.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Could you please help just to retrieve ID 2:4 in the final data as described step-wise above? &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id weight diff_range;
cards;
1	1	2
1	1	21
1	1	54
1	1	87
2	1	80
2	1	90
2	2	100
2	2	120
2	3	100
3	1	21
3	4	3
3	5	0
3	6	2
3	6	2
4	1	200
4	1	250
4	2	30
4	2	50
4	4	23
4	5	56
4	5	89
5	2	23
5	2	56
5	2	78
;
run;

data want;
input id weight diff_range;
cards;
2	1	80
2	2	100
4	1	200
4	2	30
;
run;

proc sql;
create table want(drop=_:) as
select *
from 
(select *, max(weight in (1,2)) as _c,min(diff_range) as _min from have group by id, weight)
group by id
having _c and _min=diff_range;
quit;
proc print; run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Apr 2019 18:16:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-subjects-based-on-the-combination-of-values/m-p/549368#M152430</guid>
      <dc:creator>Cruise</dc:creator>
      <dc:date>2019-04-08T18:16:56Z</dc:date>
    </item>
    <item>
      <title>Re: Select subjects based on the combination of values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-subjects-based-on-the-combination-of-values/m-p/549378#M152432</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/132289"&gt;@Cruise&lt;/a&gt;&amp;nbsp; &amp;nbsp;enjoy&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input id weight diff_range;
cards;
1	1	2
1	1	21
1	1	54
1	1	87
2	1	80
2	1	90
2	2	100
2	2	120
2	3	100
3	1	21
3	4	3
3	5	0
3	6	2
3	6	2
4	1	200
4	1	250
4	2	30
4	2	50
4	4	23
4	5	56
4	5	89
5	2	23
5	2	56
5	2	78
;
run;


proc sql;
create table  want as
select *
from
(select * from have where weight in (1,2) group by id
having sum(distinct weight)=3)
group by id, weight
having diff_range=min(diff_range)
order by id, weight;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 08 Apr 2019 18:39:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-subjects-based-on-the-combination-of-values/m-p/549378#M152432</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-04-08T18:39:01Z</dc:date>
    </item>
    <item>
      <title>Re: Select subjects based on the combination of values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-subjects-based-on-the-combination-of-values/m-p/549408#M152437</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt; Thanks&amp;nbsp; a lot. Smart solution indeed. &lt;/P&gt;</description>
      <pubDate>Mon, 08 Apr 2019 20:01:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-subjects-based-on-the-combination-of-values/m-p/549408#M152437</guid>
      <dc:creator>Cruise</dc:creator>
      <dc:date>2019-04-08T20:01:47Z</dc:date>
    </item>
    <item>
      <title>Re: Select subjects based on the combination of values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Select-subjects-based-on-the-combination-of-values/m-p/549409#M152438</link>
      <description>&lt;P&gt;Always welcome!&lt;/P&gt;</description>
      <pubDate>Mon, 08 Apr 2019 20:02:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Select-subjects-based-on-the-combination-of-values/m-p/549409#M152438</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-04-08T20:02:21Z</dc:date>
    </item>
  </channel>
</rss>

