The following SAS program is submitted:
%let a=cat;
%macro animal(a=frog);
%let a=bird;
%mend;
%animal(a=pig)
%put a is &a;
What is written to the SAS log?
The following SAS program is submitted:
%let a=cat;
%macro animal(a=frog);
%let a=bird;
%mend;
%animal(a=pig)
%put a is &a;
What is written to the SAS log?
The statement %let a=cat; creates a global macro variable a with the value 'cat'. When the macro %animal(a=frog); is defined, it takes an optional parameter 'a', defaulting to 'frog' if not supplied. Inside this macro, %let a=bird; changes the local macro variable 'a' to 'bird'. However, when the macro is invoked as %animal(a=pig), the macro variable 'a' is locally set to 'pig' within the macro. The macro completes, but does not affect the global macro variable 'a'. Finally, when %put a is &a; is executed, it references the global macro variable 'a', which is 'cat', as declared at the beginning. Therefore, the correct output is 'a is cat'.
a is pig %let a=cat creates variable a in global symbol table; %macro animal (a=frog): as this is parameter, variable a with value of frog is created in local symbol table %put a is &a is to print variable a. As this is inside a macro, this will look at local symbol table first and take value of frog. %let a=bird looks at the local symbol table first and will find and overwrite variable a =frog with a=bird. When the macro is called with parameter, SAS will look at local symbol table and find and overwrite variable a with a =pig