DATA Step, Macro, Functions and more

replace commas in a macro variable and put spaces

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 84
Accepted Solution

replace commas in a macro variable and put spaces

Hello,

 

I have a macro variable like this

 

%let have = a,b,c;

 

and i want a macro withour those commas

 

%let want = %sysfun(tranwrd(&have,',',' ');

 

and this is not working.

 

caould you please suggest?


Accepted Solutions
Solution
‎05-11-2017 03:16 PM
Super User
Super User
Posts: 6,502

Re: replace commas in a macro variable and put spaces

First let's clean up the obvious typos in your posted code and try it.

2129  %let have = a,b,c;
2130  %let want = %sysfunc(tranwrd(&have,',',' '));
ERROR: The function TRANWRD referenced by the %SYSFUNC or %QSYSFUNC macro function has too many
       arguments.

Because &HAVE contains commas the TRANWRD() function is seeing way too many commas. You can add some macro quoting to prevent that.

2134  %let have = a,b,c;
2135  %let want = %sysfunc(tranwrd(%quote(&have),',',' '));
2136  %put &=want;
WANT=a,b,c

But notice that it did not change anything. That is because your macro variable does not contain the any commas that are enclosed in single quotes. Since to macro code everthing is a character string you do not need to use quotes around string literals.  But you will need to macro quote both commas and spaces to get them to be treated as text instead of delimiters.

2137  %let have = a,b,c;
2138  %let want = %sysfunc(tranwrd(%quote(&have),%str(,),%str( )));
2139  %put &=want;
WANT=a b c

 

 

View solution in original post


All Replies
Super User
Posts: 10,550

Re: replace commas in a macro variable and put spaces

Several problems wrong function: sysfunC not sysfun Translate is the function to change single characters, Tranwrd is intended for groups of characters, And since your value has , imbedded they become parameters to the functions and through things off, hence %quote. 

%let have = a,b,c;

%let want = %sysfunc(translate(%quote(&have),' ',','));

%put &have &want;
Super User
Posts: 9,691

Re: replace commas in a macro variable and put spaces

%let have = a,b,c;

%let want=%sysfunc(translate(%bquote(&have),%str( ),%str(,)));

%put &want;
Solution
‎05-11-2017 03:16 PM
Super User
Super User
Posts: 6,502

Re: replace commas in a macro variable and put spaces

First let's clean up the obvious typos in your posted code and try it.

2129  %let have = a,b,c;
2130  %let want = %sysfunc(tranwrd(&have,',',' '));
ERROR: The function TRANWRD referenced by the %SYSFUNC or %QSYSFUNC macro function has too many
       arguments.

Because &HAVE contains commas the TRANWRD() function is seeing way too many commas. You can add some macro quoting to prevent that.

2134  %let have = a,b,c;
2135  %let want = %sysfunc(tranwrd(%quote(&have),',',' '));
2136  %put &=want;
WANT=a,b,c

But notice that it did not change anything. That is because your macro variable does not contain the any commas that are enclosed in single quotes. Since to macro code everthing is a character string you do not need to use quotes around string literals.  But you will need to macro quote both commas and spaces to get them to be treated as text instead of delimiters.

2137  %let have = a,b,c;
2138  %let want = %sysfunc(tranwrd(%quote(&have),%str(,),%str( )));
2139  %put &=want;
WANT=a b c

 

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 360 views
  • 3 likes
  • 4 in conversation