<?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 How to create an ID from different variables and OR condition in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-ID-from-different-variables-and-OR-condition/m-p/900795#M355993</link>
    <description>&lt;P&gt;I am trying&amp;nbsp; to recreate a new variable in a dataset to be used for group by or class in proc means, that has to be built in function of other two variable. Here an example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data test;
input rapp $ 1-13 posiz $ 15-23;
datalines;
0900028641002 000838931
0600700253462 000838937
0100701192000 000838931
0100701192000 000838930
0600700253462 000838937
;&lt;/PRE&gt;&lt;P&gt;The idea is to add a new_id in the table that has the same value every time the records have the same value for rapp OR the same value for POSIZ, obtaining a thing like this:&lt;/P&gt;&lt;PRE&gt;data test2;
input rapp $ 1-13 posiz $ 15-23 new_id $ 25-27;
datalines;
0900028641002 000838931 001
0600700253462 000838937 002
0100701192000 000838931 001
0100701192000 000838930 001
0600700253462 000838937 002
;&lt;/PRE&gt;&lt;P&gt;In this case, row3 has new_id=001 because it has the same posiz of row1. Row4 has 001 because it has the same rapp of row3. Row2 and row5 have 002 because they have both the same rapp and posiz. How could I obtain a result like this?&lt;/P&gt;</description>
    <pubDate>Mon, 30 Oct 2023 19:44:23 GMT</pubDate>
    <dc:creator>Jack90_</dc:creator>
    <dc:date>2023-10-30T19:44:23Z</dc:date>
    <item>
      <title>How to create an ID from different variables and OR condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-ID-from-different-variables-and-OR-condition/m-p/900795#M355993</link>
      <description>&lt;P&gt;I am trying&amp;nbsp; to recreate a new variable in a dataset to be used for group by or class in proc means, that has to be built in function of other two variable. Here an example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data test;
input rapp $ 1-13 posiz $ 15-23;
datalines;
0900028641002 000838931
0600700253462 000838937
0100701192000 000838931
0100701192000 000838930
0600700253462 000838937
;&lt;/PRE&gt;&lt;P&gt;The idea is to add a new_id in the table that has the same value every time the records have the same value for rapp OR the same value for POSIZ, obtaining a thing like this:&lt;/P&gt;&lt;PRE&gt;data test2;
input rapp $ 1-13 posiz $ 15-23 new_id $ 25-27;
datalines;
0900028641002 000838931 001
0600700253462 000838937 002
0100701192000 000838931 001
0100701192000 000838930 001
0600700253462 000838937 002
;&lt;/PRE&gt;&lt;P&gt;In this case, row3 has new_id=001 because it has the same posiz of row1. Row4 has 001 because it has the same rapp of row3. Row2 and row5 have 002 because they have both the same rapp and posiz. How could I obtain a result like this?&lt;/P&gt;</description>
      <pubDate>Mon, 30 Oct 2023 19:44:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-ID-from-different-variables-and-OR-condition/m-p/900795#M355993</guid>
      <dc:creator>Jack90_</dc:creator>
      <dc:date>2023-10-30T19:44:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to create an ID from different variables and OR condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-ID-from-different-variables-and-OR-condition/m-p/900834#M356008</link>
      <description>&lt;P&gt;And what NEW_ID is assigned for a record in which both the RAPP and POSIZ values have already been seen, but are associated with different values of NEW_ID?&lt;/P&gt;</description>
      <pubDate>Mon, 30 Oct 2023 22:57:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-ID-from-different-variables-and-OR-condition/m-p/900834#M356008</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-10-30T22:57:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to create an ID from different variables and OR condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-ID-from-different-variables-and-OR-condition/m-p/900836#M356010</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;I believe new_id is the desired outcome for the sample data. Looks to me somehow like the clusterid for networks.&lt;/P&gt;</description>
      <pubDate>Mon, 30 Oct 2023 23:06:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-ID-from-different-variables-and-OR-condition/m-p/900836#M356010</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-10-30T23:06:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to create an ID from different variables and OR condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-ID-from-different-variables-and-OR-condition/m-p/900837#M356011</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is the key text of the OP for me&amp;nbsp; (&lt;EM&gt;&lt;STRONG&gt;bold italics&lt;/STRONG&gt;&lt;/EM&gt; mine):&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;The idea is to add a new_id in the table that has the same value every time the records have the&lt;EM&gt;&lt;STRONG&gt; same value for rapp OR the same value for POSIZ&lt;/STRONG&gt;&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The "OR" is the problematic issue for me.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If the first two records are:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;rapp     posiz
1111     AAAA
2222     BBBB&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I presume NEW_ID gets 001 and 002 respectively.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And if a subsequent record has rapp=1111 or posiz=AAAA it would get ID=001.&amp;nbsp; Similarly subsequent rapp=2222 or posiz=BBBB records would get NEW_ID=002.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But what NEW_ID should be assigned if a record comes up with rapp=1111&amp;nbsp; and posiz=BBBB?&lt;/P&gt;</description>
      <pubDate>Mon, 30 Oct 2023 23:18:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-ID-from-different-variables-and-OR-condition/m-p/900837#M356011</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-10-30T23:18:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to create an ID from different variables and OR condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-ID-from-different-variables-and-OR-condition/m-p/900839#M356013</link>
      <description>Your example is right. But in my dataset the exception that you showed can't verify. Every POSIZ is associated only to one RAPP, so to me the logic should be based on two conditions in every record:&lt;BR /&gt;&lt;BR /&gt;- both POSIZ and RAPP of the record never appeared before---&amp;gt;new_id increase by 1&lt;BR /&gt;&lt;BR /&gt;- POSIZ or RAPP already appeared---&amp;gt;you gave the same new_id of that past record.&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Mon, 30 Oct 2023 23:59:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-ID-from-different-variables-and-OR-condition/m-p/900839#M356013</guid>
      <dc:creator>Jack90_</dc:creator>
      <dc:date>2023-10-30T23:59:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to create an ID from different variables and OR condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-ID-from-different-variables-and-OR-condition/m-p/900929#M356053</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/206116"&gt;@Jack90_&lt;/a&gt;&amp;nbsp;wrote:&amp;nbsp; (&lt;EM&gt;&lt;STRONG&gt;bold italics mine&lt;/STRONG&gt;&lt;/EM&gt;)&lt;BR /&gt;Your example is right. But in my dataset the exception that you showed can't verify.&lt;EM&gt;&lt;STRONG&gt; Every POSIZ is associated only to one RAPP&lt;/STRONG&gt;&lt;/EM&gt;, so to me the logic should be based on two conditions in every record:&lt;BR /&gt;&lt;BR /&gt;- both POSIZ and RAPP of the record never appeared before---&amp;gt;new_id increase by 1&lt;BR /&gt;&lt;BR /&gt;- POSIZ or RAPP already appeared---&amp;gt;you gave the same new_id of that past record.&lt;BR /&gt;&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I don't quite know what is meant by "exception that you showed can't verify".&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But regardless, your example data (lines 1 and 3 below) shows that "every POSIZ is associated only to one RAPP" is not true.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input rapp $ 1-13 posiz $ 15-23;
datalines;
0900028641002 000838931
0600700253462 000838937
0100701192000 000838931
0100701192000 000838930
0600700253462 000838937
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I think we need some more clarification.&lt;/P&gt;</description>
      <pubDate>Tue, 31 Oct 2023 15:40:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-ID-from-different-variables-and-OR-condition/m-p/900929#M356053</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-10-31T15:40:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to create an ID from different variables and OR condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-ID-from-different-variables-and-OR-condition/m-p/900962#M356069</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;I believe the OPs question was clear especially because the desired result had been provided.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/206116"&gt;@Jack90_&lt;/a&gt;&amp;nbsp;Can you please confirm that the following re-formulation of the problem is correct and what you need?&lt;/P&gt;
&lt;P&gt;I've taken your sample data and just renamed the variables and replaced the values 1:1 with some easier to read strings.&lt;/P&gt;
&lt;P&gt;The additional variable "person" would just be the observation in your original sample data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Problem: For below sample data create clusters of persons (same cluster_id) that share at least one of the two attributes Household and Workplace.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data desired_result;
  input person $ household $ workplace $ desired_id;
datalines;
A h01 w01 001
B h02 w02 002
C h03 w01 001
D h03 w03 001
E h02 w02 002
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For above sample data person A and person D are in the same cluster because they are connected indirectly via person C.&lt;/P&gt;
&lt;P&gt;Person A shares the workplace with person C and person D shares the household with person C.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;...and that's why I believe this problem belongs somewhere in the realm of data prep for network/link analysis.&lt;/P&gt;
&lt;P&gt;I've never written logic for clustering and I couldn't make things work (yet?) via docu for Proc Cluster and Proc Fastclus and any datastep logic I could think of so far feels way too complicated and inefficient.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 31 Oct 2023 21:18:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-ID-from-different-variables-and-OR-condition/m-p/900962#M356069</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-10-31T21:18:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to create an ID from different variables and OR condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-ID-from-different-variables-and-OR-condition/m-p/901004#M356087</link>
      <description>&lt;P&gt;Removed because I found a use case where the proposed code didn't work. I'll share working code if I find the time to/can fix it.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Nov 2023 05:29:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-ID-from-different-variables-and-OR-condition/m-p/901004#M356087</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-11-01T05:29:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to create an ID from different variables and OR condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-ID-from-different-variables-and-OR-condition/m-p/901041#M356101</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I believe this problem belongs somewhere in the realm of data prep for network/link analysis.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I agree and so I think we can use one of the clustering tools provided by others in the past to do the heavy lifting, for example, the &lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-find-all-connected-components-in-a-graph/ta-p/231539" target="_blank" rel="noopener"&gt;SubGraphs macro by PGStats&lt;/A&gt;. We just need to create the input dataset containing the information about links between different records.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Add record numbers to the data */

data have / view=have;
set test;
record=_n_;
run;

/* Create dataset describing the links between different records */

proc sql;
create table links as
select distinct a.record as from, b.record as to
from have a, have b
where (a.rapp=b.rapp | a.posiz=b.posiz) &amp;amp; a.record&amp;lt;b.record;
quit;

/* Identify connected components ("clusters") */

%SubGraphs(links)

/* Add cluster IDs to the original data */

proc sql;
create table want(drop=record) as
select have.*, clust as new_id format=z3.
from have, clusters
where record=node
order by record;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 01 Nov 2023 11:14:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-ID-from-different-variables-and-OR-condition/m-p/901041#M356101</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2023-11-01T11:14:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to create an ID from different variables and OR condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-ID-from-different-variables-and-OR-condition/m-p/901045#M356102</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input rapp $ 1-13 posiz $ 15-23;
datalines;
0900028641002 000838931
0600700253462 000838937
0100701192000 000838931
0100701192000 000838930
0600700253462 000838937
;
data have;
set test;
length from to $ 80;
from=rapp; to=posiz;
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(rename=(household=new_id));
   declare hash h(dataset:'want(rename=(household=new_id))');
   h.definekey('node');
   h.definedata('new_id');
   h.definedone();
 end;
set test;
call missing(new_id);
rc=h.find(key:rapp);
drop rc node;
format new_id z8.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 01 Nov 2023 11:37:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-ID-from-different-variables-and-OR-condition/m-p/901045#M356102</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-11-01T11:37:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to create an ID from different variables and OR condition</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-ID-from-different-variables-and-OR-condition/m-p/901066#M356105</link>
      <description>&lt;P&gt;Yes&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;&amp;nbsp;your re-formulation seems right to me. Honestly at the end I made my task grouping by another variable that could be considered a proxy of what I asked to build with this question, obtaining results that seems acceptable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Anyway I add some other details about the problem if this could useful. The dataset that I am using collects banking accounts contracts. But the same banking account could be splitted in more than one record in the dataset. Essentially for two potential reasons (both could be present, just one of them, or none):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- the banking account is splitted in more than one record because you have the secured part of the drown amount in a record, and the unsecured part in another record: the two or more records share the same ID contract (the variable RAPP that I provided previously) but they have different POSITION code (the variable POSIZ that I provided previously)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- the banking account has a credit line with an undrown exposure (the credit not used by the counterpart). This undrown part insists on the credit line ID contract, not the banking account, so the two ore more records have different ID contract (again the variable RAPP). But the undrown part is always linked to only one banking account, sharing the same POSITION ID with one of its tranche (again the variable POSIZ).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My aim would be to reaggregate all the records, to see the total undrown and drown amount overall.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Nov 2023 12:55:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-an-ID-from-different-variables-and-OR-condition/m-p/901066#M356105</guid>
      <dc:creator>Jack90_</dc:creator>
      <dc:date>2023-11-01T12:55:49Z</dc:date>
    </item>
  </channel>
</rss>

