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);
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.)
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.
All variables in a macro that are not macro parameters or explicitly meant to be global should be local, to avoid unwanted side effects.
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.)
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.
Ready to level-up your skills? Choose your own adventure.