<?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: Search and Replace Special Characters in String in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Search-and-Replace-Special-Characters-in-String/m-p/726898#M225950</link>
    <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/368913"&gt;@larsc&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think the &lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=lefunctionsref&amp;amp;docsetTarget=p05ww22zp7lcg3n1bjk7v93tscyo.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;TRANSLATE function&lt;/A&gt; is a good candidate:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let chars_to_replace = '-,+*!.=^!';
%let char_to_replace_with = '|';

data have;
input old_string $20.;
cards;
a**2-2a*b+b**2
3.142^=2.718!
;

data want;
set have;
new_string = translate(old_string, repeat(&amp;amp;char_to_replace_with,%length(&amp;amp;chars_to_replace)-3), &amp;amp;chars_to_replace);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 16 Mar 2021 20:28:11 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2021-03-16T20:28:11Z</dc:date>
    <item>
      <title>Search and Replace Special Characters in String</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Search-and-Replace-Special-Characters-in-String/m-p/726883#M225942</link>
      <description>&lt;P&gt;Dear community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Fairly new to SAS and am currently stuck on something which there is hopefully a simple solution for - a solution which I have not been able to find thus far unfortunately.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My issue is that I want to search for a defined list of special characters (say "-,.|''_!") and replace any occurence of these with another character (say "|").&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For instance, for the string "My, name - is. Lars!" I would like to produce the string "My|name|is|Lars|" (silly example but hopefully it illustrates my desired output) if I specified the characters '-,.!' to be replaced and '|' to be the replacement. In pseudocode it would likely look something like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let chars_to_replace = '-,+*!.=^!';  *Gives a list of special characters to search for and replace
%let char_to_replace_with = '|';      *Gives the character I want to replace with

data &amp;amp;_output;
      set &amp;amp;_input;
      new_string = magical_function(old_string, &amp;amp;chars_to_replace, &amp;amp;char_to_replace_with);
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;As of yet, I have not been able to find the correct function to perform this operation, but I am hopeful such a function exists within SAS.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Mar 2021 19:37:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Search-and-Replace-Special-Characters-in-String/m-p/726883#M225942</guid>
      <dc:creator>larsc</dc:creator>
      <dc:date>2021-03-16T19:37:08Z</dc:date>
    </item>
    <item>
      <title>Re: Search and Replace Special Characters in String</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Search-and-Replace-Special-Characters-in-String/m-p/726898#M225950</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/368913"&gt;@larsc&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think the &lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=lefunctionsref&amp;amp;docsetTarget=p05ww22zp7lcg3n1bjk7v93tscyo.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;TRANSLATE function&lt;/A&gt; is a good candidate:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let chars_to_replace = '-,+*!.=^!';
%let char_to_replace_with = '|';

data have;
input old_string $20.;
cards;
a**2-2a*b+b**2
3.142^=2.718!
;

data want;
set have;
new_string = translate(old_string, repeat(&amp;amp;char_to_replace_with,%length(&amp;amp;chars_to_replace)-3), &amp;amp;chars_to_replace);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 16 Mar 2021 20:28:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Search-and-Replace-Special-Characters-in-String/m-p/726898#M225950</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-03-16T20:28:11Z</dc:date>
    </item>
    <item>
      <title>Re: Search and Replace Special Characters in String</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Search-and-Replace-Special-Characters-in-String/m-p/726900#M225952</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/368913"&gt;@larsc&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Dear community,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Fairly new to SAS and am currently stuck on something which there is hopefully a simple solution for - a solution which I have not been able to find thus far unfortunately.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My issue is that I want to search for a defined list of special characters (say "-,.|''_!") and replace any occurence of these with another character (say "|").&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For instance, for the string "My, name - is. Lars!" I would like to produce the string "My|name|is|Lars|" (silly example but hopefully it illustrates my desired output) if I specified the characters '-,.!' to be replaced and '|' to be the replacement. In pseudocode it would likely look something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let chars_to_replace = '-,+*!.=^!';  *Gives a list of special characters to search for and replace
%let char_to_replace_with = '|';      *Gives the character I want to replace with

data &amp;amp;_output;
      set &amp;amp;_input;
      new_string = magical_function(old_string, &amp;amp;chars_to_replace, &amp;amp;char_to_replace_with);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As of yet, I have not been able to find the correct function to perform this operation, but I am hopeful such a function exists within SAS.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;One way you may be looking for is function TRANSLATE. You provide two lists of "replacement" and "find" characters. The results are processed in pairs, so each item in the "find" list needs a corresponding value in the replace. Please pay attention to the order of the lists&lt;/P&gt;
&lt;PRE&gt;data example;
   string =  "My, name - is. Lars!";
   y=translate(string,"||||||||", "-,.|''_!");
run;&lt;/PRE&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;However&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT color="#000000"&gt;, the replace example you show includes replacing multiple characters, a comma and space, space dash space, period and space with a single character. So you may not have stated your requirement clearly.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;The Compress function could be used on the translated string to remove spaces if that is a separate requirement.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;Really big caution: placing random characters into Macro variables may be an issue because of the uses of % and &amp;amp; characters by the macro language&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Mar 2021 20:29:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Search-and-Replace-Special-Characters-in-String/m-p/726900#M225952</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-03-16T20:29:11Z</dc:date>
    </item>
    <item>
      <title>Re: Search and Replace Special Characters in String</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Search-and-Replace-Special-Characters-in-String/m-p/726901#M225953</link>
      <description>&lt;P&gt;This is a great page to bookmark. &lt;BR /&gt;&lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=lefunctionsref&amp;amp;docsetTarget=n01f5qrjoh9h4hn1olbdpb5pr2td.htm&amp;amp;locale=en" target="_blank" rel="noopener"&gt;https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.5&amp;amp;docsetId=lefunctionsref&amp;amp;docsetTarget=n01f5qrjoh9h4hn1olbdpb5pr2td.htm&amp;amp;locale=en&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;Then you can search for "replace" and it'll find 11 functions, three of which can be used to solve your problem.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Mar 2021 20:44:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Search-and-Replace-Special-Characters-in-String/m-p/726901#M225953</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-03-16T20:44:29Z</dc:date>
    </item>
    <item>
      <title>Re: Search and Replace Special Characters in String</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Search-and-Replace-Special-Characters-in-String/m-p/726915#M225963</link>
      <description>&lt;P&gt;Great tip, added to my SAS bookmarks now &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Mar 2021 21:07:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Search-and-Replace-Special-Characters-in-String/m-p/726915#M225963</guid>
      <dc:creator>larsc</dc:creator>
      <dc:date>2021-03-16T21:07:52Z</dc:date>
    </item>
    <item>
      <title>Re: Search and Replace Special Characters in String</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Search-and-Replace-Special-Characters-in-String/m-p/726920#M225966</link>
      <description>&lt;P&gt;For some more magic, use prxChange&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let chars_to_replace = -,+*.=^!;
%let char_to_replace_with = |;

data have;
input old_string $20.;
cards;
a**2-2a*b+b**2
3.142^=2.718!
;
data want;
set have;
new_string = prxChange("s/[&amp;amp;chars_to_replace.]/&amp;amp;char_to_replace_with./o", -1, old_string);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="PGStats_0-1615930360764.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/56034i3130082CEBE48237/image-size/medium?v=v2&amp;amp;px=400" role="button" title="PGStats_0-1615930360764.png" alt="PGStats_0-1615930360764.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To replace multiple consecutive special characters with a single character, it's simple :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let chars_to_replace = -,+*. =^!;
%let char_to_replace_with = |;

data have;
input old_string $20.;
cards;
a**2 - 2a*b + b**2
3.142^=2.718!
;
data want;
set have;
new_string = prxChange("s/[&amp;amp;chars_to_replace.]+/&amp;amp;char_to_replace_with./o", -1, old_string);
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="PGStats_0-1615931032980.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/56035iEFC38901ED5BF1D6/image-size/medium?v=v2&amp;amp;px=400" role="button" title="PGStats_0-1615931032980.png" alt="PGStats_0-1615931032980.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Mar 2021 21:44:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Search-and-Replace-Special-Characters-in-String/m-p/726920#M225966</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2021-03-16T21:44:27Z</dc:date>
    </item>
  </channel>
</rss>

