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

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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