Hi Everyone,
I have the following data set:
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:
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,
Use macros in last resort. They are not needed here.
data New;
set Questions;
length newQuestion $100;
newQuestion = tranwrd(Question, trim(name), "[Name]");
run;
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]");
a=index(question, '[');
newvar=substr(question,1,a-1)||put(name,$50.);
??
Use macros in last resort. They are not needed here.
data New;
set Questions;
length newQuestion $100;
newQuestion = tranwrd(Question, trim(name), "[Name]");
run;
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:
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:
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,
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.