Replacing words in a variable

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 79
Accepted Solution

Replacing words in a variable

Hi Everyone,

 

I have the following data set:

 

Screen Shot 2018-07-23 at 20.00.24.png

Code used to create above data set is:

data Questions;
length Question $100 Name $20;
infile datalines dlm=',';
input Question $ Name $ ;
cards;
When was your last interaction with Pablo Matinee,Pablo Matinee
When was your last interaction with Lucien Koku,Lucien Koku
;

 

I want to replace every occurrence of the customer Name in the Question with [Name]. So what i intend to achieve is this:

 

Screen Shot 2018-07-23 at 20.03.00.png

 

What i have written so far is:

 

data New;
set Questions;
tranwrd(Question,Name,"%str(%[)Name%str(%])");
run;

 

but this has not worked.  Please can anyone suggest a solution?

 

Thanks,


Accepted Solutions
Solution
a month ago
Esteemed Advisor
Posts: 5,627

Re: Replacing words in a variable

Use macros in last resort. They are not needed here.

 

data New;
set Questions;
length newQuestion $100;
newQuestion  = tranwrd(Question, trim(name), "[Name]");
run;
PG

View solution in original post


All Replies
Contributor svh
Contributor
Posts: 21

Re: Replacing words in a variable

You may want to try the TRANSTRN() function, for it lets you remove part of string and not leave a blank behind:

 

 new_var = transtrn(Question, Name, "[Name]");
Regular Contributor
Posts: 170

Re: Replacing words in a variable

a=index(question, '[');

newvar=substr(question,1,a-1)||put(name,$50.);

 

??

--------------
blog: papersandprograms.com
Solution
a month ago
Esteemed Advisor
Posts: 5,627

Re: Replacing words in a variable

Use macros in last resort. They are not needed here.

 

data New;
set Questions;
length newQuestion $100;
newQuestion  = tranwrd(Question, trim(name), "[Name]");
run;
PG
Super User
Posts: 24,026

Re: Replacing words in a variable

TRANWRD is for a word but you have two words so it's not going to match. Is it possible to have multiple iterations of a persons name in the text? And is it guaranteed that the name is always at the end?

 

You do want a TRANSLATE/TRANS family of functions for sure though. If you have multiple occurrences it could be problematic though.

 


@frupaul wrote:

Hi Everyone,

 

I have the following data set:

 

Screen Shot 2018-07-23 at 20.00.24.png

Code used to create above data set is:

data Questions;
length Question $100 Name $20;
infile datalines dlm=',';
input Question $ Name $ ;
cards;
When was your last interaction with Pablo Matinee,Pablo Matinee
When was your last interaction with Lucien Koku,Lucien Koku
;

 

I want to replace every occurrence of the customer Name in the Question with [Name]. So what i intend to achieve is this:

 

Screen Shot 2018-07-23 at 20.03.00.png

 

What i have written so far is:

 

data New;
set Questions;
tranwrd(Question,Name,"%str(%[)Name%str(%])");
run;

 

but this has not worked.  Please can anyone suggest a solution?

 

Thanks,



 

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 174 views
  • 4 likes
  • 5 in conversation