Trim function problem with comma

Reply
Contributor
Posts: 58

Trim function problem with comma

Hi,

I'm getting the following error

ERROR: More positional parameters found than defined.

running the following code:

%let mystring=A, B    CD   ;

%let mystringtrimmed=%trim(&mystring.);

%put &mystringtrimmed.;

The error does not appear if I use double quote

%let mystringtrimmed=%trim("&mystring.");

As usual, I can't find anything on SAS trim function page.

Could please someone explain me why  the error raise?

Many thanks.

Regards

SAS Employee
Posts: 23

Re: Trim function problem with comma

Leading and trailing blanks are ignored by %let.

%trim is an autocall macro, and your call %trim(&mystring.) will be resolved to %trim(A, B    CD), but you can't send two parameters to %trim.

Trusted Advisor
Posts: 1,610

Re: Trim function problem with comma

%let mystringtrimmed=%trim(%quote(&mystring));

Occasional Contributor
Posts: 15

Re: Trim function problem with comma

Thanks PaigeMiller for the answer to this question.  I had the same question and your %quote fix worked perfectly.  Wish I knew how to mark it as "Correct Answer" to give you points.

Super Contributor
Posts: 282

Re: Trim function problem with comma

Hi,

As has been explained only one argument can be used with the %trim() function, so the comma in your argument needs to be masked / quoted.

To avoid the error message, you could try using the %str() function, e.g.:

%let mystring=A%str(,) B    CD   ;


Regards,

Amir.


Trusted Advisor
Posts: 1,610

Re: Trim function problem with comma

Amir wrote:

Hi,

As has been explained only one argument can be used with the %trim() function, so the comma in your argument needs to be masked / quoted.

To avoid the error message, you could try using the %str() function, e.g.:

%let mystring=A%str(,) B    CD   ;

This works if the user is explicitly creating a string of text before executing the %trim macro function.

If the string comes from somewhere else, for example a data set (where the value could be "New York, NY") and you don't have control over the presence or absence of commas (and other characters), then %trim(%quote(&mystring)) is the solution. In my opinion, it is the more robust solution here.

Ask a Question
Discussion stats
  • 5 replies
  • 510 views
  • 0 likes
  • 5 in conversation