<?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: Specifying the observation as in same group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Specifying-the-observation-as-in-same-group/m-p/647739#M193886</link>
    <description>&lt;P&gt;Hello again&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292097"&gt;@ed_sas_member&lt;/a&gt;&amp;nbsp;,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your both solutions seems worked fine. I am gonna&amp;nbsp; see the codes in detail. If anythings come up new, I'll let you now.&lt;/P&gt;&lt;P&gt;Thank you for your time and contributions.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best,&lt;/P&gt;&lt;P&gt;Uğur,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 14 May 2020 10:16:14 GMT</pubDate>
    <dc:creator>uguraltuntas67</dc:creator>
    <dc:date>2020-05-14T10:16:14Z</dc:date>
    <item>
      <title>Specifying the observation as in same group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Specifying-the-observation-as-in-same-group/m-p/647714#M193874</link>
      <description>&lt;P&gt;I have a table consisting of two columns (X,Y) that represent correlations between observations like below.&lt;/P&gt;&lt;P&gt;X Y&lt;BR /&gt;1 2&lt;BR /&gt;2 3&lt;BR /&gt;3 4&lt;BR /&gt;A B&lt;BR /&gt;B C&lt;/P&gt;&lt;P&gt;I want a create new column that represent the relation between observation. 1 become 2, 2 become 3, 3 become 4. So i wanna show this variables in same group(1,2,3,4 are belong to same group). The table should be like below.&lt;/P&gt;&lt;P&gt;X Y Z&lt;BR /&gt;1 2 Group 1&lt;BR /&gt;2 3 Group 1&lt;BR /&gt;3 4 Group 1&lt;BR /&gt;A B Group 2&lt;BR /&gt;B C Group 2&lt;/P&gt;&lt;P&gt;I can not sort the variables because observation dates random. I am using SAS Enterprise Guide. I need the logic or sas code.&lt;/P&gt;&lt;P&gt;Note: I have no additional information except this table.&lt;/P&gt;&lt;P&gt;If there is no clear information please notify me.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help would be appreciated,&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;</description>
      <pubDate>Thu, 14 May 2020 07:31:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Specifying-the-observation-as-in-same-group/m-p/647714#M193874</guid>
      <dc:creator>uguraltuntas67</dc:creator>
      <dc:date>2020-05-14T07:31:14Z</dc:date>
    </item>
    <item>
      <title>Re: Specifying the observation as in same group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Specifying-the-observation-as-in-same-group/m-p/647716#M193876</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/262956"&gt;@uguraltuntas67&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you mean that if X is equal to the previous value of Y, they belong to the same group?&lt;/P&gt;
&lt;P&gt;If so, please try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set have;
	_lag = lag(Y);
	if x ne _lag then group+1;
	drop _lag;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Best,&lt;/P&gt;</description>
      <pubDate>Thu, 14 May 2020 07:38:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Specifying-the-observation-as-in-same-group/m-p/647716#M193876</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-05-14T07:38:11Z</dc:date>
    </item>
    <item>
      <title>Re: Specifying the observation as in same group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Specifying-the-observation-as-in-same-group/m-p/647717#M193877</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292097"&gt;@ed_sas_member&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for quick response. But my data is not sorted. Sorry for my misleading. My data look like as below.&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;X Y&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;1 2&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;A B&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;3 4&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;B C&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2 3&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;And i wanna do this;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;X Y Z&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;1 2 Group 1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;A B Group 2&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;3 4 Group 1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2 3 Group 1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;B C Group 2&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Best,&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 14 May 2020 08:02:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Specifying-the-observation-as-in-same-group/m-p/647717#M193877</guid>
      <dc:creator>uguraltuntas67</dc:creator>
      <dc:date>2020-05-14T08:02:25Z</dc:date>
    </item>
    <item>
      <title>Re: Specifying the observation as in same group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Specifying-the-observation-as-in-same-group/m-p/647722#M193879</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/262956"&gt;@uguraltuntas67&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sorry for the confusion&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":smiling_face_with_smiling_eyes:"&gt;😊&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Here is a similar thread :&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Programming/Creating-a-grouper-column-from-multiple-columns-diagonal-records/m-p/622789#M183227" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/SAS-Programming/Creating-a-grouper-column-from-multiple-columns-diagonal-records/m-p/622789#M183227&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;I think that&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;'s answer will correspond to what you're looking for:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture d’écran 2020-05-14 à 10.24.30.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/39425i0FC1211BE8BE6987/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture d’écran 2020-05-14 à 10.24.30.png" alt="Capture d’écran 2020-05-14 à 10.24.30.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;As mentioned in his post, here is the link to this great hash solution:&amp;nbsp;&lt;A href="https://gist.github.com/statgeek/14e3aa2a9f718f551cd98134e9ceed30" target="_blank" rel="noopener"&gt;https://gist.github.com/statgeek/14e3aa2a9f718f551cd98134e9ceed30&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Wish I could be able to write such a program&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":frowning_face:"&gt;☹️&lt;/span&gt;.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Anyway, I have tried to adapt it to your case:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input from $ to $;
	datalines;
1 2
A B
3 4
B C
2 3
4 5
7 8
;
run;

%macro SubGraphs(arcs,from=from,to=to,out=Clusters,exp=8);
data _null_;
if 0 then set &amp;amp;arcs(keep=&amp;amp;from rename=(&amp;amp;from=node)); /* get node data type */
length clust 8;
declare hash nodes(hashexp:&amp;amp;exp);
nodes.defineKey('node');
nodes.defineData('node', 'clust');
nodes.defineDone();
declare hiter nodeList('nodes');

do newClust = 1 by 1 while(not endLoop);
	set &amp;amp;arcs end=endLoop;
	call missing(clust); node = &amp;amp;from;
	if 0^=nodes.find() then nodes.add(); 
	fromClust = clust; 
	call missing(clust); node = &amp;amp;to;
	if 0^=nodes.find() then nodes.add(); 
	toClust = clust;
	if n(fromClust, toClust) = 0 then do;
		nodes.replace(key:&amp;amp;from, data:&amp;amp;from, data:newClust);
		nodes.replace(key:&amp;amp;to, data:&amp;amp;to, data:newClust);
	end;
	else if missing(toClust) then 
		nodes.replace(key:&amp;amp;to, data:&amp;amp;to, data:fromClust);
	else if missing(fromClust) then 
		nodes.replace(key:&amp;amp;from, data:&amp;amp;from, data:toClust);
	else if fromClust ne toClust then do;
		rc = nodeList.first();
		do while (rc = 0);
			if clust = fromClust then 
				nodes.replace(key:node, data:node, data:toClust);
			rc = nodeList.next();
		end;
	end;
end;
nodes.output(dataset:"&amp;amp;out");
stop;
run;
%mend SubGraphs;


options mprint symbolgen;
%SubGraphs(have,out=want,exp=1);

proc sql;
	create table ref as
	select clust as _group, node as x
	from want;
quit;

proc sql;
	create table have2 as
	select a.*, b._group
	from have as a left join ref as b
	on a.from = b.x
	order by _group;
run;

data want;
	set have2;
	by _group;
	if first._group then group+1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the same thread, you can also have a look at&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;'s solution&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this helps,&lt;/P&gt;
&lt;P&gt;Best,&lt;/P&gt;</description>
      <pubDate>Thu, 14 May 2020 08:43:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Specifying-the-observation-as-in-same-group/m-p/647722#M193879</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-05-14T08:43:56Z</dc:date>
    </item>
    <item>
      <title>Re: Specifying the observation as in same group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Specifying-the-observation-as-in-same-group/m-p/647733#M193884</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if we can assume that&amp;nbsp;groups are linear (one parent one child, i.e 1-&amp;gt;2 2-&amp;gt;3 but not 1-&amp;gt;2 1-&amp;gt;3) then following mix of formats and arrays may help.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input X $ Y $;
cards;
1 2
2 3
A B
3 4
B C
;
run;


/* first convert your data to integers */
data first(keep = start);
  set have;
  start = x;
  output;
  start = y;
  output;
run;

proc sort data = first out = second nodupkeys;
  by start;
run;

data third;
  retain fmtname 'toInteger' type 'i'; 
  set second curobs = curobs;
  label = curobs;
run;

proc format library=work cntlin=third;
run;


/* use modified input data to work on array */
data have1;
 set have nobs=nobs end = EOF;
 x1 = input(x, toInteger.);
 y1 = input(y, toInteger.);
 keep x1 y1;
 output;
 if EOF then call symputX("N", 2*nobs);
run;


data fourth;
  array rel[&amp;amp;N.] _temporary_;
  retain fmtname 'toGroup' type 'i';

  /* populate array */
  do until(eof);
    set have1 end=EOF;
    rel[x1] = y1;
  end;

  /* traverse array to find "lists" of related pairs */
  LABEL = 0;
  do _N_ = 1 to dim(rel);
    if rel[_N_] &amp;gt; .z then
      do;
        _I_ = _N_;
        LABEL + 1;
        do while(rel[_I_] &amp;gt; .z);
          START = _I_;
          output;
          _T_ = _I_;
          _I_ = rel[_I_];
          rel[_T_] = .; 
        end;
      end;
  end;
  keep fmtname type LABEL START;
run;

proc format library=work cntlin=fourth;
run;

/* assign groups */
data want;
 set have nobs=nobs end = EOF;
 group = input(input(x, toInteger.), toGroup.);
 output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Thu, 14 May 2020 09:45:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Specifying-the-observation-as-in-same-group/m-p/647733#M193884</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2020-05-14T09:45:50Z</dc:date>
    </item>
    <item>
      <title>Re: Specifying the observation as in same group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Specifying-the-observation-as-in-same-group/m-p/647739#M193886</link>
      <description>&lt;P&gt;Hello again&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292097"&gt;@ed_sas_member&lt;/a&gt;&amp;nbsp;,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your both solutions seems worked fine. I am gonna&amp;nbsp; see the codes in detail. If anythings come up new, I'll let you now.&lt;/P&gt;&lt;P&gt;Thank you for your time and contributions.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best,&lt;/P&gt;&lt;P&gt;Uğur,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 14 May 2020 10:16:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Specifying-the-observation-as-in-same-group/m-p/647739#M193886</guid>
      <dc:creator>uguraltuntas67</dc:creator>
      <dc:date>2020-05-14T10:16:14Z</dc:date>
    </item>
    <item>
      <title>Re: Specifying the observation as in same group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Specifying-the-observation-as-in-same-group/m-p/647741#M193888</link>
      <description>You're welcome &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/262956"&gt;@uguraltuntas67&lt;/a&gt;!</description>
      <pubDate>Thu, 14 May 2020 10:20:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Specifying-the-observation-as-in-same-group/m-p/647741#M193888</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-05-14T10:20:11Z</dc:date>
    </item>
  </channel>
</rss>

