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

Dear community,

 

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.

 

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 "|"). 

 

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:

 

%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 &_output;
      set &_input;
      new_string = magical_function(old_string, &chars_to_replace, &char_to_replace_with);
run;

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hello @larsc,

 

I think the TRANSLATE function is a good candidate:

%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(&char_to_replace_with,%length(&chars_to_replace)-3), &chars_to_replace);
run;

View solution in original post

5 REPLIES 5
FreelanceReinh
Jade | Level 19

Hello @larsc,

 

I think the TRANSLATE function is a good candidate:

%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(&char_to_replace_with,%length(&chars_to_replace)-3), &chars_to_replace);
run;
ballardw
Super User

@larsc wrote:

Dear community,

 

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.

 

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 "|"). 

 

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:

 

%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 &_output;
      set &_input;
      new_string = magical_function(old_string, &chars_to_replace, &char_to_replace_with);
run;

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.

 


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

data example;
   string =  "My, name - is. Lars!";
   y=translate(string,"||||||||", "-,.|''_!");
run;

However, 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.

The Compress function could be used on the translated string to remove spaces if that is a separate requirement.

 

Really big caution: placing random characters into Macro variables may be an issue because of the uses of % and & characters by the macro language

Reeza
Super User

This is a great page to bookmark.
https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.5&docsetId=lefunctionsref&docsetTarg...

Then you can search for "replace" and it'll find 11 functions, three of which can be used to solve your problem.

larsc
Obsidian | Level 7

Great tip, added to my SAS bookmarks now 🙂 

PGStats
Opal | Level 21

For some more magic, use prxChange

 

%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/[&chars_to_replace.]/&char_to_replace_with./o", -1, old_string);
run;

PGStats_0-1615930360764.png

 

To replace multiple consecutive special characters with a single character, it's simple :

 

%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/[&chars_to_replace.]+/&char_to_replace_with./o", -1, old_string);
run;

PGStats_0-1615931032980.png

 

PG

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 5 replies
  • 6486 views
  • 8 likes
  • 5 in conversation