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

I have the following code that works fine. What I need is to replace commas in a=A.B,C string with asterisks to get s="A" "B" "C" string.

The data step version of the code works fine:

%let a=A,B,C;
data a; s='"'||TRANSTRN("&a",",",'" "')||'"'; run;

/* dataset a:  */
/* s           */
/*"A" "B" "C"  */

 

However, using %SYSFUNC(TRANSTR(...)) fails or produces wrong result:

 

%let s=%SYSFUNC(TRANSTRN(&a,",",'" "')); %put s=&s;
/*ERROR: The function TRANSTRN referenced by the %SYSFUNC or %QSYSFUNC macro function has too many arguments.*/


%let s=%SYSFUNC(TRANSTRN("&a",",",'" "')); %put s=&s;
/*s="A,B,C"*/


%let s=%QSYSFUNC(TRANSTRN(&a,",",'" "')); %put s=&s;
/*ERROR: The function TRANSTRN referenced by the %SYSFUNC or %QSYSFUNC macro function has too many arguments.*/
Any help?

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

SAS Macro level is purely textual and though you don't need to quote text. But then because you're dealing with commas which also have a meaning on macro level you do need to macro quote these strings. Alternatively use a data _null_ step to create the macro variable. That's often easier to formulate and debug.

%let a=A,B,C;

/* option 1 */
%let s="%sysfunc(transtrn(%nrbquote(&a),%nrbquote(,)," "))";
%put &=s;

/* option 2 */
data _null_;
  s=cats('"',TRANSTRN("&a",",",'" "'),'"');
  call symputx('s',s);
  stop;
run;
%put &=s;

View solution in original post

5 REPLIES 5
Tom
Super User Tom
Super User

Why are you searching for quotes in the value of the macro variable A?  There aren't any quotes in A. Your data step did not search for quotes.

So in the data step you did this:

s='"'||TRANSTRN("&a",",",'" "')||'"';

Which is changing commas to a space with double quotes around it.

But in the %SYSFUNC() call you did this:

%let s=%SYSFUNC(TRANSTRN(&a,",",'" "'));

Which is changing a comma with double quotes around it to a space with double quotes around it that also has single quotes around it.

You probably meant to do this instead.

%let s=%SYSFUNC(TRANSTRN(&a,%str(,)," "));

 

You also need to add macro quoting to A because it also has commas in it.

Note there is no need to use TRANSTRN instead of plain old TRANWRD because you aren't converting the values to null strings.

%let s="%SYSFUNC(TRANWRD(%superq(a),%str(,)," "))";
13    %let a=One,Two,Three;
14    %let s="%SYSFUNC(TRANWRD(%superq(a),%str(,)," "))";
15    %put &=a &=s;
A=One,Two,Three S="One" "Two" "Three"
SPR
Quartz | Level 8 SPR
Quartz | Level 8

Thanks for the advice, however, I've got the following result with your code: 

 

ERROR: The function TRANSTRN referenced by the %SYSFUNC or %QSYSFUNC macro function has too many arguments.
27 %let s=%SYSFUNC(TRANSTRN(&a,%str(,)," ")); %put s=&s;

/* s= */

Patrick
Opal | Level 21

SAS Macro level is purely textual and though you don't need to quote text. But then because you're dealing with commas which also have a meaning on macro level you do need to macro quote these strings. Alternatively use a data _null_ step to create the macro variable. That's often easier to formulate and debug.

%let a=A,B,C;

/* option 1 */
%let s="%sysfunc(transtrn(%nrbquote(&a),%nrbquote(,)," "))";
%put &=s;

/* option 2 */
data _null_;
  s=cats('"',TRANSTRN("&a",",",'" "'),'"');
  call symputx('s',s);
  stop;
run;
%put &=s;
SPR
Quartz | Level 8 SPR
Quartz | Level 8
Thanks for the solution. It works fine.
SPR
Quartz | Level 8 SPR
Quartz | Level 8

Using clues from both replies it seems to I found a shorter code:

 

%let a=A,B,C:

%let s=%SYSFUNC(TRANSTRN("&a",%str(,)," ")); %put s=&s;

/*s="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
  • 5 replies
  • 1118 views
  • 0 likes
  • 3 in conversation