The concept of variable
scope can be confusing when local variables in different routines
have the same name. When this occurs, each local variable is distinct.
In the following example, the DATA step and CALL routines
subA
and
subB
contain
a local variable named x. Each x is distinct from the other x variables.
When the program executes, the DATA step calls
subA
and
subA
calls
subB
.
Each environment writes the value of x to the log. The log output
shows how each x is distinct from the others.
proc fcmp outlib=sasuser.funcs.math;
subroutine subA();
x=5;
call subB();
put 'In subA: ' x=;
endsub;
subroutine subB();
x='subB';
put 'In subB: ' x=;
endsub;
run;
options cmplib=sasuser.funcs;
data _null_;
x=99;
call subA();
put 'In DATA step: ' x=;
run;
SAS writes the following
output to the log:
Output from Local Variables in Different Routines Having the
Same Name
In subB: x=subB
In subA: x=5
In DATA step: x=99