- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
The function %Symexist
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
i know the function, but I can not use it as %symexist(&&cat&i.s&j), can I?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
%symexist(cat&i.s&j.) is what you want to be checking if the macro variable is like cat1s1.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
An alternate method is to put the macro variable names in a %GLOBAL statement. Then you can simply test with %IF or %LENGTH statements.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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....
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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...