<?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 to replace repeating elements in a string within SAS? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-replace-repeating-elements-in-a-string-within-SAS/m-p/770158#M244304</link>
    <description>&lt;P&gt;Something like below should allow for some variation in your data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA have;
	INPUT name :$100.;
	DATALINES;
AAA-AAA-A
AAA-AAA-A-AA-AAA
AAA-BBB-B
AAA-AAA
AAA-BBB-AAA-AAA-CCC-BBB
AAA--AAA-AAA-CCC-BBB

;

data want(drop=_:);
  if _n_=1 then 
    do;
      length _term $40;
      dcl hash h1(hashexp:3);
      h1.defineKey('_term');
      h1.defineDone();
    end;

  set have;
  if 0 then name2=name;
  _stop=countw(name,'-');
  do _i=1 to _stop;
    _term=scan(name,_i,'-');
    if h1.check() ne 0 then 
      do;
        name2=catx('-',name2,_term);
        h1.ref();
      end;
  end;
  h1.clear();
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1632473116057.png" style="width: 461px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/64029i779F28888EEAE419/image-dimensions/461x274?v=v2" width="461" height="274" role="button" title="Patrick_0-1632473116057.png" alt="Patrick_0-1632473116057.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 24 Sep 2021 08:45:30 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2021-09-24T08:45:30Z</dc:date>
    <item>
      <title>How to replace repeating elements in a string within SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-replace-repeating-elements-in-a-string-within-SAS/m-p/770022#M244226</link>
      <description>&lt;P&gt;I have a dataset with a variable name.&lt;/P&gt;&lt;P&gt;I would like to replace the repeating values that are concatenated twice or more with a '-' as the delimiter.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;An example dataset below, shows that the number of elements concatenated change depending on the row. Also, not all of them have two repeating elements just the first row where "AAA" is repeated twice &amp;amp; the last row where "AAA" is repeated twice.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Dataset:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA test;
	INPUT name $;
	DATALINES;
AAA-AAA-A
AAA-BBB-B
AAA-AAA
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Desirable Output Table:&lt;/P&gt;&lt;P&gt;AAA-A&lt;/P&gt;&lt;P&gt;AAA-BBB-B&lt;/P&gt;&lt;P&gt;AAA&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a way to do this in SAS?&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; I started with a do loop &amp;amp; the scan() function, but I wasn't sure how to determine the number of elements to loop to. The first two row has three strings concatenated but the last one has two strings concatenated.&lt;/P&gt;</description>
      <pubDate>Thu, 23 Sep 2021 19:38:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-replace-repeating-elements-in-a-string-within-SAS/m-p/770022#M244226</guid>
      <dc:creator>narnia649</dc:creator>
      <dc:date>2021-09-23T19:38:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to replace repeating elements in a string within SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-replace-repeating-elements-in-a-string-within-SAS/m-p/770143#M244293</link>
      <description>&lt;P&gt;Try this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA test;
   INPUT name :$10.;
   DATALINES;
AAA-AAA-A
AAA-BBB-B
AAA-AAA
;
RUN;

data want(keep = newname);
   set test;
   newname=scan(name, 1, '-');
   do i=2 to countw(name,'-');
      word=scan(name, i, '-');
      found=findw(newname, word, 'it');
      if found=0 then newname=catx('-', newname, word);
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 24 Sep 2021 08:09:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-replace-repeating-elements-in-a-string-within-SAS/m-p/770143#M244293</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-09-24T08:09:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to replace repeating elements in a string within SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-replace-repeating-elements-in-a-string-within-SAS/m-p/770158#M244304</link>
      <description>&lt;P&gt;Something like below should allow for some variation in your data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA have;
	INPUT name :$100.;
	DATALINES;
AAA-AAA-A
AAA-AAA-A-AA-AAA
AAA-BBB-B
AAA-AAA
AAA-BBB-AAA-AAA-CCC-BBB
AAA--AAA-AAA-CCC-BBB

;

data want(drop=_:);
  if _n_=1 then 
    do;
      length _term $40;
      dcl hash h1(hashexp:3);
      h1.defineKey('_term');
      h1.defineDone();
    end;

  set have;
  if 0 then name2=name;
  _stop=countw(name,'-');
  do _i=1 to _stop;
    _term=scan(name,_i,'-');
    if h1.check() ne 0 then 
      do;
        name2=catx('-',name2,_term);
        h1.ref();
      end;
  end;
  h1.clear();
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1632473116057.png" style="width: 461px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/64029i779F28888EEAE419/image-dimensions/461x274?v=v2" width="461" height="274" role="button" title="Patrick_0-1632473116057.png" alt="Patrick_0-1632473116057.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Sep 2021 08:45:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-replace-repeating-elements-in-a-string-within-SAS/m-p/770158#M244304</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2021-09-24T08:45:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to replace repeating elements in a string within SAS?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-replace-repeating-elements-in-a-string-within-SAS/m-p/770351#M244396</link>
      <description>&lt;P&gt;Nice algorithm,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;Obviously you meant the FINDW argument &lt;FONT face="courier new,courier"&gt;'it'&lt;/FONT&gt; as &lt;EM&gt;modifiers&lt;/EM&gt;&amp;nbsp;-- as in&lt;/FONT&gt;&lt;/P&gt;
&lt;PRE&gt;found=findw(newname, word, &lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;'-'&lt;/FONT&gt;&lt;/STRONG&gt;, 'it');&lt;/PRE&gt;
&lt;P&gt;&lt;FONT size="2"&gt;--, not as &lt;EM&gt;delimiters&lt;/EM&gt; (which they would be in the third argument).&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Sep 2021 20:21:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-replace-repeating-elements-in-a-string-within-SAS/m-p/770351#M244396</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-09-24T20:21:18Z</dc:date>
    </item>
  </channel>
</rss>

