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?
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
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;
%let have = a,b,c;
%let want=%sysfunc(translate(%bquote(&have),%str( ),%str(,)));
%put &want;
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
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.