BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Jakkas
Calcite | Level 5


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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

8 REPLIES 8
Tom
Super User Tom
Super User

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.

Jakkas
Calcite | Level 5

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

Tom
Super User Tom
Super User

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

Jakkas
Calcite | Level 5

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.

Tom
Super User Tom
Super User

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

esjackso
Quartz | Level 8

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

EJ

esjackso
Quartz | Level 8

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

Jakkas
Calcite | Level 5

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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