DATA Step, Macro, Functions and more

Find And Replace within a string

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 12
Accepted Solution

Find And Replace within a string

Hello

 

I have a variable called Degree. It has various values such as:

MD

NP

PA

MD-PHD

MD PHD

PA-C

 

I want to search within the degree variable and look for space or - and replace it with ;. So basically I want the output like MD;PHD, PA;C. I am using TRANWRD function but it's not working for multiple characters.

 

Thanks

Chandan Mishra

 


Accepted Solutions
Solution
‎08-01-2017 09:20 AM
PROC Star
Posts: 7,468

Re: Find And Replace within a string

I would use the translate function. e.g.:

data have;
  informat degree $20.;
  input degree &;
  cards;
MD
NP
PA
MD-PHD
MD PHD
PA-C
;

data want;
  set have;
  degree=translate(strip(degree),";;"," -");
run;

Art, CEO, AnalystFinder.com

 

View solution in original post


All Replies
Solution
‎08-01-2017 09:20 AM
PROC Star
Posts: 7,468

Re: Find And Replace within a string

I would use the translate function. e.g.:

data have;
  informat degree $20.;
  input degree &;
  cards;
MD
NP
PA
MD-PHD
MD PHD
PA-C
;

data want;
  set have;
  degree=translate(strip(degree),";;"," -");
run;

Art, CEO, AnalystFinder.com

 

Occasional Contributor
Posts: 12

Re: Find And Replace within a string

Thank you.
☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 92 views
  • 2 likes
  • 2 in conversation