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
Rhodochrosite | Level 12

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

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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