56. The following SAS program is submitted:
%put a is &a;
Which one of the following is written to the SAS log?
(A) a is &a
(B) a is cat
(C) a is pig
(D) a is bird
The answer is B. But why (D) is not correct? Thanks a lot!
The question is related to global and local macrovariables relationship. In this case we have a global macro variable a with value 'cat', and inside the macro we have a local macrovariable a with value 'bird'. These are two different macro variables. Because macro does not change global macro variable its value remains unchanged and equals to 'cat'.
SPR is absolutly correct but the reason that there is a local and global version of &A is more subtle. A slight change in the program will give D as the answer.
%put a is &a;
now there is no version of &A on the local table, and cat is replaced by bird.
In your original version of the macro %ANIMAL it is the parameter (A=) that forces a &A to appear on the local table. A %LET inside the macro is not sufficient. I feel that this is an important, albeit fairly subtle, difference in behavior that gets folks into trouble.
I understand that first a global table with a= cat created and then a local table with a=frog created and in the last step it is updated to pig. But when resolving a macro variable (&a) doesn’t SAS first look into lical table and if it finds it it retrieves it from local (hence here pig) and if it cannot find it from local table it looks into global table?