Chapter 4: Scope

Scope

The Program Data Vector (PDV)

KEEP and DROP

Scope in Other Programming Blocks

Scope

In programming, scope is the area in which a variable is visible. In other words, scope lets you know where a variable can be accessed. If you write SAS macro functions, you are familiar with local (%local) and global (%global) macro variables. Suppose you have the following code:

%local macVar

The variable is available only in the macro. When the macro completes, the variable is no longer available. Suppose you have the following code:

%global macVar;

This variable is available to all procedures, macros (including macro variables or functions), and DATA steps during the entire SAS session. What sometimes confuses a SAS programmer learning macro programming is the ability to have two macro variables with the same name but with different scopes. One variable is global and one is local. When a macro variable is defined as local in a macro function, its local scope takes precedence over a macro variable with the same name defined as global while the macro is executing. When the macro completes execution, the global macro variable is once again available.

The DATA step has no concept of scope. All variables are accessible from any part of the DATA step. (One exception—if the variable has yet to be assigned a value in the DATA step, accessing the variable results in a missing value!) You could say that variables are global to the DATA step.

In DS2, scope is an attribute of an identifier where identifier refers to a program entity. Here are the program entities:

•   method names

•   functions

•   data names

•   labels

•   program variables

In this book, examples use program variables. Remember that all identifiers are subject to scoping rules.

When scopes are nested, if a variable in an outer scope has the same name as a variable in an inner scope, the variable in the outer scope is hidden by the variable in the inner scope. For example, in the following program, two different variables have the same name, x. The global variables x and y (which are declared outside of all methods) have global scope. The local variable x (declared in the INIT statement in the INIT method) has local scope. Because y is a global variable, it is accessible in all methods.

proc ds2;

data _null_;

declare int x;  /* global x in global scope */

declare int y;  /* global y in global scope */

method init();

   declare int x;  /* local x in local scope */

   x = 5;          /* local x assigned 5 */

   y = 6;          /* global y assigned 6 */

   put '0. in init() ' x= y=;

end; 

method run();

    put '1. in run()  ' x= y=;

         x = 1;

    put '2. in run()  ' x= y=;

end;

method term();

end;

enddata;

run;

quit;

Let’s see what happens when you run this program. The INIT method runs first, assigns a value of 5 to the local variable x, and prints x to the log. After the INIT method runs, the RUN method runs. When the value of x is now printed to the log, it is no longer 5. (It actually has no value.) The value of 5 belonged to the local variable x, which was local to the INIT method. Because the INIT method is no longer active, the local variable x with the value of 5 is no longer available. Here are the new results:

0. in init()  x=5 y=6

1. in run()   x= y=6

2. in run()   x=1 y=6

The Program Data Vector (PDV)

Only global variables are included in the PDV. Because the PDV determines which variables are written to the output data set, any variable that you want to be written to an output data set needs to be global.

KEEP and DROP

Sometimes you want to define a variable as global but you do not want it to be written to the output data set. You can use the KEEP or DROP statement to control variables are written. If more granularity is needed (for example, you want to create two output tables with mutually exclusive sets of columns), you can use the data set keep= or drop= option.

Scope in Other Programming Blocks

Other programming blocks such as packages have the same scoping rules. When a variable has global scope, it really means that the variable is global within its program block. As a result, a variable that is global to the data program block is not accessible by a package instance within the whole program. Similarly, a variable that is global to a package is not accessible by the whole program, although it is accessible by method and attributes. In Chapter 1, table 1.1 lists some of the most common DS2 programming blocks and their scope.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset