Chapter Contents


SAS Macro Language: Reference

Interfaces with Screen Control Language

You can use the SAS macro facility to define macros and macro variables for your SCL program. Then, you can pass parameters between macros and the rest of your program. Also, through the use of the autocall and compiled stored macro facilities, macros can be used by more than one SCL program.

Note:   Macro modules can be more complicated to maintain than a program segment because of the symbols and macro quoting that may be required. Also, implementing modules as macros does not reduce the size of the compiled SCL code. Program statements generated by a macro are added to the compiled code as if those lines existed at that location in the program.  [cautionend]

SCL Interfaces to the Macro Facility lists the SCL macro facility interfaces.

SCL Interfaces to the Macro Facility
Category Tool Description
Read or Write SYMGET returns the value of a global macro variable during SCL execution

SYMGETN returns the value of a global macro variable as a numeric value

CALL SYMPUT assigns a value produced in SCL to a global macro vairable

CALL SYMPUTN assigns a numeric value to a global macro variable

Note:   It is inefficient to use SYMGETN to retrieve values that are not assigned with SYMPUTN. It is also inefficient to use & to reference a macro variable that was created with CALL SYMPUTN. Instead, use SYMGETN. In addition, it is inefficient to use SYMGETN and CALL SYMPUTN with values that are not numeric.  [cautionend]

For details on these elements, see Chapter 13.

Understanding How Macro References Are Resolved by SCL

An important point to remember when using the macro facility with SCL is that macros and macro variable references in SCL programs are resolved when the SCL program compiles, not when you execute the application. To further control the assignment and resolution of macros and macro variables, use the following techniques:

Referencing Macro Variables in Submit Blocks

In SCL, macro variable references are resolved at compile time unless they are in a Submit block. When SCL encounters a name prefixed with an ampersand (&) in a Submit block, it checks whether the name following the ampersand is the name of an SCL variable. If so, SCL substitutes the value of the corresponding variable for the variable reference in the submit block. If the name following the ampersand does not match any SCL variable, the name passes intact (including the ampersand) with the submitted statements. When the SAS System processes the statements, it attempts to resolve the name as a macro variable reference

To guarantee that a name is passed as a macro variable reference in submitted statements, precede the name with two ampersands (for example, &&DSNAME). If you have both a macro variable and an SCL variable with the same name, a reference with a single ampersand substitutes the SCL variable. To force the macro variable to be substituted, reference it with two ampersands (&&).

Considerations for Sharing Macros between SCL Programs

Sharing macros between SCL programs can be useful, but it can also raise some configuration management problems. If a macro is used by more than one program, you must keep track of all the programs that use it so you can recompile all of them each time the macro is updated. Because SCL is compiled, each SCL program that calls a macro must be recompiled whenever that macro is updated to update the program with the new macro code.

Recompile the SCL program. If you fail to recompile the SCL program when you update the macro, you run the risk of the compiled SCL being out of sync with the source.  [cautionend]

Example Using Macros in an SCL Program

This SCL program is for an example application with the fields BORROWED, INTEREST, and PAYMENT. The program uses the macros CKAMOUNT and CKRATE to validate values entered into fields by users. The program calculates the payment, using values entered for the interest rate (INTEREST) and the sum of money (BORROWED).

/* Display an error message if AMOUNT */
   /* is less than zero or larger than 1000. */
%macro ckamount(amount);
   if (&amount < 0) or (&amount > 1000) then
         erroron borrowed;
         _msg_='Amount must be between $0 and $1,000.';
   else erroroff borrowed;
%mend ckamount;

   /* Display an error message if RATE */
   /* is less than 0 or greater than 1.5 */
%macro ckrate(rate);
   if (&rate < 0) or (&rate > 1) then
         erroron interest;
         _msg_='Rate must be between 0 and 1.5';
   else erroroff interest;
%mend ckrate;

   /*  Open the window with BORROWED at 0 and INTEREST at .5.  */
   control error;

      /*  Run the macro CKAMOUNT to validate  */
      /*  the value of BORROWED.              */
      /*  Run the macro CKRATE to validate  */
      /* the value of INTEREST.             */
      /*  Calculate payment.  */


Chapter Contents



Top of Page

Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.