Chapter 15

DATA Step Call Routines for Macros

DATA Step Call Routines for Macros

You can interact with the Macro Facility using DATA step call routines.

Dictionary

CALL EXECUTE Routine

Resolves the argument, and issues the resolved value for execution at the next step boundary.

Type:

DATA step call routine

Syntax

CALL EXECUTE(argument);

Required Argument

argument

can be one of the following:

• a character string, enclosed in quotation marks. Argument within single quotation marks resolves during program execution. Argument within double quotation marks resolves while the DATA step is being constructed. For example, to invoke the macro SALES, you can use the following code:

call execute('%sales'),

• the name of a DATA step character variable whose value is a text expression or a SAS statement to be generated. Do not enclose the name of the DATA step variable in quotation marks. For example, to use the value of the DATA step variable FINDOBS, which contains a SAS statement or text expression, you can use the following code:

call execute(findobs);

• a character expression that is resolved by the DATA step to a macro text expression or a SAS statement. For example, to generate a macro invocation whose parameter is the value of the variable MONTH, you use the following code:

call execute('%sales('||month||')'),

Details

If an EXECUTE routine argument is a macro invocation or resolves to one, the macro executes immediately. Execution of SAS statements generated by the execution of the macro will be delayed until after a step boundary. SAS macro statements, including macro variable references, will execute immediately.

Note: Because of the delay of the execution of the SAS statements until after a step boundary, references in SAS macro statements to macro variables created or updated by the SAS statements will not resolve properly.

Note: Macro references execute immediately and SAS statements do not execute until after a step boundary. You cannot use CALL EXECUTE to invoke a macro that contains references for macro variables that are created by CALL SYMPUT in that macro. For a workaround, see the following TIP.

TIP The following example uses the %NRSTR macro quoting function to mask the macro statement. This function will delay the execution of macro statements until after a step boundary.

call execute('%nrstr(%sales('||month||'))'),

Comparisons

Unlike other elements of the macro facility, a CALL EXECUTE statement is available regardless of the setting of the SAS system option MACRO | NOMACRO. In both cases, EXECUTE places the value of its argument in the program stack. However, when NOMACRO is set, any macro calls or macro functions in the argument are not resolved.

Examples

Example 1: Executing a Macro Conditionally

The following DATA step uses CALL EXECUTE to execute a macro only if the DATA step writes at least one observation to the temporary data set.

%macro overdue;
   proc print data=late;
      title "Overdue Accounts As of &sysdate";
   run;
%mend overdue;
data late;
   set Sasuser.Billed end=final;
   if datedue<=today()-30 then

   do;
      n+1;
         output;
      end;
   if final and n then call execute('%overdue'),
run;

Example 2: Passing DATA Step Values into a Parameter List

CALL EXECUTE passes the value of the DATE variable in the Dates data set to macro REPT for its DAT parameter, the value of the VAR1 variable in the REPTDATA data set for its A parameter, and REPTDATA as the value of its DSN parameter. After the DATA _NULL_ step finishes, three PROC GCHART statements are submitted, one for each of the three dates in the Dates data set.

data dates;
   input date $;
datalines;
10nov11
11nov11
12nov11
;
data reptdata;
   input date $ var1 var2;
datalines;
10nov11 25 10
10nov11 50 11
11nov11 23 10
11nov11 30 29
12nov11 33 44
12nov11 75 86
;
%macro rept(dat,a,dsn);
   proc chart data=&dsn;
       title "Chart for &dat";
       where(date="&dat");
       vbar &a;
   run;
%mend rept;
data _null_;
   set dates;
   call execute('%rept('||date||','||'var1,reptdata)'),
run;

CALL SYMDEL Routine

Deletes the specified variable from the macro global symbol table.

Type:

DATA step call routine

Syntax

CALL SYMDEL(macro-variable<, option>);

Required Arguments

macro-variable

can be any of the following:

• the name of a macro variable within quotation marks but without an ampersand. When a macro variable value contains another macro variable reference, SYMDEL does not attempt to resolve the reference.

• the name of a DATA step character variable, specified with no quotation marks, which contains the name of a macro variable. 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 warning to the log.

• a character expression that constructs a macro variable name.

option

NOWARN

suppresses the warning message when an attempt is made to delete a non-existent macro variable. NOWARN must be within quotation marks.

Details

CALL SYMDEL issues a warning when an attempt is made to delete a non-existent macro variable. To suppress this message, use the NOWARN option.

CALL SYMPUT Routine

Assigns a value produced in a DATA step to a macro variable.

Type:

DATA step call routine

See:

SYMGET Function” on page 244 and “CALL SYMPUTX Routine” on page 239

Syntax

CALL SYMPUT(macro-variable, value);

Required Arguments

macro-variable

can be one of the following items:

• a character string that is a SAS name, enclosed in quotation marks. For example, to assign the character string testing to macro variable NEW, submit the following statement:

call symput('new','testing'),

• the name of a character variable whose values are SAS names. For example, this DATA step creates the three macro variables SHORTSTP, PITCHER, and FRSTBASE and respectively assign them the values ANN, TOM, and BILL.

data team1;
   input position : $8. player : $12.;
   call symput(position,player);
datalines;
shortstp Ann
pitcher Tom

frstbase Bill
;

• a character expression that produces a macro variable name. This form is useful for creating a series of macro variables. For example, the CALL SYMPUT statement builds a series of macro variable names by combining the character string POS and the left-aligned value of _N_ . Values are assigned to the macro variables POS1, POS2, and POS3.

data team2;
   input position : $12. player $12.;
   call symput('POS'||left(_n_), position);
   datalines;
shortstp Ann
pitcher Tom
frstbase Bill
;

value

is the value to be assigned, which can be

• a string enclosed in quotation marks. For example, this statement assigns the string testing to the macro variable NEW:

call symput('new','testing'),

• the name of a numeric or character variable. The current value of the variable is assigned as the value of the macro variable. If the variable is numeric, SAS performs an automatic numeric-to-character conversion and writes a message in the log. Later sections on formatting rules describe the rules that SYMPUT follows in assigning character and numeric values of DATA step variables to macro variables.

Note: This form is most useful when macro-variable is also the name of a SAS variable or a character expression that contains a SAS variable. A unique macro variable name and value can be created from each observation, as shown in the previous example for creating the data set Team1.

If macro-variable is a character string, SYMPUT creates only one macro variable, and its value changes in each iteration of the program. Only the value assigned in the last iteration remains after program execution is finished.

• a DATA step expression. The value returned by the expression in the current observation is assigned as the value of macro-variable. In this example, the macro variable named HOLDATE receives the value July 4,1997:

data c;
   input holiday mmddyy.;
   call symput('holdate',trim(left(put(holiday,worddate.))));
datalines;
070497
;
run;

If the expression is numeric, SAS performs an automatic numeric-to-character conversion and writes a message in the log. Later sections on formatting rules describe the rules that SYMPUT follows in assigning character and numeric values of expressions to macro variables.

Details

If macro-variable does not exist, SYMPUT creates it. SYMPUT makes a macro variable assignment when the program executes.

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

Scope of Variables Created with SYMPUT

SYMPUT puts the macro variable in the most local nonempty symbol table. A symbol table is nonempty if it contains the following:

• a value

• a computed %GOTO (A computed %GOTO contains % or & and resolves to a label.)

• the macro variable &SYSPBUFF, created at macro invocation time.

However, there are three cases where SYMPUT creates the variable in the local symbol table, even if that symbol table is empty:

• Beginning with Version 8, if SYMPUT is used after a PROC SQL, the variable will be created in a local symbol table.

• If an executing macro contains a computed %GOTO statement and uses SYMPUT to create a macro variable, the variable is created in the local symbol table.

• If an executing macro uses &SYSPBUFF and SYMPUT to create a macro variable, the macro variable is created in the local symbol table.

For more information about creating a variable with SYMPUT, see “Scopes of Macro Variables” on page 47.

Problem Trying to Reference a SYMPUT-Assigned Value Before It Is Available

One of the most common problems in using SYMPUT is trying to reference a macro variable value assigned by SYMPUT before that variable is created. The failure generally occurs because the statement referencing the macro variable compiles before execution of the CALL SYMPUT statement that assigns the variable’s value. The most important fact to remember in using SYMPUT is that it assigns the value of the macro variable during program execution. Macro variable references resolve during the compilation of a step, a global statement used outside a step, or an SCL program. As a result:

• You cannot use a macro variable reference to retrieve the value of a macro variable in the same program (or step) in which SYMPUT creates that macro variable and assigns it a value.

• You must specify a step boundary statement to force the DATA step to execute before referencing a value in a global statement following the program (for example, a TITLE statement). The boundary could be a RUN statement or another DATA or PROC statement. For example:

data x;
   x='December';
   call symput('var',x);
proc print;
title "Report for &var";
run;

Processing on page 37 provides details about compilation and execution.

Formatting Rules For Assigning Character Values

If value is a character variable, SYMPUT writes it using the $w. format, where w is the length of the variable. Therefore, a value shorter than the length of the program variable is written with trailing blanks. For example, in the following DATA step the length of variable C is 8 by default. Therefore, SYMPUT uses the $8. format and assigns the letter x followed by seven trailing blanks as the value of CHAR1. To eliminate the blanks, use the TRIM function as shown in the second SYMPUT statement.

data char1;
   input c $;
   call symput('char1',c);
   call symput('char2',trim(c));
   datalines;
x
;
run;
%put char1 = ***&char1***;
%put char2 = ***&char2***;

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

char1 = ***x  ***
char2 = ***x***

Formatting Rules For Assigning Numeric Values

If value is a numeric variable, SYMPUT writes it using the BEST12. format. The resulting value is a 12-byte string with the value right-aligned within it. For example, this DATA step assigns the value of numeric variable X to the macro variables NUM1 and NUM2. The last CALL SYMPUT statement deletes undesired leading blanks by using the LEFT function to left-align the value before the SYMPUT routine assigns the value to NUM2.

data _null_;
   x=1;
   call symput('num1',x);
   call symput('num2',left(x));
   call symput('num3',trim(left(put(x,8.)))); /*preferred technique*/
run;
%put num1 = ***&num1***;
%put num2 = ***&num2***;
%put num3 = ***&num3***;

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

num1 = ***          1***
num2 = ***1          ***
num3 = ***1***

Comparisons

• SYMPUT assigns values produced in a DATA step to macro variables during program execution, but the SYMGET function returns values of macro variables to the program during program execution.

• SYMPUT is available in DATA step and SCL programs, but SYMPUTN is available only in SCL programs.

• SYMPUT assigns character values, but SYMPUTN assigns numeric values.

Example: Creating Macro Variables and Assigning Them Values 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;
data _null_;
   set stats;
   if _n_=1 then call symput('s_tot',trim(left(s_sal)));
   else call symput('s'||dept,trim(left(s_sal)));
run;
%put _user_;

When this program executes, this list of variables is written to the SAS log:

GLOBAL SCARPET 41000
GLOBAL SKITCHEN 46000
GLOBAL STV 48000
GLOBAL SGIFTS 27000
GLOBAL SBEDDING 59000
GLOBAL S_TOT 221000

CALL SYMPUTN Routine

In SCL programs, assigns a numeric value to a global macro variable.

Type:

SCL call routine

See:

SYMGET Function” on page 244, “SYMGETN Function” on page 247, and “CALL SYMPUT Routine” on page 234

Syntax

CALL SYMPUTN(’macro-variable’, value);

Required Arguments

macro-variable

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

value

is the numeric value to assign, which can be a number or the name of a numeric SCL variable.

Details

The SYMPUTN routine assigns a numeric value to a global SAS macro variable. SYMPUTN assigns the value when the SCL program executes. You can also use SYMPUTN to assign the value of a macro variable whose name is stored in an SCL variable. For example, to assign the value of SCL variable UNITNUM to SCL variable UNITVAR, which contains ’UNIT’, submit the following:

call symputn(unitvar,unitnum)

You must use SYMPUTN with a CALL statement.

Note: It is inefficient to use an ampersand (&) to reference a macro variable that was created with CALL SYMPUTN. Instead, use SYMGETN. It is also inefficient to use CALL SYMPUTN to store a variable that does not contain a numeric value.

Comparisons

• SYMPUTN assigns numeric values, but SYMPUT assigns character values.

• SYMPUTN is available only in SCL programs, but SYMPUT is available in DATA step programs and SCL programs.

• SYMPUTN assigns numeric values, but SYMGETN retrieves numeric values.

Example: Storing the Value 1000 in the Macro Variable UNIT When the SCL Program Executes

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

call symputn('unit',1000);

CALL SYMPUTX Routine

Assigns a value to a macro variable, and removes both leading and trailing blanks.

Category:

Macro

See:

CALL SYMPUTX Routine” in SAS Functions and CALL Routines: Reference

Syntax

CALL SYMPUTX(macro-variable, value <, symbol-table>);

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

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