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,



 

sas-innovate-2024.png

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.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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