<?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: Replacing all punctuation in a string with spaces in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Replacing-all-punctuation-in-a-string-with-spaces/m-p/356898#M274175</link>
    <description>&lt;P&gt;Replacing characters is often the purview of the TRANSLATE function. Translate is one of the functions many people use incorrectly as the search string goes after the replacement target&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt; data example;
   string='This is, a string; with. common? punction:';
   newstring = translate(string,' ',',.;:?');
run;&lt;/PRE&gt;
&lt;P&gt;So your example may well work with&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;TxtVar =&amp;nbsp;TRANSLATE(TxtVar, " ", "&amp;amp;PunctChars.");&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Including special character, especially quotes is going to be an issue in several places and without the actual entire Macro code I'm not going to make specific recommendations.&lt;/P&gt;</description>
    <pubDate>Mon, 08 May 2017 15:18:18 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2017-05-08T15:18:18Z</dc:date>
    <item>
      <title>Replacing all punctuation in a string with spaces</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-all-punctuation-in-a-string-with-spaces/m-p/356886#M274174</link>
      <description>&lt;P&gt;In the data I list below, I'd like to replace all punctuation in variable TxtVar with a space. Furthermore, I'd like to have a macro that supports a parameter which specifies what characters qualify as punctuation.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;infile datalines delimiter="\n";&lt;BR /&gt;input TxtVar $;&lt;BR /&gt;datalines;&lt;BR /&gt;This, has, commas\n&lt;BR /&gt;And. This. Has. Periods\n&lt;BR /&gt;This "is" harder\n&lt;BR /&gt;How about some question marks ??\n&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My try so far is this macro:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro ReplacePunct(datain, dataout, PunctChars);&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; data &amp;amp;&lt;SPAN&gt;dataout ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; set &amp;amp;datain ;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;TxtVar =&amp;nbsp;TRANWRD(TxtVar, "&amp;amp;PunctChars.", " ");&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;%mend;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The macro call is: %&lt;SPAN&gt;ReplacePunct(have, want, %bquote(,) );&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Obviously, this works as intended only if the PunctChars parameter only has a single character in it (in the example call above it has the comma). However, I would like to call it like this:&amp;nbsp;%ReplacePunct(have, want, %bquote(&lt;STRONG&gt;,.;_@#/\?!'"&lt;/STRONG&gt;) );&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;So I gues there are two questions:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1. How do i handle the special characters? Double quotes (") or periods (.) always&amp;nbsp;throws an error ?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2. How can I parse the contents of&amp;nbsp;PunctChars parameter one by one ? (or any other method which would lead to replacing each occurence of every character with a space)&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 08 May 2017 15:01:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-all-punctuation-in-a-string-with-spaces/m-p/356886#M274174</guid>
      <dc:creator>BogdanC</dc:creator>
      <dc:date>2017-05-08T15:01:08Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing all punctuation in a string with spaces</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-all-punctuation-in-a-string-with-spaces/m-p/356898#M274175</link>
      <description>&lt;P&gt;Replacing characters is often the purview of the TRANSLATE function. Translate is one of the functions many people use incorrectly as the search string goes after the replacement target&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt; data example;
   string='This is, a string; with. common? punction:';
   newstring = translate(string,' ',',.;:?');
run;&lt;/PRE&gt;
&lt;P&gt;So your example may well work with&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;TxtVar =&amp;nbsp;TRANSLATE(TxtVar, " ", "&amp;amp;PunctChars.");&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Including special character, especially quotes is going to be an issue in several places and without the actual entire Macro code I'm not going to make specific recommendations.&lt;/P&gt;</description>
      <pubDate>Mon, 08 May 2017 15:18:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-all-punctuation-in-a-string-with-spaces/m-p/356898#M274175</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-05-08T15:18:18Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing all punctuation in a string with spaces</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-all-punctuation-in-a-string-with-spaces/m-p/356900#M274176</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines ;
input TxtVar &amp;amp;$100.;
new=prxchange('s/[",?.]//',-1,txtvar);
datalines;
This, has, commas\n
And. This. Has. Periods\n
This "is" harder\n
How about some question marks ??\n
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 08 May 2017 15:21:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-all-punctuation-in-a-string-with-spaces/m-p/356900#M274176</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2017-05-08T15:21:08Z</dc:date>
    </item>
    <item>
      <title>Re: Replacing all punctuation in a string with spaces</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Replacing-all-punctuation-in-a-string-with-spaces/m-p/356909#M274177</link>
      <description>&lt;P&gt;This regular expression may work for you&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data example;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; string='This is, a string;"%@!^~ with. common? punction:';&lt;BR /&gt;&amp;nbsp;&amp;nbsp; new=prxchange('s/[^a-zA-Z0-9_-]/ /',-1,string);&amp;nbsp;&amp;nbsp;&amp;nbsp; /* By putting characters in [^...], you can decide which characters should be allowed and all others replaced */&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;Hope this helps,&lt;/P&gt;
&lt;P&gt;Ahmed&lt;/P&gt;</description>
      <pubDate>Mon, 08 May 2017 15:33:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Replacing-all-punctuation-in-a-string-with-spaces/m-p/356909#M274177</guid>
      <dc:creator>AhmedAl_Attar</dc:creator>
      <dc:date>2017-05-08T15:33:41Z</dc:date>
    </item>
  </channel>
</rss>

