<?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: Creating a grouper column from multiple columns, diagonal records in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-grouper-column-from-multiple-columns-diagonal-records/m-p/622821#M183237</link>
    <description>Thanks, this appears responsive. I really appreciate the help!</description>
    <pubDate>Thu, 06 Feb 2020 18:49:57 GMT</pubDate>
    <dc:creator>paulsonalec</dc:creator>
    <dc:date>2020-02-06T18:49:57Z</dc:date>
    <item>
      <title>Creating a grouper column from multiple columns, diagonal records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-grouper-column-from-multiple-columns-diagonal-records/m-p/622779#M183222</link>
      <description>&lt;P&gt;Hi! given these data:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;infile datalines delimiter=',';&lt;BR /&gt;input id1 $ id2 $;&lt;BR /&gt;datalines;&lt;/P&gt;&lt;P&gt;M,A&lt;BR /&gt;A,B&lt;BR /&gt;C,D&lt;BR /&gt;B,C&lt;BR /&gt;V,W&lt;BR /&gt;W,X&lt;BR /&gt;X,Y&lt;BR /&gt;Y,Z&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am hoping to create a grouper ID that will associate M to A, A to B, B to C, and C to D. Same thing with V to W, W to X, X to Y and Y to Z. Please see below for the data I want:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;infile datalines delimiter=',';&lt;BR /&gt;input id1 $ id2 $ grouper;&lt;BR /&gt;datalines;&lt;/P&gt;&lt;P&gt;M,A,1&lt;BR /&gt;A,B,1&lt;BR /&gt;B,C,1&lt;BR /&gt;C,D,1&lt;BR /&gt;V,W,2&lt;BR /&gt;W,X,2&lt;BR /&gt;X,Y,2&lt;BR /&gt;Y,Z,2&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, in the output, there are 2 groups that are established. Is this possible using SAS 9.4? &lt;STRONG&gt;Please note that I cannot guarantee sort order so ideally the solution does not use first/last/lag commands&lt;/STRONG&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;TIA!&lt;/P&gt;</description>
      <pubDate>Thu, 06 Feb 2020 17:24:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-grouper-column-from-multiple-columns-diagonal-records/m-p/622779#M183222</guid>
      <dc:creator>paulsonalec</dc:creator>
      <dc:date>2020-02-06T17:24:02Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a grouper column from multiple columns, diagonal records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-grouper-column-from-multiple-columns-diagonal-records/m-p/622783#M183223</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/265905"&gt;@paulsonalec&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How would you describe the rule here:&lt;/P&gt;
&lt;P&gt;for example, two records belong to the same group if the last value of id2= the value of id1.&lt;/P&gt;
&lt;P&gt;is that right?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set have;
	_lag = lag(ID2);
	if _lag ne ID1 then count+1;
	retain count;
	drop _lag;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 06 Feb 2020 17:13:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-grouper-column-from-multiple-columns-diagonal-records/m-p/622783#M183223</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-02-06T17:13:21Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a grouper column from multiple columns, diagonal records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-grouper-column-from-multiple-columns-diagonal-records/m-p/622784#M183224</link>
      <description>Thanks! I should have included in my post, but the sort order is not guaranteed, so I don't think first/last functions will work if that makes sense. I will edit my post</description>
      <pubDate>Thu, 06 Feb 2020 17:10:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-grouper-column-from-multiple-columns-diagonal-records/m-p/622784#M183224</guid>
      <dc:creator>paulsonalec</dc:creator>
      <dc:date>2020-02-06T17:10:24Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a grouper column from multiple columns, diagonal records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-grouper-column-from-multiple-columns-diagonal-records/m-p/622786#M183225</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/265905"&gt;@paulsonalec&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for the prompt answer.&lt;/P&gt;
&lt;P&gt;I have updated my post too &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Feb 2020 17:14:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-grouper-column-from-multiple-columns-diagonal-records/m-p/622786#M183225</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-02-06T17:14:51Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a grouper column from multiple columns, diagonal records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-grouper-column-from-multiple-columns-diagonal-records/m-p/622787#M183226</link>
      <description>Thanks! Again, I really wish I would have included this detail in the initial post - sort order cannot be guaranteed. Please see the updated post, which includes the value 'M' related to 'A'. Further, I think this will create 3 groups assuming my first set of data - group 0, 1 &amp;amp; 2, right? Thanks!</description>
      <pubDate>Thu, 06 Feb 2020 17:18:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-grouper-column-from-multiple-columns-diagonal-records/m-p/622787#M183226</guid>
      <dc:creator>paulsonalec</dc:creator>
      <dc:date>2020-02-06T17:18:07Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a grouper column from multiple columns, diagonal records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-grouper-column-from-multiple-columns-diagonal-records/m-p/622789#M183227</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/265905"&gt;@paulsonalec&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the output with the same code:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture d’écran 2020-02-06 à 18.19.17.png" style="width: 98px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/35892iCAF3EA7C4EE7FE35/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Capture d’écran 2020-02-06 à 18.19.17.png" alt="Capture d’écran 2020-02-06 à 18.19.17.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;-&amp;gt; 2 groups are created as the condition is based on the lag value of ID2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Feb 2020 17:20:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-grouper-column-from-multiple-columns-diagonal-records/m-p/622789#M183227</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-02-06T17:20:14Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a grouper column from multiple columns, diagonal records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-grouper-column-from-multiple-columns-diagonal-records/m-p/622791#M183228</link>
      <description>&lt;P&gt;Right, sorry for the confusion. I guess I'm interested in this example, since sort order cannot be guaranteed here (see how I've changed row order of 'C,D' and 'B,C'). Again, I'm sorry for being annoying/not including this detail in the beginning:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines delimiter=',';
input id1 $ id2 $;
datalines;
M,A
A,B
C,D
B,C
V,W
W,X
X,Y
Y,Z
;
data want;
	set have;
	_lag = lag(ID2);
	if _lag ne ID1 then count+1;
	retain count;
	drop _lag;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 06 Feb 2020 17:23:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-grouper-column-from-multiple-columns-diagonal-records/m-p/622791#M183228</guid>
      <dc:creator>paulsonalec</dc:creator>
      <dc:date>2020-02-06T17:23:28Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a grouper column from multiple columns, diagonal records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-grouper-column-from-multiple-columns-diagonal-records/m-p/622795#M183230</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/265905"&gt;@paulsonalec&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It seems to be better if you run a proc sort before the data step:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines delimiter=',';
input id1 $ id2 $;
datalines;
M,A
X,Y
A,B
C,D
B,C
V,W
W,X
Y,Z
;

proc sort data=have out=have_sorted;
	by id2 id1;
run;
data want;
	set have_sorted;
	_lag = lag(ID2);
	if _lag ne ID1 then count+1;
	retain count;
	drop _lag;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 06 Feb 2020 17:34:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-grouper-column-from-multiple-columns-diagonal-records/m-p/622795#M183230</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-02-06T17:34:00Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a grouper column from multiple columns, diagonal records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-grouper-column-from-multiple-columns-diagonal-records/m-p/622800#M183232</link>
      <description>&lt;P&gt;Thanks, again, lag/sort isn't an option here unfortunately. Consider this possibility (I've added row Z,S):&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines delimiter=',';
input id1 $ id2 $;
datalines;
M,A
X,Y
A,B
C,D
B,C
Z,S
V,W
W,X
Y,Z
;

proc sort data=have out=have_sorted;
	by id2 id1;
run;

data want;
	set have_sorted;
	_lag = lag(ID2);
	if _lag ne ID1 then count+1;
	retain count;
	drop _lag;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 06 Feb 2020 17:42:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-grouper-column-from-multiple-columns-diagonal-records/m-p/622800#M183232</guid>
      <dc:creator>paulsonalec</dc:creator>
      <dc:date>2020-02-06T17:42:54Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a grouper column from multiple columns, diagonal records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-grouper-column-from-multiple-columns-diagonal-records/m-p/622802#M183233</link>
      <description>You need a hash solution that's recursive to find your groups. &lt;BR /&gt;&lt;BR /&gt;I believe this solution works or what you need, run the example to see if I'm understanding your problem correctly first. It helps if you make sure your input data is as much related to your input data, ie if sort isn't defined don't show it as sorted. The hash solution doesn't depend on any order. &lt;BR /&gt;&lt;A href="https://gist.github.com/statgeek/14e3aa2a9f718f551cd98134e9ceed30" target="_blank"&gt;https://gist.github.com/statgeek/14e3aa2a9f718f551cd98134e9ceed30&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;There are a few users on here who can develop a solution related to this problem, I'm not one of them &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 06 Feb 2020 17:56:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-grouper-column-from-multiple-columns-diagonal-records/m-p/622802#M183233</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-02-06T17:56:42Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a grouper column from multiple columns, diagonal records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-grouper-column-from-multiple-columns-diagonal-records/m-p/622821#M183237</link>
      <description>Thanks, this appears responsive. I really appreciate the help!</description>
      <pubDate>Thu, 06 Feb 2020 18:49:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-grouper-column-from-multiple-columns-diagonal-records/m-p/622821#M183237</guid>
      <dc:creator>paulsonalec</dc:creator>
      <dc:date>2020-02-06T18:49:57Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a grouper column from multiple columns, diagonal records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-grouper-column-from-multiple-columns-diagonal-records/m-p/622864#M183246</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines delimiter=',';
input id1 $ id2 $;
datalines;
M,A
X,Y
A,B
C,D
B,C
Z,S
V,W
W,X
Y,Z
;
data want ;
 if _n_=1 then do;
  if 0 then set have; 
  dcl hash H (dataset:'have') ;
  h.definekey  ("id2") ;
  h.definedata ("id2","id1") ;
  h.definedone () ;
  dcl hash H1 (dataset:'have') ;
  h1.definekey  ("id1") ;
  h1.definedata ("id1","id2") ;
  h1.definedone () ;
 end;
 set have( rename=(id1=v1 id2=v2)) ;
 array t(999) $ _temporary_;
 if v1 not in t and v2 not in t;
 grp+1;
 id1=v1;
 id2=v2;
 output;
 do while(h1.find(key:v2)=0);
  output;
  if v2 not in t then do;
   _iorc_+1;
   t(_iorc_)=v2;
  end;
  v2=id2;
 end;
 do while(h.find(key:v1)=0);
  output;
  if v1 not in t then do;
   _iorc_+1;
   t(_iorc_)=v1;
  end;
  v1=id1;
 end;
 drop v:;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 06 Feb 2020 20:46:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-grouper-column-from-multiple-columns-diagonal-records/m-p/622864#M183246</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-02-06T20:46:39Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a grouper column from multiple columns, diagonal records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-grouper-column-from-multiple-columns-diagonal-records/m-p/622867#M183248</link>
      <description>&lt;P&gt;This is unbelievable, thank you!&lt;/P&gt;</description>
      <pubDate>Thu, 06 Feb 2020 20:52:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-grouper-column-from-multiple-columns-diagonal-records/m-p/622867#M183248</guid>
      <dc:creator>paulsonalec</dc:creator>
      <dc:date>2020-02-06T20:52:54Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a grouper column from multiple columns, diagonal records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-grouper-column-from-multiple-columns-diagonal-records/m-p/622940#M183289</link>
      <description>&lt;P&gt;It is one to one match or one to many match ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
infile cards ;
input from $  to $ ;
cards;
1     2
1     3
4     5
5     2
9     4
6     7
8     7
;
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;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 07 Feb 2020 04:47:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-grouper-column-from-multiple-columns-diagonal-records/m-p/622940#M183289</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-02-07T04:47:43Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a grouper column from multiple columns, diagonal records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-grouper-column-from-multiple-columns-diagonal-records/m-p/622947#M183293</link>
      <description>&lt;P&gt;Not much of an OR person myself, but if you have SAS/OR, you can do this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines delimiter=',';
input id1 $ id2 $;
datalines;
M,A
A,B
C,D
B,C
V,W
W,X
X,Y
Y,Z
;

proc optnet data_links = have(rename=(id1=from id2=to)) 
            out_nodes  = temp(rename=concomp=group);
   concomp;
run;

proc sql;
   create table want as
   select have.*, temp.group
   from have, temp
   where have.id1 = temp.node;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Feb 2020 08:26:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-grouper-column-from-multiple-columns-diagonal-records/m-p/622947#M183293</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-02-07T08:26:47Z</dc:date>
    </item>
    <item>
      <title>Re: Creating a grouper column from multiple columns, diagonal records</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-a-grouper-column-from-multiple-columns-diagonal-records/m-p/623028#M183325</link>
      <description>&lt;P&gt;Very good curiosity and thinking&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Feb 2020 12:38:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-a-grouper-column-from-multiple-columns-diagonal-records/m-p/623028#M183325</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-02-07T12:38:16Z</dc:date>
    </item>
  </channel>
</rss>

