BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
BogdanC
Fluorite | Level 6

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)

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

3 REPLIES 3
ballardw
Super User

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.

Jagadishkatam
Amethyst | Level 16
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
AhmedAl_Attar
Ammonite | Level 13

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

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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