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

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

3 REPLIES 3
ballardw
Super User

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;
Ksharp
Super User
%let have = a,b,c;

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

%put &want;
Tom
Super User Tom
Super User

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

 

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 3 replies
  • 33692 views
  • 6 likes
  • 4 in conversation