DATA Step, Macro, Functions and more

how to check if a macro variable exists....

Reply
Contributor
Posts: 32

how to check if a macro variable exists....


I have a few macro variables like this:

cat1s1, cat1s2, cat1s3, cat2s1, cat2s2

i need a loop in  my code:

%do i=1 %to 2;

   %do j=1 %to 3;

              ***if macro variable &&cat&i.s&j exist

               *************************

   %end;

%end;

How do i put the blue color sentence into code?

Thanks!

Super User
Posts: 10,516

Re: how to check if a macro variable exists....

The function %Symexist

Contributor
Posts: 32

Re: how to check if a macro variable exists....

i know the function, but I can not use it as %symexist(&&cat&i.s&j), can I?

New Contributor
Posts: 3

Re: how to check if a macro variable exists....

%symexist(cat&i.s&j.) is what you want to be checking if the macro variable is like cat1s1.

Occasional Contributor
Posts: 6

Re: how to check if a macro variable exists....

An alternate method is to put the macro variable names in a %GLOBAL statement. Then you can simply test with %IF or %LENGTH statements.

Super Contributor
Posts: 262

Re: how to check if a macro variable exists....

JayCorbett wrote:

An alternate method is to put the macro variable names in a %GLOBAL statement. Then you can simply test with %IF or %LENGTH statements.

That way you create a global macro variable and symexist will always return true.

The macro %symexist is available as normal sas function. See SAS(R) 9.2 Macro Language: Reference

Occasional Contributor
Posts: 6

Re: how to check if a macro variable exists....

I should have been more complete with my answer. if cat1s1, cat1s2, cat1s3, cat2s1, cat2s2 were declared in a %GLOBAL statement. Then to test for existence you could use

%If &&CAT1S&i^=%str() OR

%If %Length(&&CAT1S&i)>0.

Also, you'll be happier if you

%LET &CAT1=&&CAT1S&i;

Then referencing it is not so tedious....

Super User
Super User
Posts: 6,502

Re: how to check if a macro variable exists....

That is not a test for existence.  That is a test for whether it is empty.

They are two different things.

For example if I want to write a macro than can optionally update the macro variable RETURN I could add this line of code to the top of the macro to check if the macro variable exists and if it doesn't make a local version so that the value is not returned. 

%if not %symexist(RETURN) %then %local return ;

Notice that this will work better than adding a %GLOBAL statement since it will allow for the existence of the macro variable in the local scope of a calling macro environment.

Super User
Super User
Posts: 7,411

Re: how to check if a macro variable exists....

Hi,

Could I ask why your doing this?  The syntax you use CATxSx seems to my mind, to be more closely associated with an array.  Why do you need to use macro language to do this, just use a basic datastep with and array and loop over it.  If you can provide and example of what you want...

Ask a Question
Discussion stats
  • 8 replies
  • 4256 views
  • 1 like
  • 7 in conversation