<?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 Generate group id based on multiple parameters in &amp;quot;or&amp;quot; condition in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Generate-group-id-based-on-multiple-parameters-in-quot-or-quot/m-p/639491#M190263</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I have a business problem where i want to generate group id against every row. Grouping will be done based on 3 parameters. sample input and expected output is below.&lt;/P&gt;&lt;P&gt;input.&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;custid&lt;/TD&gt;&lt;TD&gt;var1&lt;/TD&gt;&lt;TD&gt;var2&lt;/TD&gt;&lt;TD&gt;var3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;a11&lt;/TD&gt;&lt;TD&gt;b11&lt;/TD&gt;&lt;TD&gt;c11&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;a12&lt;/TD&gt;&lt;TD&gt;b11&lt;/TD&gt;&lt;TD&gt;c12&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;a13&lt;/TD&gt;&lt;TD&gt;b13&lt;/TD&gt;&lt;TD&gt;c12&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;a14&lt;/TD&gt;&lt;TD&gt;b14&lt;/TD&gt;&lt;TD&gt;c14&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Output:&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;custid&lt;/TD&gt;&lt;TD&gt;var1&lt;/TD&gt;&lt;TD&gt;var2&lt;/TD&gt;&lt;TD&gt;var3&lt;/TD&gt;&lt;TD&gt;groupid&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;a11&lt;/TD&gt;&lt;TD&gt;b11&lt;/TD&gt;&lt;TD&gt;c11&lt;/TD&gt;&lt;TD&gt;G1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;a12&lt;/TD&gt;&lt;TD&gt;b11&lt;/TD&gt;&lt;TD&gt;c12&lt;/TD&gt;&lt;TD&gt;G1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;a13&lt;/TD&gt;&lt;TD&gt;b13&lt;/TD&gt;&lt;TD&gt;c12&lt;/TD&gt;&lt;TD&gt;G1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;a14&lt;/TD&gt;&lt;TD&gt;b14&lt;/TD&gt;&lt;TD&gt;c14&lt;/TD&gt;&lt;TD&gt;G2&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
    <pubDate>Mon, 13 Apr 2020 15:19:44 GMT</pubDate>
    <dc:creator>arpitagarwal512</dc:creator>
    <dc:date>2020-04-13T15:19:44Z</dc:date>
    <item>
      <title>Generate group id based on multiple parameters in "or" condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generate-group-id-based-on-multiple-parameters-in-quot-or-quot/m-p/639491#M190263</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I have a business problem where i want to generate group id against every row. Grouping will be done based on 3 parameters. sample input and expected output is below.&lt;/P&gt;&lt;P&gt;input.&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;custid&lt;/TD&gt;&lt;TD&gt;var1&lt;/TD&gt;&lt;TD&gt;var2&lt;/TD&gt;&lt;TD&gt;var3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;a11&lt;/TD&gt;&lt;TD&gt;b11&lt;/TD&gt;&lt;TD&gt;c11&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;a12&lt;/TD&gt;&lt;TD&gt;b11&lt;/TD&gt;&lt;TD&gt;c12&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;a13&lt;/TD&gt;&lt;TD&gt;b13&lt;/TD&gt;&lt;TD&gt;c12&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;a14&lt;/TD&gt;&lt;TD&gt;b14&lt;/TD&gt;&lt;TD&gt;c14&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Output:&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;custid&lt;/TD&gt;&lt;TD&gt;var1&lt;/TD&gt;&lt;TD&gt;var2&lt;/TD&gt;&lt;TD&gt;var3&lt;/TD&gt;&lt;TD&gt;groupid&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;a11&lt;/TD&gt;&lt;TD&gt;b11&lt;/TD&gt;&lt;TD&gt;c11&lt;/TD&gt;&lt;TD&gt;G1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;a12&lt;/TD&gt;&lt;TD&gt;b11&lt;/TD&gt;&lt;TD&gt;c12&lt;/TD&gt;&lt;TD&gt;G1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;a13&lt;/TD&gt;&lt;TD&gt;b13&lt;/TD&gt;&lt;TD&gt;c12&lt;/TD&gt;&lt;TD&gt;G1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;a14&lt;/TD&gt;&lt;TD&gt;b14&lt;/TD&gt;&lt;TD&gt;c14&lt;/TD&gt;&lt;TD&gt;G2&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Mon, 13 Apr 2020 15:19:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generate-group-id-based-on-multiple-parameters-in-quot-or-quot/m-p/639491#M190263</guid>
      <dc:creator>arpitagarwal512</dc:creator>
      <dc:date>2020-04-13T15:19:44Z</dc:date>
    </item>
    <item>
      <title>Re: Generate group id based on multiple parameters in "or" condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generate-group-id-based-on-multiple-parameters-in-quot-or-quot/m-p/639493#M190264</link>
      <description>&lt;P&gt;And what is the rule or rules involved.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With the example you have provided we have no idea what the "group id" value would be if a value of a15, b15 or c15 appears&lt;/P&gt;</description>
      <pubDate>Mon, 13 Apr 2020 15:25:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generate-group-id-based-on-multiple-parameters-in-quot-or-quot/m-p/639493#M190264</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-04-13T15:25:56Z</dc:date>
    </item>
    <item>
      <title>Re: Generate group id based on multiple parameters in "or" condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generate-group-id-based-on-multiple-parameters-in-quot-or-quot/m-p/639495#M190266</link>
      <description>group id is a sequence no prefix with "G" and starts from 1. value inside is a static value</description>
      <pubDate>Mon, 13 Apr 2020 15:28:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generate-group-id-based-on-multiple-parameters-in-quot-or-quot/m-p/639495#M190266</guid>
      <dc:creator>arpitagarwal512</dc:creator>
      <dc:date>2020-04-13T15:28:54Z</dc:date>
    </item>
    <item>
      <title>Re: Generate group id based on multiple parameters in "or" condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generate-group-id-based-on-multiple-parameters-in-quot-or-quot/m-p/639497#M190267</link>
      <description>here in first row, column var2 value is "b11" and similar value available in second row so ideally row1 and row 2 belongs to same group. now in second row, column v3 have same value in third row v3 column value, so second row and third row also belongs to same group.&lt;BR /&gt;row 4 column value don't have common value in any of the row so row 4 belongs to different group.&lt;BR /&gt;Now,&lt;BR /&gt;row1 and row 2 belongs to same group . row2 and row 3 belongs to same group&lt;BR /&gt;i.e row 1, row 2 and row 3 belongs to same group . So G1 group id assigned&lt;BR /&gt;rest row4 assigned with G2 group is</description>
      <pubDate>Mon, 13 Apr 2020 15:34:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generate-group-id-based-on-multiple-parameters-in-quot-or-quot/m-p/639497#M190267</guid>
      <dc:creator>arpitagarwal512</dc:creator>
      <dc:date>2020-04-13T15:34:15Z</dc:date>
    </item>
    <item>
      <title>Re: Generate group id based on multiple parameters in "or" condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generate-group-id-based-on-multiple-parameters-in-quot-or-quot/m-p/639516#M190276</link>
      <description>&lt;P&gt;May I ask how big is your data or how many &lt;STRONG&gt;custid&lt;/STRONG&gt;s are there?&lt;/P&gt;
&lt;P&gt;and how many groups, approximately, you expect?&lt;/P&gt;
&lt;P&gt;I have the feeling you'll need a complex compare, maybe recursive, to assign the final group - to your sample test:&lt;/P&gt;
&lt;P&gt;comparing var1 there are four different groups,&lt;/P&gt;
&lt;P&gt;comparing var2 will eliminate it into 3 groups,&lt;/P&gt;
&lt;P&gt;comparing var3 will eliminate it into 2 groups.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If I add a 5th custid as: a11-b15-c16 - will it be assigned as G1?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The technical code may depend on data-set size.&lt;/P&gt;</description>
      <pubDate>Mon, 13 Apr 2020 16:32:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generate-group-id-based-on-multiple-parameters-in-quot-or-quot/m-p/639516#M190276</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2020-04-13T16:32:23Z</dc:date>
    </item>
    <item>
      <title>Re: Generate group id based on multiple parameters in "or" condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generate-group-id-based-on-multiple-parameters-in-quot-or-quot/m-p/639517#M190277</link>
      <description>data will be around 3 million rows and expected group will be around .7 million.&lt;BR /&gt;&lt;BR /&gt;If I add a 5th custid as: a11-b15-c16 - will it be assigned as G1&lt;BR /&gt;above statement is correct because a11 is common in first and fifth row for column var1</description>
      <pubDate>Mon, 13 Apr 2020 16:37:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generate-group-id-based-on-multiple-parameters-in-quot-or-quot/m-p/639517#M190277</guid>
      <dc:creator>arpitagarwal512</dc:creator>
      <dc:date>2020-04-13T16:37:04Z</dc:date>
    </item>
    <item>
      <title>Re: Generate group id based on multiple parameters in "or" condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generate-group-id-based-on-multiple-parameters-in-quot-or-quot/m-p/639521#M190280</link>
      <description>&lt;P&gt;Use &lt;STRONG&gt;proc optnet&lt;/STRONG&gt; to find connected components:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input custid  var1$    var2$    var3$;
datalines;
1   a11 b11 c11
2   a12 b11 c12
3   a13 b13 c12
4   a14 b14 c14
;

proc transpose data=have out=temp1;
by custid;
var var1-var3;
run;

proc sql;
create table links as
select unique
	a.custid as from,
	b.custid as to
from 
	temp1 as a inner join
	temp1 as b on a.col1=b.col1 and a.custid &amp;lt;= b.custid;
quit;

proc optnet data_links=links GRAPH_DIRECTION=UNDIRECTED out_nodes=groups;
concomp;
run;

proc sql;
create table want as
select
	a.*,
	cats("G", b.concomp) as groupid
from 
	have as a left join
	groups as b on a.custid=b.node;
*drop table temp1 links groups;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 13 Apr 2020 16:50:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generate-group-id-based-on-multiple-parameters-in-quot-or-quot/m-p/639521#M190280</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-04-13T16:50:26Z</dc:date>
    </item>
    <item>
      <title>Re: Generate group id based on multiple parameters in "or" condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generate-group-id-based-on-multiple-parameters-in-quot-or-quot/m-p/639675#M190346</link>
      <description>I don't have license of proc optnet instead i am using sas viya where i got to know proc optnetwork procedure. does it will same as proc optnet?&lt;BR /&gt;</description>
      <pubDate>Tue, 14 Apr 2020 09:44:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generate-group-id-based-on-multiple-parameters-in-quot-or-quot/m-p/639675#M190346</guid>
      <dc:creator>arpitagarwal512</dc:creator>
      <dc:date>2020-04-14T09:44:04Z</dc:date>
    </item>
    <item>
      <title>Re: Generate group id based on multiple parameters in "or" condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generate-group-id-based-on-multiple-parameters-in-quot-or-quot/m-p/639713#M190367</link>
      <description>&lt;P&gt;You are trying to get linked components .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
input custid  var1$    var2$    var3$;
datalines;
1   a11 b11 c11
2   a12 b11 c12
3   a13 b13 c12
4   a14 b14 c14
;
data have;
 set temp;
 array x{*} $ var1-var3;
 do i=1 to dim(x)-1;
   from=x{i};
   do j=i+1 to dim(x);
     to=x{j};output;
   end;
 end;
 keep from to;
run;
data full;
  set have end=last;
  if _n_ eq 1 then do;
   declare hash h();
    h.definekey('node');
     h.definedata('node');
     h.definedone();
  end;
  output;
  node=from; h.replace();
  from=to; to=node;
  output;
  node=from; h.replace();
  if last then h.output(dataset:'node');
  drop node;
run;


data want(keep=node household);
declare hash ha(ordered:'a');
declare hiter hi('ha');
ha.definekey('count');
ha.definedata('last');
ha.definedone();
declare hash _ha(hashexp: 20);
_ha.definekey('key');
_ha.definedone();

if 0 then set full;
declare hash from_to(dataset:'full(where=(from is not missing and to is not missing))',hashexp:20,multidata:'y');
 from_to.definekey('from');
 from_to.definedata('to');
 from_to.definedone();

if 0 then set node;
declare hash no(dataset:'node');
declare hiter hi_no('no');
 no.definekey('node');
 no.definedata('node');
 no.definedone();
 

do while(hi_no.next()=0);
 household+1; output;
 count=1;
 key=node;_ha.add();
 last=node;ha.add();
 rc=hi.first();
 do while(rc=0);
   from=last;rx=from_to.find();
   do while(rx=0);
     key=to;ry=_ha.check();
      if ry ne 0 then do;
       node=to;output;rr=no.remove(key:node);
       key=to;_ha.add();
       count+1;
       last=to;ha.add();
      end;
      rx=from_to.find_next();
   end;
   rc=hi.next();
end;
ha.clear();_ha.clear();
end;
stop;
run;

data final_want;
if _n_=1 then do;
  if 0 then set want;
  declare hash h(dataset:'want',hashexp:20);
  h.definekey('node');
  h.definedata('household');
  h.definedone();
end;
 set temp;
call missing(household);
rc=h.find(key:var1);&lt;BR /&gt;length groupid $ 20 ;
groupid=cats('G',household);
drop rc node household;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 14 Apr 2020 12:23:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generate-group-id-based-on-multiple-parameters-in-quot-or-quot/m-p/639713#M190367</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-04-14T12:23:33Z</dc:date>
    </item>
  </channel>
</rss>

