<?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 put duplicates into different groups based on the original order? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-put-duplicates-into-different-groups-based-on-the/m-p/887883#M350785</link>
    <description>Work like charm! Thank you!</description>
    <pubDate>Fri, 04 Aug 2023 14:26:45 GMT</pubDate>
    <dc:creator>SAS-questioner</dc:creator>
    <dc:date>2023-08-04T14:26:45Z</dc:date>
    <item>
      <title>How to put duplicates into different groups based on the original order?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-put-duplicates-into-different-groups-based-on-the/m-p/887792#M350739</link>
      <description>&lt;P&gt;Hi, I have a question about how to put duplicates into different groups based on the original order? I have a data like below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;ID  condition
1        A
1        A
2        B
2        B
3        A
3        A
3        B
3        B
4        B
4        B
4        A
4        A
5        A
5        A
5       B
5       B
5       A
5       A       &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The data is like above, basically, there are five situations: 1. all As; 2, all Bs; 3, A first and then B; 4, B first and then A; 5, A first and then B, and then A.&lt;/P&gt;&lt;P&gt;I want to keep the original order, which means I can't use proc sort, because after I use proc sort, AABBAA or BBAA will become AAAABB and AABB.&lt;/P&gt;&lt;P&gt;What I want to do is to put ID with all As in group 1, put ID with all Bs in group 2, and put ID with A first and then B in to group 3. So, for the rest of patterns like B first and then A, or A first and then B, and then A can be removed. I am wondering if there is a way to do it? Hopefully that I express myself clearly. Thank you!&lt;/P&gt;</description>
      <pubDate>Thu, 03 Aug 2023 19:30:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-put-duplicates-into-different-groups-based-on-the/m-p/887792#M350739</guid>
      <dc:creator>SAS-questioner</dc:creator>
      <dc:date>2023-08-03T19:30:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to put duplicates into different groups based on the original order?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-put-duplicates-into-different-groups-based-on-the/m-p/887795#M350741</link>
      <description>&lt;P&gt;If you just want those specific groups AB, BA, A , B and perhaps none then you could do something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  do row=1 by 1 until (last.id);
    set have;
    by id;
    if condition='A' then a=min(a,row);
    else if condition='B' then b=min(b,row);
  end;
  length group $2 ;
  if . &amp;lt; a &amp;lt; b then group='AB';
  else if . &amp;lt; b &amp;lt; a then group='BA';
  else if . &amp;lt; b then group='B';
  else if . &amp;lt; a then group='A';
  keep id group;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs    ID    group

 1     1      A
 2     2      B
 3     3      AB
 4     4      BA
 5     5      AB

&lt;/PRE&gt;
&lt;P&gt;If you also want to look for longer patterns, like ABA&amp;nbsp;you might use something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by id condition notsorted;
  length group $8 ;
  retain group;
  if first.id then group=' ';
  if first.condition then group=cats(group,condition);
  if last.id;
  keep id group;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;Obs    ID    group

 1     1      A
 2     2      B
 3     3      AB
 4     4      BA
 5     5      ABA
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 03 Aug 2023 19:49:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-put-duplicates-into-different-groups-based-on-the/m-p/887795#M350741</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-08-03T19:49:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to put duplicates into different groups based on the original order?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-put-duplicates-into-different-groups-based-on-the/m-p/887883#M350785</link>
      <description>Work like charm! Thank you!</description>
      <pubDate>Fri, 04 Aug 2023 14:26:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-put-duplicates-into-different-groups-based-on-the/m-p/887883#M350785</guid>
      <dc:creator>SAS-questioner</dc:creator>
      <dc:date>2023-08-04T14:26:45Z</dc:date>
    </item>
  </channel>
</rss>

