<?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 can I create a variable indicating which values are the same? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-create-a-variable-indicating-which-values-are-the-same/m-p/933447#M367121</link>
    <description>&lt;P&gt;Steps to follow:&lt;/P&gt;
&lt;P&gt;Create a variable to uniquely identify the observations (e.g. derived from _N_).&lt;/P&gt;
&lt;P&gt;Transpose the ID: variables, use the above variable as BY.&lt;/P&gt;
&lt;P&gt;Sort by this unique id, COL1 and _NAME_;&lt;/P&gt;
&lt;P&gt;Then&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set long;
by unique_id col1;
length match $10;
retain match count;
if first.col1
then do;
  match = scan(_name_,2,"_");
  count = 1;
end;
else do;
  match = catx(",",match,scan(_name_,2,"_"));
  count + 1;
end;
if last.col1 and count &amp;gt; 1;
drop count _name_ /* col1 */;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sun, 23 Jun 2024 08:10:44 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2024-06-23T08:10:44Z</dc:date>
    <item>
      <title>How can I create a variable indicating which values are the same?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-create-a-variable-indicating-which-values-are-the-same/m-p/933438#M367115</link>
      <description>&lt;P&gt;Is there an easy way to create a variable called match which will list all the IDs that are the same? The data is in the format below. Any help would be greatly appreciated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ID_A&amp;nbsp; &amp;nbsp; &amp;nbsp; ID_B&amp;nbsp; &amp;nbsp; &amp;nbsp;ID_C&amp;nbsp; &amp;nbsp; &amp;nbsp;ID_D&amp;nbsp; &amp;nbsp; &amp;nbsp; MATCH&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; A,B,C,D&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;A,B,C&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; B,C,D&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;A.B&lt;/P&gt;</description>
      <pubDate>Sun, 23 Jun 2024 03:21:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-create-a-variable-indicating-which-values-are-the-same/m-p/933438#M367115</guid>
      <dc:creator>kyle234</dc:creator>
      <dc:date>2024-06-23T03:21:15Z</dc:date>
    </item>
    <item>
      <title>Re: How can I create a variable indicating which values are the same?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-create-a-variable-indicating-which-values-are-the-same/m-p/933442#M367118</link>
      <description>&lt;P&gt;What if you have a combination of&lt;/P&gt;
&lt;PRE&gt;1   1   3   3&lt;/PRE&gt;
&lt;P&gt;?&lt;/P&gt;</description>
      <pubDate>Sun, 23 Jun 2024 07:04:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-create-a-variable-indicating-which-values-are-the-same/m-p/933442#M367118</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-06-23T07:04:37Z</dc:date>
    </item>
    <item>
      <title>Re: How can I create a variable indicating which values are the same?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-create-a-variable-indicating-which-values-are-the-same/m-p/933447#M367121</link>
      <description>&lt;P&gt;Steps to follow:&lt;/P&gt;
&lt;P&gt;Create a variable to uniquely identify the observations (e.g. derived from _N_).&lt;/P&gt;
&lt;P&gt;Transpose the ID: variables, use the above variable as BY.&lt;/P&gt;
&lt;P&gt;Sort by this unique id, COL1 and _NAME_;&lt;/P&gt;
&lt;P&gt;Then&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set long;
by unique_id col1;
length match $10;
retain match count;
if first.col1
then do;
  match = scan(_name_,2,"_");
  count = 1;
end;
else do;
  match = catx(",",match,scan(_name_,2,"_"));
  count + 1;
end;
if last.col1 and count &amp;gt; 1;
drop count _name_ /* col1 */;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 23 Jun 2024 08:10:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-create-a-variable-indicating-which-values-are-the-same/m-p/933447#M367121</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2024-06-23T08:10:44Z</dc:date>
    </item>
    <item>
      <title>Re: How can I create a variable indicating which values are the same?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-create-a-variable-indicating-which-values-are-the-same/m-p/933513#M367139</link>
      <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data SAMPLE;
   A=1; B=1; C=1; D=3; output;
   A=1; B=2; C=2; D=2; output;
   A=1; B=2; C=3; D=4; output;
run;

data WANT;
  set SAMPLE;
  _POS = prxmatch('/(.)\1/', cats(A,B,C,D));          * Find the position of a repeated value;
  if _POS then do;                                    * If found then analyse;
    _CHAR = char(cats(A,B,C,D), _POS);                * Extract repeated value;
    MATCH = catx(',', ifc(put(A,1.)=_CHAR,'A','')
                    , ifc(put(B,1.)=_CHAR,'B','')
                    , ifc(put(C,1.)=_CHAR,'C','')
                    , ifc(put(D,1.)=_CHAR,'D','')
                );                                    * Extract occurrences;
  end; 
  drop _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This creates this table:&lt;/P&gt;
&lt;TABLE class="table" style="border-spacing: 0;" aria-label="Data Set WORK.WANT"&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="r header" scope="col"&gt;A&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;B&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;C&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;D&lt;/TH&gt;
&lt;TH class="header" scope="col"&gt;MATCH&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;3&lt;/TD&gt;
&lt;TD class="data"&gt;A,B,C&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="data"&gt;B,C,D&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;3&lt;/TD&gt;
&lt;TD class="r data"&gt;4&lt;/TD&gt;
&lt;TD class="data"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jun 2024 04:47:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-create-a-variable-indicating-which-values-are-the-same/m-p/933513#M367139</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2024-06-24T04:47:37Z</dc:date>
    </item>
    <item>
      <title>Re: How can I create a variable indicating which values are the same?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-create-a-variable-indicating-which-values-are-the-same/m-p/933514#M367140</link>
      <description>&lt;P&gt;If the values are longer than a digit, or not contiguous:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data SAMPLE;
   A=11; B=11; C=11; D= 3; output;
   A= 1; B=22; C= 4; D=22; output;
   A= 1; B= 2; C= 3; D= 4; output;
run;

data WANT;
  set SAMPLE;   
  call sort (of A--D); 
  _STR   = catx('|', of A--D, .);
  _VALUE = prxchange('s/.*((\d+)\|)\1.*/\2/',1, _STR);
  set SAMPLE;   
  if _VALUE ne _STR then do;
    MATCH = catx(',', ifc(put(A,8. -l)=_VALUE, 'A', '')
                    , ifc(put(B,8. -l)=_VALUE, 'B', '')
                    , ifc(put(C,8. -l)=_VALUE, 'C', '')
                    , ifc(put(D,8. -l)=_VALUE, 'D', '')
                );
  end; 
  drop _: ;
run; 
   &lt;/CODE&gt;&lt;/PRE&gt;
&lt;TABLE class="table" style="border-spacing: 0;" aria-label="Data Set WORK.WANT"&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="r header" scope="col"&gt;A&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;B&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;C&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;D&lt;/TH&gt;
&lt;TH class="header" scope="col"&gt;MATCH&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;11&lt;/TD&gt;
&lt;TD class="r data"&gt;11&lt;/TD&gt;
&lt;TD class="r data"&gt;11&lt;/TD&gt;
&lt;TD class="r data"&gt;3&lt;/TD&gt;
&lt;TD class="data"&gt;A,B,C&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;22&lt;/TD&gt;
&lt;TD class="r data"&gt;4&lt;/TD&gt;
&lt;TD class="r data"&gt;22&lt;/TD&gt;
&lt;TD class="data"&gt;B,D&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="r data"&gt;3&lt;/TD&gt;
&lt;TD class="r data"&gt;4&lt;/TD&gt;
&lt;TD class="data"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The regular expression&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;.*((\d+)\|)\1.*/\2/&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;means:&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000FF"&gt;&lt;STRONG&gt;/&lt;/STRONG&gt; &lt;/FONT&gt;start match pattern&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000FF"&gt;&lt;STRONG&gt;.*&lt;/STRONG&gt; &lt;/FONT&gt;any character(s), then&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000FF"&gt;&lt;STRONG&gt;(&amp;nbsp;&lt;/STRONG&gt;&lt;/FONT&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; start a group, then&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;FONT color="#0000FF"&gt;&lt;STRONG&gt; (\d+)&amp;nbsp;&lt;/STRONG&gt;&lt;/FONT&gt; another ,group with one digit or more, then&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000FF"&gt;&lt;STRONG&gt;&amp;nbsp; \|&amp;nbsp;&lt;/STRONG&gt;&lt;/FONT&gt; a pipe character, then&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;FONT color="#0000FF"&gt;)&lt;/FONT&gt;&lt;/STRONG&gt; close group, then&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;FONT color="#0000FF"&gt;/1&amp;nbsp;&lt;/FONT&gt; &lt;/STRONG&gt;repeat the group just matched (so we want the same value twice), then&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000FF"&gt;&lt;STRONG&gt;.*&lt;/STRONG&gt;&lt;/FONT&gt; any character(s)&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000FF"&gt;&lt;STRONG&gt;/&lt;/STRONG&gt;&lt;/FONT&gt; end match pattern, start replace pattern&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000FF"&gt;&lt;STRONG&gt;\1&amp;nbsp;&lt;/STRONG&gt;&lt;/FONT&gt; replace matched characters with 2nd (inner) group: (\d+)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;FONT color="#0000FF"&gt;&lt;STRONG&gt;/&lt;/STRONG&gt;&lt;/FONT&gt; end of replace pattern&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can shorten to something more compact if you want:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WANT;
  set SAMPLE;   
  call sort (of A--D); 
  _VALUE = prxchange('s/.*((\d+)\|)\1.*/\2/',1, catx('|', of A--D, .));
  set SAMPLE;   
  MATCH = catx(',', ifc(put(A, 8. -l)=_VALUE, 'A', '')
                  , ifc(put(B, 8. -l)=_VALUE, 'B', '')
	              , ifc(put(C, 8. -l)=_VALUE, 'C', '')
	              , ifc(put(D, 8. -l)=_VALUE, 'D', '')
	           );
  drop _: ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data WANT;
  set SAMPLE;   
  call sort(of A--D); 
  _VALUE = input(prxchange('s/.*((\d+)\|)\1.*/\2/', 1, catx('|', of A--D, .)), ?? 8.);
  set SAMPLE;   
  MATCH = catx(',', ifc(A=_VALUE, 'A', '')
                  , ifc(B=_VALUE, 'B', '')
                  , ifc(C=_VALUE, 'C', '')
                  , ifc(D=_VALUE, 'D', '')
              );
   drop _: ;
run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jun 2024 11:19:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-create-a-variable-indicating-which-values-are-the-same/m-p/933514#M367140</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2024-06-24T11:19:05Z</dc:date>
    </item>
    <item>
      <title>Re: How can I create a variable indicating which values are the same?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-create-a-variable-indicating-which-values-are-the-same/m-p/933650#M367194</link>
      <description>&lt;P&gt;Assuming data like this (I added the ROW_ID variable to identify rows):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  row_id=_N_;
  input ID_A ID_B ID_C ID_D;
cards;
1 1 1 1
2 2 2 3
3 4 4 4
1 1 3 5
2 2 6 6
1 2 3 4
;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I would first convert to a long format and sort:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=have out=long;
  var ID_:;
  by row_id;
run;

proc sort data=long;
  by row_id col1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then it is easy to find the matches:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data match;
  do until(last.col1);
    set long;
    by row_id col1;
    length match $10;
    call catx(',',match,substr(_name_,4));
    end;
  if index(match,','); /* not single values */
  keep row_id match;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And finally merge with the original data:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  merge have match;
  by row_id;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The answer to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;'s question is in this case that if there are two matching pairs, we will create an output row for each.&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jun 2024 07:57:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-create-a-variable-indicating-which-values-are-the-same/m-p/933650#M367194</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2024-06-25T07:57:22Z</dc:date>
    </item>
  </channel>
</rss>

