03-19-2015 03:12 PM
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
How do i put the blue color sentence into code?
03-23-2015 07:45 AM
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
03-24-2015 10:45 AM
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
Also, you'll be happier if you
Then referencing it is not so tedious....
03-24-2015 01:00 PM
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.
03-24-2015 11:35 AM
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...