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

Hi everyone.

I have perhaps simple question but I stacked with this code and I don't know how to correct it.

 

I have a variable called "zm". And I want to create a new variable (zm2) based on this first one. I tried a few codes like this below but not one of them works.

 

%let zm = yrbrn edlvdee isco08 blgetmg;

%let zm2 = %sysfunc(tranward(&zm, "isco08", "iscogrp"));
%put &zm2;

 

I want to receive something like that:

zm2 = yrbrn edlvdee ISCOGRP blgetmg;

 

Could anyone help me?

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19

tranWARD is not a function.  Don't use " ";

 

4    %let zm = yrbrn edlvdee isco08 blgetmg;
5    %let zm2 = %sysfunc(transtrn(&zm,isco08,iscogrp));
6    %put &=zm2;
ZM2=yrbrn edlvdee iscogrp blgetmg

 

View solution in original post

8 REPLIES 8
data_null__
Jade | Level 19

tranWARD is not a function.  Don't use " ";

 

4    %let zm = yrbrn edlvdee isco08 blgetmg;
5    %let zm2 = %sysfunc(transtrn(&zm,isco08,iscogrp));
6    %put &=zm2;
ZM2=yrbrn edlvdee iscogrp blgetmg

 

PaigeMiller
Diamond | Level 26

Seems to me that the quotes are needed inside the transtrn function.

--
Paige Miller
data_null__
Jade | Level 19

@PaigeMiller wrote:

Seems to me that the quotes are needed inside the transtrn function.


Not when using with SYSFUNC on macro variables.  Unless the quotes are part of the string you want to change.  Notice how this doesn't work.

 

7    %let zm = yrbrn edlvdee isco08 blgetmg;
8    %let zm2 = %sysfunc(transtrn(&zm,"isco08","iscogrp"));
9    %put &=zm2;
ZM2=yrbrn edlvdee isco08 blgetmg
novinosrin
Tourmaline | Level 20

Guru, Nice example to understand how tokenisation works especially when literal tokens are processed  by the word scanner , then resolved by the macro processor and eventually sent to compiler for execution. Kudos! 

Margrett
Obsidian | Level 7

Could you tell me also how to delete a word from string? 

 

If I use this function then I get an extra space in the variable and I don't want that. Is there any other way?

 

%let zm_obj3 = %sysfunc(transtrn(&zm_obj2,eduyrs,));
%put &zm_obj3;
yrbrn edlvdee  hhmmb iscogrp hinctnta rlgdgr rlgdnm stflife domicil inprdsc blgetmg
data_null__
Jade | Level 19

@Margrett wrote:

Could you tell me also how to delete a word from string? 

 

If I use this function then I get an extra space in the variable and I don't want that. Is there any other way?

 

%let zm_obj3 = %sysfunc(transtrn(&zm_obj2,eduyrs,));
%put &zm_obj3;
yrbrn edlvdee  hhmmb iscogrp hinctnta rlgdgr rlgdnm stflife domicil inprdsc blgetmg

 

Your target does not include the blank so you are getting what you asked for.  You could use COMPBL after TRANSTRN or add a space to the target.

 

141  %let zm = yrbrn edlvdee isco08 blgetmg;
142  %let zm2 = %sysfunc(transtrn(&zm,%str(isco08 ),));
143  %put &=zm2;
ZM2=yrbrn edlvdee blgetmg

 

I'm a bit worried that you are doing this macro string manipulation when you should be using different tools.  Can you start a new question where you ask a more fundamental question related to your overall goal.

Margrett
Obsidian | Level 7

Thank you for your help. I'm doing a project for logistic regression for my university classes. It's more about results than code itself but I wonder how can I automatize the code in few places rather than manually rewrite variables etc. So it is good. I don't need more. Thank you for your time and help. 🙂

Ksharp
Super User
%let zm = yrbrn edlvdee isco08 blgetmg;

%let zm2 = %sysfunc(tranwrd(&zm, isco08, iscogrp));
%put &zm2;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 8 replies
  • 1547 views
  • 4 likes
  • 5 in conversation