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

Hi there

 

As a simple example , I have a data set that looks like below - the question the person gets asked depends on other criteria, and at the end I want to populate the person's name into question2 instead of the dummy tag.

 

I've tried using tranwrd and this works for part of my problem - but it replaces {{lead.FirstName}}  in Question2 to say "Tom" for every row as this is the last name in my data set. 

 

the code I am using is (in Base SAS 9.4):

 

data test;
set test;
call symput('FirstName2',trim(FirstName));
question2 = (tranwrd(question,"{{lead.First Name}}","&FirstName2.");
run;

 

Please could someone help me on how I get the correct name populated in each data row? have been searching and trying things for hours but can't find anything.

 

FirstNameQuestionQuestion2 (what I want it to say)Question2 (what it actually does)
Caroline{{lead.First Name}}, do you have a red car?Caroline, do you have a red car?Tom, do you have a red car?
Mike {{lead.First Name}}, do you have a blue car?Mike, do you have a blue car?Tom, do you have a blue car?
Eden{{lead.First Name}}, do you have a green car?Eden, do you have a green car?Tom, do you have a green car?
Tom{{lead.First Name}}, do you have a black car?Tom, do you have a black car?Tom, do you have a black car?
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Why are you using a macro variable at all?

Since you want to use the current value of Firstname to replace text on that record there is not reason to create a macro variable.

 

Did you try some

 

question2 = tranwrd(question,"{{lead.First Name}}", strip(FirstName) );

View solution in original post

4 REPLIES 4
ballardw
Super User

Why are you using a macro variable at all?

Since you want to use the current value of Firstname to replace text on that record there is not reason to create a macro variable.

 

Did you try some

 

question2 = tranwrd(question,"{{lead.First Name}}", strip(FirstName) );

Tom
Super User Tom
Super User

SAS resolves the macro code/references before it compiles and runs the data step.

So the first time you ran it it would have generated this value instead.

&Firstname2., do you have a red car?

And written this note into the log:

WARNING: Apparent symbolic reference FIRSTNAME2 not resolved.

It is only after you had run the step at least once that FIRSTNAME2 had a value and of course it just has the value from the last observation as at each iteration of the data step the macro variable's value is getting changed.

 

Do not use a macro variable for this problem as there is nothing about it that requires you to generate any code.

Just use the value that is already in the dataset!!

question2 = tranwrd(question,'{{lead.First Name}}',trim(FirstName)) ;
ScoobieR
Obsidian | Level 7

Thank you Tom and BallardW - I don't know why I didn't try this - I thought the last parameter of tranwrd had to be in quotes! Clearly been looking at it too long.

 

Both great and helpful replies - thank you.

ballardw
Super User

@ScoobieR wrote:

Thank you Tom and BallardW - I don't know why I didn't try this - I thought the last parameter of tranwrd had to be in quotes! Clearly been looking at it too long.

 

Both great and helpful replies - thank you.


The quotes would be needed if I want to set a specific value, not a variable, for all records. Such as

tranwrd (schoolname, 'HS', 'High School');

 

The requirement is that the result be a character value. You can even do operations such as concatenating a string and a variable, or multiple variables, or the substr of a variable or ...

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 4158 views
  • 2 likes
  • 3 in conversation