0

Here is a simple SAS program I created ...

%MACRO SCANLOOP();
    %DO I=1 %TO 5; 
        %put &I;
    %END;
%MEND;

%MACRO TEST();
    %DO I=1 %TO 3; 
        %SCANLOOP();    
    %END;
%MEND;

%TEST();
RUN;

I was expecting this SAS code to produce the following output:

1
2
3
4
5
1
2
3
4
5
1
2
3
4
5

but instead I just got ...

1
2
3
4
5

Can anyone explain to me why?

Thanks

Brian

2 Answers 2

2

You need to define your macro variables as LOCAL. Otherwise SAS will use the existing macro variable with the same name in the outer scope. For your particular example you must make I local in the SCANLOOP macro. But you should really do it in both.

%MACRO SCANLOOP();
  %LOCAL I;
  %DO I=1 %TO 5; 
    %put &I;
  %END;
%MEND;
Sign up to request clarification or add additional context in comments.

Comments

1

Oh those variables are not scoped the way I expected. If I change the variable in the first macro from I to J then it works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.