Hello,
When I run the following code I get warning saying that "var1 not resolved" from the %put statement in Macro2.
%Macro Macro1(Var1 = myVar);
%Put Inside Macro1 with Var1 = &Var1;
%Macro2
%Mend Macro1;
%Macro Macro2(Var2);
%Put Inside Macro2 with Var1 = &Var1;
%Mend Macro2;
%Macro1
But when I change the code to the following (only change is semi colon after %Macro2 invocation line) Var1 is accessible from Macro2 and I get no warning. What is going on?
%Macro Macro1(Var1 = myVar);
%Put Inside Macro1 with Var1 = &Var1;
%Macro2;
%Mend Macro1;
%Macro Macro2(Var2);
%Put Inside Macro2 with Var1 = &Var1;
%Mend Macro2;
%Macro1
Thank you
Actually I think the issue that is since you defined macro2 with parameters it does not think you are finished calling macro two yet. So either add () to the macro call or remove them from the macro definition.
%macro Macro1(Var1 = myVar);
%Put Inside Macro1 with Var1 = &Var1;
%macro2()
%mend Macro1;
%macro Macro2(Var2);
%Put Inside Macro2 with Var1 = &Var1;
%mend Macro2;
%macro1()
Because the last statement in MACRO1 does not end in a semi-colon by the time SAS executes the statement MACRO1 has finished and removed its symbol space and hence the definition of the variable VAR1.
Thank you Tom. I dont know if I understand your reply, but if the issue is not having more statements then that doesn't seem to be the case as the following code also have same warning.
%Macro Macro1(Var1 = myVar);
%Put Inside Macro1 with Var1 = &Var1;
%Macro2
%Return;
%Mend Macro1;
%Macro Macro2(Var2);
%Put Inside Macro2 with Var1 = &Var1;
%Mend Macro2;
%Macro1
Also the following code also gives no warning(only change is I removed the parameters to Macro2)
%Macro Macro1(Var1 = myVar);
%Put Inside Macro1 with Var1 = &Var1;
%Macro2
%Mend Macro1;
%Macro Macro2;
%Put Inside Macro2 with Var1 = &Var1;
%Mend Macro2;
%Macro1
Actually I think the issue that is since you defined macro2 with parameters it does not think you are finished calling macro two yet. So either add () to the macro call or remove them from the macro definition.
%macro Macro1(Var1 = myVar);
%Put Inside Macro1 with Var1 = &Var1;
%macro2()
%mend Macro1;
%macro Macro2(Var2);
%Put Inside Macro2 with Var1 = &Var1;
%mend Macro2;
%macro1()
Thanks Tom, I think that is the learning here for me. If a macro takes parameters then using () is the correct way to invoke it, even if no value is passed.
It is A correct way. Anything that indicates to SAS that the macro call had been completed is fine.
I believe macro variables defined within macro default to the local scope of macro variables and not global. Local meaning available during the implementation of the macro while global being available during the sas session.
So implementing the second macro within the first keeps the variable available to the second macro. You can have the same effect by declaring the var1 macro variable as global in the first macro: %global var1; (I didnt look the syntax up but I believe this is it).
Hope that helps.
EJ
Thanks EJ. I was just wondering the reason behind this unexpected behavior.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.