Chapter 16

DATA Step Functions for Macros

DATA Step Functions for Macros

Dictionary

RESOLVE Function

SYMEXIST Function

SYMGET Function

SYMGETN Function

SYMGLOBL Function

SYMLOCAL Function

DATA Step Functions for Macros

You can interact with the Macro Facility using DATA step functions.

Dictionary

RESOLVE Function

Resolves the value of a text expression during DATA step execution.

Type:

DATA step function

Syntax

RESOLVE(argument)

Required Argument

argument

can be one of the following items:

• a text expression enclosed in single quotation marks (to prevent the macro processor from resolving the argument while the DATA step is being constructed). When a macro variable value contains a macro variable reference, RESOLVE attempts to resolve the reference. If argument references a nonexistent macro variable, RESOLVE returns the unresolved reference. These examples using text expressions show how to assign the text generated by macro LOCATE or assign the value of the macro variable NAME:

x=resolve('%locate'),
x=resolve('&name'),

• the name of a DATA step variable whose value is a text expression. For example, this example assigns the value of the text expression in the current value of the DATA step variable ADDR1 to X:

addr1='&locate';
x=resolve(addr1);

• a character expression that produces a text expression for resolution by the macro facility. For example, this example uses the current value of the DATA step variable STNUM in building the name of a macro:

x=resolve('%state'||left(stnum));

The RESOLVE function should not reference secure macros.

Details

The RESOLVE function returns a character value that is the maximum length of a DATA step character variable unless you specifically assign the target variable a shorter length. A returned value that is longer is truncated.

If RESOLVE cannot locate the macro variable or macro identified by the argument, it returns the argument without resolution and the macro processor issues a warning message.

You can create a macro variable with the SYMPUT routine and use RESOLVE to resolve it in the same DATA step.

Comparisons

• RESOLVE resolves the value of a text expression during execution of a DATA step or SCL program. Whereas a macro variable reference resolves when a DATA step is being constructed or an SCL program is being compiled. For this reason, the resolved value of a macro variable reference is constant during execution of a DATA step or SCL program. However, RESOLVE can return a different value for a text expression in each iteration of the program.

• RESOLVE accepts a wider variety of arguments than the SYMGET function accepts. SYMGET resolves only a single macro variable but RESOLVE resolves any macro expression. Using RESOLVE might result in the execution of macros and resolution of more than one macro variable.

• When a macro variable value contains an additional macro variable reference, RESOLVE attempts to resolve the reference, but SYMGET does not.

• If argument references a nonexistent macro variable, RESOLVE returns the unresolved reference, whereas SYMGET returns a missing value.

• Because of its greater flexibility, RESOLVE requires slightly more computer resources than SYMGET.

Example: Resolving Sample References

This example shows RESOLVE used with a macro variable reference, a macro invocation, and a DATA step variable whose value is a macro invocation.

%let event=Holiday;
%macro date;
   New Year
%mend date;
data test;
   length var1-var3 $ 15;
   when='%date';
   var1=resolve('&event'), /* macro variable reference */
   var2=resolve('%date'),  /* macro invocation */
   var3=resolve(when);     /* DATA step variable with macro invocation */
   put var1=  var2=  var3=;
run;

When this program executes, these lines are written to the SAS log:

VAR1=Holiday  VAR2=New Year  VAR3=New Year
NOTE: The data set WORK.TEST has 1 observations and 4 variables.

SYMEXIST Function

Returns an indication of the existence of a macro variable.

Type:

DATA step function

Syntax

SYMEXIST(argument)

Required Argument

argument

can be one of the following items:

• the name of a macro variable within quotation marks but without an ampersand

• the name of a DATA step character variable, specified with no quotation marks, which contains a macro variable name

• a character expression that constructs a macro variable name

Details

The SYMEXIST function searches any enclosing local symbol tables and then the global symbol table for the indicated macro variable. The SYMEXIST function returns one of the following values:

1 if the macro variable is found

0 if the macro variable is not found

Example: Using SYMEXIST Function

The following example of the %TEST macro contains the SYMEXIST function:

%global x;

%macro test;
%local y;
data null;
   if symexist("x") then put "x EXISTS";
                          else put "x does not EXIST";
   if symexist("y") then put "y EXISTS";
                          else put "y does not EXIST";
   if symexist("z") then put "z EXISTS";
                          else put "z does not EXIST";
run;
%mend test;
%test;

In the previous example, executing the %TEST macro, which contains the SYMEXIST function, writes the following output to the SAS log:

x EXISTS
y EXISTS
z does not EXIST

SYMGET Function

Returns the value of a macro variable to the DATA step during DATA step execution.

Type:

DATA step function

See:

RESOLVE Function” on page 241, “SYMGETN Function” on page 247, “CALL SYMPUT Routine” on page 234, and “CALL SYMPUTN Routine” on page 238

Syntax

SYMGET(argument)

Required Argument

argument

can be one of the following items:

• the name of a macro variable within quotation marks but without an ampersand. When a macro variable value contains another macro variable reference, SYMGET does not attempt to resolve the reference. If argument references a nonexistent macro variable, SYMGET returns a missing value. This example shows how to assign the value of the macro variable G to the DATA step variable X.

x=symget('g'),

• the name of a DATA step character variable, specified with no quotation marks, which contains names of one or more macro variables. If the value is not a valid SAS name, or if the macro processor cannot find a macro variable of that name, SAS writes a note to the log that the function has an invalid argument and sets the resulting value to missing. For example, these statements assign the value stored in the DATA step variable CODE, which contains a macro variable name, to the DATA step variable KEY:

length key $ 8;

input code $;
key=symget(code);

Each time the DATA step iterates, the value of CODE supplies the name of a macro variable whose value is then assigned to KEY.

• a character expression that constructs a macro variable name. For example, this statement assigns the letter s and the number of the current iteration (using the automatic DATA step variable _N_).

score=symget('s'||left(_n_));

Details

SYMGET returns a character value that is the maximum length of a DATA step character variable. A returned value that is longer is truncated.

If SYMGET cannot locate the macro variable identified as the argument, it returns a missing value, and the program issues a message for an invalid argument to a function.

SYMGET can be used in all SAS language programs, including SCL programs. Because it resolves variables at program execution instead of macro execution, SYMGET should be used to return macro values to DATA step views, SQL views, and SCL programs.

Comparisons

• SYMGET returns values of macro variables during program execution, whereas the SYMPUT function assigns values that are produced by a program to macro variables during program execution.

• SYMGET accepts fewer types of arguments than the RESOLVE function. SYMGET resolves only a single macro variable. Using RESOLVE might result in the execution of macros and further resolution of values.

• SYMGET is available in all SAS programs, but SYMGETN is available only in SCL programs.

Example: Retrieving Variable Values Previously Assigned from a Data Set

data dusty;
   input dept $ name $ salary @@;
   datalines;
bedding Watlee 18000    bedding Ives 16000
bedding Parker 9000     bedding George 8000
bedding Joiner 8000     carpet Keller 20000
carpet Ray 12000        carpet Jones 9000
gifts Johnston 8000     gifts Matthew 19000
kitchen White 8000      kitchen Banks 14000
kitchen Marks 9000      kitchen Cannon 15000
tv Jones 9000           tv Smith 8000
tv Rogers 15000         tv Morse 16000
;
proc means noprint;
   class dept;
   var salary;
   output out=stats sum=s_sal;
run;

proc print data=stats;
   var dept s_sal;
   title "Summary of Salary Information";
   title2 "For Dusty Department Store";
run;
data _null_;
   set stats;
   if _n_=1 then call symput('s_tot',s_sal);
   else call symput('s'||dept,s_sal);
run;
data new;
   set dusty;
   pctdept=(salary/symget('s'||dept))*100;
   pcttot=(salary/&s_tot)*100;
run;
proc print data=new split="*";
   label dept  ="Department"
         name  ="Employee"
         pctdept="Percent of *Department* Salary"
         pcttot ="Percent of *  Store  * Salary";
   format pctdept pcttot 4.1;
   title  "Salary Profiles for Employees";
   title2 "of Dusty Department Store";
run;

This program produces the following output:

Output 16.1 Summary of Salary Information

Image

Output 16.2 Salary Profiles for Employees

Image

SYMGETN Function

In SAS Component Control Language (SCL) programs, returns the value of a global macro variable as a numeric value.

Type:

SCL function

See:

SYMGET Function” on page 244, “CALL SYMPUT Routine” on page 234, and “CALL SYMPUTN Routine” on page 238

Syntax

SCL-variable=SYMGETN(’macro-variable’);

Required Arguments

SCL variable

is the name of a numeric SCL variable to contain the value stored in macro-variable.

macro-variable

is the name of a global macro variable with no ampersand – note the single quotation marks. Or, the name of an SCL variable that contains the name of a global macro variable.

Details

SYMGETN returns the value of a global macro variable as a numeric value and stores it in the specified numeric SCL variable. You can also use SYMGETN to retrieve the value of a macro variable whose name is stored in an SCL variable. For example, to retrieve the value of SCL variable UNITVAR, whose value is ’UNIT’, submit the following code:

unitnum=symgetn(unitvar)

SYMGETN returns values when SCL programs execute. If SYMGETN cannot locate macro-variable, it returns a missing value.

To return the value stored in a macro variable when an SCL program compiles, use a macro variable reference in an assignment statement:

SCL variable=&macro-variable;

Note: It is inefficient to use SYMGETN to retrieve values that are not assigned with SYMPUTN and values that are not numeric.

Comparisons

• SYMGETN is available only in SCL programs, but SYMGET is available in DATA step programs and SCL programs.

• SYMGETN retrieves values, but SYMPUTN assigns values.

Example: Storing a Macro Variable Value as a Numeric Value in an SCL Program

This statement stores the value of the macro variable UNIT in the SCL variable UNITNUM when the SCL program executes:

unitnum=symgetn('unit'),

SYMGLOBL Function

Returns an indication as to whether a macro variable is global in scope to the DATA step during DATA step execution.

Type:

DATA step function

Syntax

SYMGLOBL(argument)

Required Argument

argument

can be one of the following items:

• the name of a macro variable within quotation marks but without an ampersand

• the name of a DATA step character variable, specified with no quotation marks, that contains a macro variable name

• a character expression that constructs a macro variable name

Details

The SYMGLOBL function searches enclosing scopes for the indicated macro variable and returns a value of 1 if the macro variable is found in the global symbol table, otherwise it returns a 0. See “Scopes of Macro Variables” on page 47 for more information about the global and local symbol tables and macro variable scopes.

Example: Using SYMGLOBL Function

The following example of the %TEST macro contains the SYMGLOBL function:

%global x;
      %macro test;
      %local y;
      data null;
         if symglobl("x") then put "x is GLOBAL";
                                else put "x is not GLOBAL";
         if symglobl("y") then put "y is GLOBAL";
                                else put "y is not GLOBAL";
         if symglobl("z") then put "z is GLOBAL";
                                else put "z is not GLOBAL";
run;
%mend test;
%test;

In the previous example, executing the %TEST macro, which contains the SYMGLOBL function, writes the following output to the SAS log:

x is GLOBAL
y is not GLOBAL
z is not GLOBAL

SYMLOCAL Function

Returns an indication as to whether a macro variable is local in scope to the DATA step during DATA step execution.

Type:

DATA step function

Syntax

SYMLOCAL(argument)

Required Argument

argument

can be one of the following items:

• the name of a macro variable within quotation marks but without an ampersand

• the name of a DATA step character variable, specified with no quotation marks, that contains a macro variable name

• a character expression that constructs a macro variable name

Details

The SYMLOCAL function searches enclosing scopes for the indicated macro variable and returns a value of 1 if the macro variable is found in a local symbol table, otherwise it returns a 0. See “Scopes of Macro Variables” on page 47 for more information about the global and local symbol tables and macro variable scopes.

Example: Using SYMLOCAL Function

The following example of the %TEST macro contains the SYMLOCAL function:

%global x;
     %macro test;
     %local y;
     data null;
        if symlocal("x") then put "x is LOCAL";
                               else put "x is not LOCAL";
        if symlocal("y") then put "y is LOCAL";
                               else put "y is not LOCAL";
        if symlocal("z") then put "z is LOCAL";
                               else put "z is not LOCAL";
run;
%mend test;
%test;

In the previous example, executing the %TEST macro, which contains the SYMLOCAL function, writes the following output to the SAS log:

x is not LOCAL
y is LOCAL
z is not LOCAL

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

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