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

I figured out a problem I had today because I had nested macros what used macro do loops which both start i=0. Because the inner macro went all the way to 70, when the outside macro referenced i for it's second pass, it thought i was 70 so it didn't need to do another loop.

 

My question is how to use %LOCAL to stop this.

 

When using the local statement, do I need to refer to that variable as &i or &j? Is %LET &j= the right syntax or should it be &j.= %EVAL(&i.+1);

 

Thanks!

 

%LOCAL i j;

 

%DO &i.=0 %TO 70;

 

%LET &j = %EVAL(&i.+1);

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

What you have:

 

%LOCAL i j;

 

%DO &i.=0 %TO 70;

 

%LET &j = %EVAL(&i.+1);

 

What it should be:

 

%LOCAL i j;

 

%DO i=0 %TO 70;

 

%LET j = %EVAL(&i.+1);

 

The correct syntax for referring to a macro variable has nothing at all to do with whether the variable is local or global.  %DO and %LET have the same syntax rules that apply to both local and global variables.

 

Where the %LOCAL statement belongs:  within every macro definition that uses &i or &j.  (There are a few, complex exceptions that you are not likely to encounter.)

View solution in original post

5 REPLIES 5
tomrvincent
Rhodochrosite | Level 12
You should refer to either &i or &j depending on how they are used, since you've defined them both.

I would suggest naming local vars local_i and local_j so they are clearly defined. I don't think the '.' does anything, but I would remove it just in case.
PaigeMiller
Diamond | Level 26

When using the local statement, do I need to refer to that variable as &i or &j? Is %LET &j= the right syntax or should it be &j.= %EVAL(&i.+1);

 

You wouldn't usually use %LET &J= ... ; in most cases the command is %LET J= ...;

 

None of the macro referencing changes because you define a macro variable to be local.

--
Paige Miller
Peter_C
Rhodochrosite | Level 12
All local and outer vars would be available as if global to the "inner" macros unless "overridden" by the inner macro defining that name in a %local statement
Astounding
PROC Star

What you have:

 

%LOCAL i j;

 

%DO &i.=0 %TO 70;

 

%LET &j = %EVAL(&i.+1);

 

What it should be:

 

%LOCAL i j;

 

%DO i=0 %TO 70;

 

%LET j = %EVAL(&i.+1);

 

The correct syntax for referring to a macro variable has nothing at all to do with whether the variable is local or global.  %DO and %LET have the same syntax rules that apply to both local and global variables.

 

Where the %LOCAL statement belongs:  within every macro definition that uses &i or &j.  (There are a few, complex exceptions that you are not likely to encounter.)

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1722 views
  • 0 likes
  • 6 in conversation