Help using Base SAS procedures

Problem of using macro

Reply
Regular Contributor
Posts: 222

Problem of using macro

Hello everyone,

In the SAS code below,the third one has a problem ,please advise.

Thanks

Mike

%macro test(a);
%put a: &a;
%mend test;

%test(a=aa);


%test(a=njhl5IJK44t95y7u45n64);


%test(a=njhl5u#$%^&*(IJK44t95y7u45n64);/*This one has a problem*/

Super User
Posts: 11,343

Re: Problem of using macro

Posted in reply to Mike_Davis

Special characters such as % & as a minimum get involved with the macro resolver. Depending on what you are actually trying to do you need to either mast or quote the string involving those characters. Look up %str() and the various macro quoting functions.

Regular Contributor
Posts: 222

Re: Problem of using macro

Would you give me a solution?

Thanks

Mike

Regular Contributor
Posts: 222

Re: Problem of using macro

Posted in reply to Mike_Davis

%macro test(a);
%put value of a is: &a;
%mend test;

%test(a=njhl5u#$%^&*IJK44t95y7u45n64); /*this one works*/
%test(a=njhl5u#$%^&*(IJK44t95y7u45n64);/*this one doesn't work*/

PROC Star
Posts: 7,473

Re: Problem of using macro

Posted in reply to Mike_Davis

How about:

%macro test(a);

%let a2=%nrbquote(&a);

%put a: &a2;

%mend test;

%test(a=aa)

%test(a=njhl5IJK44t95y7u45n64)

%test(a=%str(njhl5u#$%^&*%(IJK44t95y7u45n64))

Regular Contributor
Posts: 222

Re: Problem of using macro

Thank you Art,

This works!

But do we must use a %str( ) function in the parameter list?

could we move the %str( ) in the code part?

Thanks!

Mike

 


PROC Star
Posts: 7,473

Re: Problem of using macro

Posted in reply to Mike_Davis

Mike: I don't think it can be moved as ( had to be replaced with %( in calling the macro.  However, I am definitely NOT an expert in macro quoting.  You can find some good reading if you do a search for macro papers by either Ian Whitlock and/or Art Carpenter.

Super User
Super User
Posts: 7,046

Re: Problem of using macro

Posted in reply to Mike_Davis

You have to do something.

Why not just use single quotes around the value?  You can remove them when you need to.

%macro test(a);

   %let y=%qsysfunc(dequote(&a));

   %put a=%superq(a);

   %put y=%superq(y);

%mend test;

Of course you still need watch out for strings that contain single quotes.

%test(a='Don''t forget to double the single quotes')

Ask a Question
Discussion stats
  • 7 replies
  • 257 views
  • 3 likes
  • 4 in conversation