user1094779
user1094779

Reputation: 141

Calling a Macro in a loop?

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

Upvotes: 0

Views: 85

Answers (2)

Tom
Tom

Reputation: 51621

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;

Upvotes: 2

user1094779
user1094779

Reputation: 141

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.

Upvotes: 1

Related Questions