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.
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;
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;
@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
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.
Great tip, added to my SAS bookmarks now 🙂
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;
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;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.