DATA Step, Macro, Functions and more

Macro and semicolon

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 16
Accepted Solution

Macro and semicolon


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


Accepted Solutions
Solution
‎09-10-2013 10:14 AM
Super User
Super User
Posts: 6,502

Re: Macro and semicolon

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()

View solution in original post


All Replies
Super User
Super User
Posts: 6,502

Re: Macro and semicolon

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.

Occasional Contributor
Posts: 16

Re: Macro and semicolon

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

Solution
‎09-10-2013 10:14 AM
Super User
Super User
Posts: 6,502

Re: Macro and semicolon

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()

Occasional Contributor
Posts: 16

Re: Macro and semicolon

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.

Super User
Super User
Posts: 6,502

Re: Macro and semicolon

It is A correct way.  Anything that indicates to SAS that the macro call had been completed is fine.

Super Contributor
Posts: 333

Re: Macro and semicolon

You are exactly right ... missed that in my look over the code.

EJ

Super Contributor
Posts: 333

Re: Macro and semicolon

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

Occasional Contributor
Posts: 16

Re: Macro and semicolon

Thanks EJ. I was just wondering the reason behind this unexpected behavior.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 380 views
  • 0 likes
  • 3 in conversation