CPak
CPak

Reputation: 13581

assign macro variable output of tranwrd in macro function

I can't figure out this seemingly trivial problem - expect macro variable to be assigned mpg_city.

%macro test(col=);
    %let id = %sysfunc(tranwrd(&col, 'extra_', ''));    
    %put &id;
%mend test;
%test(col=extra_mpg_city);

Current output is extra_mpg_city.

Upvotes: 0

Views: 1479

Answers (2)

Richard
Richard

Reputation: 27508

Arguments listed in a function invoked through %sysfunc are implicitly text and should not be quoted. Placing quotes in a sysfunc invoked function is like nesting quotes in a DATA step invocation.

Try

%let id = %sysfunc(tranwrd(&col, extra_, %str()));

The DATA Step analog is

id = tranwrd("&col", "extra_", "");

Your original code in DATA Step analog (below) should show why the tranwrd did not operate as you expected.

id = tranwrd("&col", "'extra_'", "''");

Upvotes: 4

user667489
user667489

Reputation: 9569

You don't need the quotes when using string functions with %sysfunc, unless you expect to find them in the input. Try this:

%macro test(col=);
    %let id = %sysfunc(tranwrd(&col, extra_, ));    
    %put &id;
%mend test;
%test(col=extra_mpg_city);

Upvotes: 2

Related Questions