<?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: If all entries in a group are same or different in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/If-all-entries-in-a-group-are-same-or-different/m-p/658504#M197356</link>
    <description>&lt;P&gt;Please assume all variables are numeric. And, I want to identify all IDs that have different values for the variable "type of drug". Preferable in SQL. If not, any other way also will work.&lt;/P&gt;&lt;P&gt;Thank you.&lt;/P&gt;</description>
    <pubDate>Mon, 15 Jun 2020 05:21:57 GMT</pubDate>
    <dc:creator>Bhontu</dc:creator>
    <dc:date>2020-06-15T05:21:57Z</dc:date>
    <item>
      <title>If all entries in a group are same or different</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-all-entries-in-a-group-are-same-or-different/m-p/658501#M197355</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;Any help will be much appreciated.&lt;/P&gt;&lt;P&gt;Here is a replica of my dataset:&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Type of Drug&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; T1&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; T1&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; T2&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;T2&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; T2&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; T2&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; T3&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; T3&lt;/P&gt;&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; T4&lt;/P&gt;&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; T4&lt;/P&gt;&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; TX&lt;/P&gt;&lt;P&gt;I want to find out if for each ID the entries under variable "type of drug" are the same or not.&amp;nbsp; If they are not the same then how can I identify the IDs for which entry for variable "type of drug" is not same throughout? In the example dataset above, each entry under "type of drug" for ID 4 is different. So I want the code to identify ID 4.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you in advance.&lt;/P&gt;</description>
      <pubDate>Mon, 15 Jun 2020 05:19:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-all-entries-in-a-group-are-same-or-different/m-p/658501#M197355</guid>
      <dc:creator>Bhontu</dc:creator>
      <dc:date>2020-06-15T05:19:07Z</dc:date>
    </item>
    <item>
      <title>Re: If all entries in a group are same or different</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-all-entries-in-a-group-are-same-or-different/m-p/658504#M197356</link>
      <description>&lt;P&gt;Please assume all variables are numeric. And, I want to identify all IDs that have different values for the variable "type of drug". Preferable in SQL. If not, any other way also will work.&lt;/P&gt;&lt;P&gt;Thank you.&lt;/P&gt;</description>
      <pubDate>Mon, 15 Jun 2020 05:21:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-all-entries-in-a-group-are-same-or-different/m-p/658504#M197356</guid>
      <dc:creator>Bhontu</dc:creator>
      <dc:date>2020-06-15T05:21:57Z</dc:date>
    </item>
    <item>
      <title>Re: If all entries in a group are same or different</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-all-entries-in-a-group-are-same-or-different/m-p/658518#M197358</link>
      <description>&lt;P&gt;How about&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID Type $;
datalines;
1 T1
1 T1
2 T2
2 T2
2 T2
2 T2
3 T3
3 T3
4 T4
4 T4
4 TX
;

proc sql;
    create table want as
    select *, count(distinct Type)&amp;gt;1 as flag
    from have 
    group by ID;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 Jun 2020 05:35:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-all-entries-in-a-group-are-same-or-different/m-p/658518#M197358</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-06-15T05:35:40Z</dc:date>
    </item>
    <item>
      <title>Re: If all entries in a group are same or different</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-all-entries-in-a-group-are-same-or-different/m-p/658520#M197359</link>
      <description>&lt;P&gt;Below if you also want to know how many distinct types you've got for an ID with more than one type.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID Type $;
  datalines;
1 T1
1 T1
2 T2
2 T2
2 T2
2 T2
3 T3
3 T3
4 T4
4 T4
4 TX
;

proc sql;
/*  create table want as*/
    select ID, count(distinct Type) as n_types
    from have 
    group by ID
    having n_types&amp;gt;1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1592199895074.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/42940i478D4E4E8CDA74BF/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1592199895074.png" alt="Patrick_0-1592199895074.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Jun 2020 05:45:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-all-entries-in-a-group-are-same-or-different/m-p/658520#M197359</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-06-15T05:45:03Z</dc:date>
    </item>
    <item>
      <title>Re: If all entries in a group are same or different</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-all-entries-in-a-group-are-same-or-different/m-p/658545#M197360</link>
      <description>&lt;P&gt;You can, of course, use a data step to create a dataset containing all observations not having the the same type as the first observation (per ID).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set have;
	by ID;
	
	lastType = lag(Type);
	
	if not first.ID and lastType ^= Type then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 Jun 2020 06:23:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-all-entries-in-a-group-are-same-or-different/m-p/658545#M197360</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-06-15T06:23:08Z</dc:date>
    </item>
    <item>
      <title>Re: If all entries in a group are same or different</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-all-entries-in-a-group-are-same-or-different/m-p/658546#M197361</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;you could also use double DoW-loop to recognise group with doubles and output it all at the same time:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID Type $;
datalines;
1 T1
1 T1
2 T2
2 T2
2 T2
2 T2
3 T3
3 T3
4 T4
4 T4
4 TX
;
run;

data want;
  do _N_ = 1 by 1  until(last.ID);
    set have;
    by id;
    if first.id then _first_ = Type;
    _marker_ = (_first_ ^= Type);
  end;

  do _N_ = 1 to _N_;
    set have;
    if _marker_ then output;
  end;
  drop _:;
run;
proc print data  = want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here is classic article by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/21262"&gt;@hashman&lt;/a&gt;&amp;nbsp;which describe the idea of DoW-loop:&amp;nbsp;&lt;A href="https://support.sas.com/resources/papers/proceedings13/126-2013.pdf" target="_blank"&gt;https://support.sas.com/resources/papers/proceedings13/126-2013.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;All the best&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Mon, 15 Jun 2020 06:23:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-all-entries-in-a-group-are-same-or-different/m-p/658546#M197361</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-06-15T06:23:37Z</dc:date>
    </item>
    <item>
      <title>Re: If all entries in a group are same or different</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-all-entries-in-a-group-are-same-or-different/m-p/658917#M197394</link>
      <description>&lt;PRE&gt;proc sql;
 create table want as
    select *
    from have 
    group by ID
    having count(distinct Type)&amp;gt;1;
quit;&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 Jun 2020 10:54:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-all-entries-in-a-group-are-same-or-different/m-p/658917#M197394</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-06-15T10:54:31Z</dc:date>
    </item>
    <item>
      <title>Re: If all entries in a group are same or different</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-all-entries-in-a-group-are-same-or-different/m-p/659173#M197532</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;Thank you so much. All the options worked for me and more. Much appreciate.&lt;/P&gt;</description>
      <pubDate>Mon, 15 Jun 2020 23:58:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-all-entries-in-a-group-are-same-or-different/m-p/659173#M197532</guid>
      <dc:creator>Bhontu</dc:creator>
      <dc:date>2020-06-15T23:58:40Z</dc:date>
    </item>
  </channel>
</rss>

