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,
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.