DATA Step, Macro, Functions and more

Find And Replace within a string

Accepted Solution Solved
Reply
N/A
Posts: 0
Accepted Solution

Find And Replace within a string

Hi

I am trying to find a character within a string and replace it with something else, within the data step.
For eg.
My&Name&Is&Simon
Hello&World
Simon

I want to replace the & with a space.

I have tried a few of the macros available on here but they only seem to replace the first instance only.
Any ideas?

thanks
Simon

Accepted Solutions
Solution
‎03-16-2017 08:34 AM
SAS Employee
Posts: 105

Re: Find And Replace within a string

[ Edited ]

You can use translate function.

For example:

 

data a;
 input x $40.;
 y=translate(x,' ','&');
cards;
My&Name&Is&Simon
Hello&World
Simon
;
run;

Result:

 

x                   y

My&Name&Is&Simon    My Name Is Simon
Hello&World         Hello World     
Simon               Simon           

From @sbb: For more than one character in a sequence, use the TRANWRD function.

 

From @Patrick: And since it looks like you might be trying to decode URL strings, check out the URLDECODE function which handles your specific task.

 

data _null_;
 infile datalines truncover;
 input text $40.;
 text=translate(text,' ','&');
 text=urldecode(text);
 put text;
datalines;
My&Name&Is&Simon
Simon%27s&World
%27 - '
%28 - (
%29 - )
;

Output:

 

My Name Is Simon
Simon's World
' - '
( - (
) - )

View solution in original post


All Replies
Solution
‎03-16-2017 08:34 AM
SAS Employee
Posts: 105

Re: Find And Replace within a string

[ Edited ]

You can use translate function.

For example:

 

data a;
 input x $40.;
 y=translate(x,' ','&');
cards;
My&Name&Is&Simon
Hello&World
Simon
;
run;

Result:

 

x                   y

My&Name&Is&Simon    My Name Is Simon
Hello&World         Hello World     
Simon               Simon           

From @sbb: For more than one character in a sequence, use the TRANWRD function.

 

From @Patrick: And since it looks like you might be trying to decode URL strings, check out the URLDECODE function which handles your specific task.

 

data _null_;
 infile datalines truncover;
 input text $40.;
 text=translate(text,' ','&');
 text=urldecode(text);
 put text;
datalines;
My&Name&Is&Simon
Simon%27s&World
%27 - '
%28 - (
%29 - )
;

Output:

 

My Name Is Simon
Simon's World
' - '
( - (
) - )
Super User
Posts: 5,260

Re: Find And Replace within a string

textVar2=translate(textVar,' ','&');

/Linus
Data never sleeps
N/A
Posts: 0

Re: Find And Replace within a string

Hi

Thanks for that...it works, but now have another problem....
My string also contains other things i wanted to replace, like
'%27' i want to replace with an apostrophe
so my original string is
Simon%27s&World

translate works at replacing the & but i tried
translate(translate(title,' ','&'),"%27","'")
but it still only replces the &
Help would be much appreciated.

Thanks
N/A
Posts: 0

Re: Find And Replace within a string

in fact i have half a dozen replacements to do:
%27 - '
%28 - (
%29 - )
and a few others... any ideas?

thanks
Super Contributor
Super Contributor
Posts: 3,174

Re: Find And Replace within a string

Use the TRANWRD function - similar to TRANSLATE.

Refer to SAS Language DOC at the link below:


Scott Barry
SBBWorks, Inc.

SAS 9.2 TRANWRD DOC:

http://support.sas.com/documentation/cdl/en/lrdict/59540/HTML/default/a000215027.htm


SAS 9.1 DOC:

http://support.sas.com/documentation/onlinedoc/91pdf/index.html


SAS DOC main page (choose your SAS component and version):

http://support.sas.com/documentation/index.html
N/A
Posts: 0

Re: Find And Replace within a string

Perfect thanks!!
Respected Advisor
Posts: 3,902

Re: Find And Replace within a string:

Hi Simon

Looks like URL escape syntax.
Try this - it should also cover cases which might not yet be in your current data:

data _null_;
infile datalines truncover;
input text $40.;
text=translate(text,' ','&');
text=urldecode(text);
put text;
datalines;
My&Name&Is&Simon
Simon%27s&World
%27 - '
%28 - (
%29 - )
;

http://support.sas.com/onlinedoc/913/getDoc/en/lrdict.hlp/a001191534.htm

HTH
Patrick
🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 10235 views
  • 1 like
  • 5 in conversation