Replacing all punctuation in a string with spaces

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

Replacing all punctuation in a string with spaces

[ Edited ]

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.

 

data have;
infile datalines delimiter="\n";
input TxtVar $;
datalines;
This, has, commas\n
And. This. Has. Periods\n
This "is" harder\n
How about some question marks ??\n
;

 

My try so far is this macro:

 

%macro ReplacePunct(datain, dataout, PunctChars);

    data &dataout ;

      set &datain ;

      TxtVar = TRANWRD(TxtVar, "&PunctChars.", " ");

    run;

%mend;

 

The macro call is: %ReplacePunct(have, want, %bquote(,) );

 

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: %ReplacePunct(have, want, %bquote(,.;_@#/\?!'") );

 

So I gues there are two questions:

1. How do i handle the special characters? Double quotes (") or periods (.) always throws an error ?

2. How can I parse the contents of PunctChars parameter one by one ? (or any other method which would lead to replacing each occurence of every character with a space)


Accepted Solutions
Solution
‎05-15-2017 05:31 AM
Grand Advisor
Posts: 10,196

Re: Replacing all punctuation in a string with spaces

[ Edited ]

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

 

 data example;
   string='This is, a string; with. common? punction:';
   newstring = translate(string,' ',',.;:?');
run;

So your example may well work with

 

TxtVar = TRANSLATE(TxtVar, " ", "&PunctChars.");

 

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.

View solution in original post


All Replies
Solution
‎05-15-2017 05:31 AM
Grand Advisor
Posts: 10,196

Re: Replacing all punctuation in a string with spaces

[ Edited ]

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

 

 data example;
   string='This is, a string; with. common? punction:';
   newstring = translate(string,' ',',.;:?');
run;

So your example may well work with

 

TxtVar = TRANSLATE(TxtVar, " ", "&PunctChars.");

 

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.

Super User
Posts: 1,099

Re: Replacing all punctuation in a string with spaces

data have;
infile datalines ;
input TxtVar &$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
;
Thanks,
Jag
Regular Contributor
Posts: 211

Re: Replacing all punctuation in a string with spaces

This regular expression may work for you

 

data example;
   string='This is, a string;"%@!^~ with. common? punction:';
   new=prxchange('s/[^a-zA-Z0-9_-]/ /',-1,string);    /* By putting characters in [^...], you can decide which characters should be allowed and all others replaced */
run;

Hope this helps,

Ahmed

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 146 views
  • 1 like
  • 4 in conversation