BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
frupaul
Quartz | Level 8

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,

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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

4 REPLIES 4
svh
Lapis Lazuli | Level 10 svh
Lapis Lazuli | Level 10

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]");
pau13rown
Lapis Lazuli | Level 10

a=index(question, '[');

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

 

??

PGStats
Opal | Level 21

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
Reeza
Super User

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,



 

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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