Help using Base SAS procedures

How to check existence of a macro variable .

Reply
Regular Contributor
Posts: 222

How to check existence of a macro variable .

Hello everyone,

Sometime I don't know whether there was a macro variable in the SAS environment and I want to delete it .It will show me a warning  message if the macro variable not exist when I trying to delete it.

%let bbb=1;

%SYMDEL  aaa bbb; /*try to delete aaa  and bbb,but aaa is not exist*/

 

"WARNING: Attempt to delete macro variable aaa  failed. Variable not found."

My question is ,To avoid the warning message.How to check whether a macro variable exist before delete it?

Thanks

Mike

Respected Advisor
Posts: 3,156

Re: How to check existence of a macro variable .

Posted in reply to Mike_Davis

%let bbb=1;

%put %symexist(bbb);

%put %symexist(aaa);

Haikuo

Super Contributor
Posts: 282

Re: How to check existence of a macro variable .

Posted in reply to Mike_Davis

Hi

Have you tried the %symexist macro function?

Regards,

Amir.

PROC Star
Posts: 7,492

Re: How to check existence of a macro variable .

Posted in reply to Mike_Davis

You could just add the /NOWARN option.

Super User
Posts: 5,518

Re: How to check existence of a macro variable .

Posted in reply to Mike_Davis

Presumably, you must be asking about open code.  Otherwise if you were talking about inside a macro, you could just use %SYMEXIST, as suggested by others:

%if %SYMEXIST(aaa) %then %SYMDEL(aaa);

Since %IF is not permitted in open code, here's a way around that:

data _null_;

   set sashelp.vmacro;

   where upcase(name)='AAA';

   call execute('%symdel(aaa);');

run;

I'm not sure if I have the syntax exactly right here, but the approach should work easily enough.  If you have a series of macro variables to delete, you would need to replace the WHERE statement:

if upcase(name) = 'AAA' then call execute('%symdel(aaa);');

else if upcase(name)='BBB' then call execute('%symdel(bbb);');

If you need to apply this to a long series of macro variable names, you might as well write a macro and stick with %symexist / %symdel.

Good luck.

PROC Star
Posts: 7,492

Re: How to check existence of a macro variable .

Posted in reply to Mike_Davis

In case my previous message wasn't sufficiently clear, I think the following does exactly what you want:

%let bbb=1;

%SYMDEL  aaa bbb /NOWARN;

Super User
Posts: 5,518

Re: How to check existence of a macro variable .

Art,

It doesn't get any better than that.  Good solution.

Ask a Question
Discussion stats
  • 6 replies
  • 327 views
  • 7 likes
  • 5 in conversation