BookmarkSubscribeRSS Feed
advoss
Quartz | Level 8
I know that I can use regular expressions in the Enhanced Editor to find strings. Is it possible to use regular expressions in the "Replace with:" box on the Replace window?

Thank you.
8 REPLIES 8
Patrick
Opal | Level 21
I can understand how a regular expression works for finding patterns - but how would a regular expression work for replacing a string? How exactly would it replace it??
advoss
Quartz | Level 8
Unix tools like SED and AWK, Perl, too, I think, have provisions for remembering the "found" string and then altering it, like changing lower case to upper case, leaving all numerics alone and deleting the characters, etc.

Probably a little to specialized for an IDE editor.
ChrisNZ
Tourmaline | Level 20
It is possible.
For example, enter \d in the find text box and enter 4 in the replace with box to replace all digits with 4.
There seems to be a very limited set of regular expressions that can be used however.
advoss
Quartz | Level 8
I was hoping that I might be able to do something like
find string is \a\d\a
replace string is \a_\a
to replace all strings that have an alpha followed by a digit followed by and alpha to be replaced by the original alpha, and underscore, and the original trailing alpha.

I think I am asking for a little too much. There are other editors out there that will allow me to do something like this, or I can handle it in other ways.

Thanks for your thoughts.

This thread can be closed
Tim_SAS
Barite | Level 11
Many text editors I've used, and many programming languages, support replacement by regular expressions. The simplest case is replacing the text that is matched with new text. For example (in vi)

s/June/July/

replaces the text "June" with "July". You can also identify a part or several parts of the regular expression (typically by surrounding it/them with parentheses). The text matched by that part is replaced with new text. For example, in my text editor I can specify this regular expression as a search target:

(\c+)blank

This means "any string that starts with at least one alphabetic character followed by "blank". The parentheses around \c+ "tags" that part of the match. Then I specify this as the replacement string:

blank\1

This replaces the match with the string blank followed by the first (in our case, only) tagged expression. That is, whatever \c+ matched.

So, if I have the words "ageblank" and "sexblank" in my text, they are replaced by "blankage" and "blanksex", respectively.
ChrisNZ
Tourmaline | Level 20
It would make sense that the enhanced editor could do what you both mention.

Indeed, the fact that a regexp check box is available would seem to imply that this is the case.

The only use I have found for this feature is to search (not replace) using the few expressions available using the > button beside the find box.
ishmo65
Obsidian | Level 7

Assuming you mean Find/Replace in SAS EG's editor or a similar SAS-based program editor, the short answer is ... no !

See Solved: Regular expression search and replace in SAS EG is broken - SAS Support Communities

Contrary to the title, it is not "solved". Comments added by a SAS employee on that Thread confirm SAS EG does not have this functionality in the Replace even though it can be used in the Find

In simple terms what you're asking is ... can you (for example):

Find: (.*)_date

Replace: date_\1

and clicking Replace (or Replace All) with Match whole word on and regular expression turned on (this should flip "date" as a prefix instead of a suffix) ?
But this does not work in SAS EG, whereas it will work in Notepad++ (which is where I end up doing these sorts of reg-exp changes).

On the flip side:

Find: (.*)_date

clicking Find (not Replace) will work, finding words that have _date as a suffix.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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