SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
sandyming
Calcite | Level 5


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!

8 REPLIES 8
sandyming
Calcite | Level 5

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

AYBiBTU
Calcite | Level 5

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

JayCorbett
Calcite | Level 5

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

andreas_lds
Jade | Level 19

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

JayCorbett
Calcite | Level 5

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....

Tom
Super User Tom
Super User

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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...

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 28071 views
  • 2 likes
  • 7 in conversation