BookmarkSubscribeRSS Feed
bayzid
Obsidian | Level 7

How can I add a underscore at the end of each macro variable?

For example, I have: %LET mvar= var1 var2 var2.. I want mvar to contain var1_ var2_ var2_

2 REPLIES 2
Tom
Super User Tom
Super User

That is adding underscores in the MIDDLE of the VALUE of the macro variable.

 

If you know that the string only has one space between the words then you can use TRANWRD() function.  You will have to use the %SYSFUNC() macro function to use TRANWRD() function in macro code.

%let mvar=%sysfunc(tranwrd(&mvar,%str( ),%str(_ )))_;

If you are worried it might have multiple spaces between the words then use COMPBL() first.

%let mvar=%sysfunc(tranwrd(%sysfunc(compbl(&mvar)),%str( ),%str(_ )))_;

Example

818  %LET mvar= var1   var2   var2 ;
819
820  %let mvar=%sysfunc(tranwrd(%sysfunc(compbl(&mvar)),%str( ),%str(_ )))_;
821
822  %put &=mvar;
MVAR=var1_ var2_ var2_
Ksharp
Super User
 %LET mvar= var1   var2   var2 ;

 %let mvar=%sysfunc(prxchange(s/(\w+)/$1_/,-1,&mvar));

 %put &=mvar;

sas-innovate-white.png

🚨 Early Bird Rate Extended!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Lock in the best rate now before the price increases on April 1.

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 2 replies
  • 601 views
  • 2 likes
  • 3 in conversation