Chapter Contents

Previous

Next
SAS Component Language: Reference

Using Macros

You can use the SAS macro facility to define macros and macro variables for your SCL program. That is, you can use SAS macros and SAS macro variables that have been defined elsewhere in the SAS session or in autocall libraries. You can then pass parameters between macros and the rest of your program. In addition, macros can be used by more than one program. However, macros can be more complicated to maintain than the original program segment because of the symbols and quoting that are required.

If a macro is used by more than one program, you must keep track of all the programs that use the macro so that you can recompile all of them each time the macro is updated. Because SCL is compiled (rather than interpreted like the SAS language), each SCL program that calls a macro must be recompiled whenever that macro is updated in order to update the program with the new macro code.

Macros and macro variables in SCL programs are resolved when you compile the SCL program, not when a user executes the application. However, you can use the SYMGET and SYMGETN functions to retrieve the value of a macro variable or to store a value in a macro variable at execution time, and you can use the SYMPUT and SYMPUTN functions to create a macro variable at execution time. For more information, see Using Macro Variables.

Note:   Macros and macro variables within submit blocks are not resolved when you compile the SCL program. Instead, they are passed with the rest of the submit block to SAS software when the block is submitted. For more information about submit blocks, see Submitting SAS Statements and SQL Statements.  [cautionend]

Note:   Using macros does not reduce the size of the compiled SCL code. Program statements that are generated by a macro are added to the compiled code as if those lines existed at that location in the program.  [cautionend]


Example

The following SCL program uses macros to validate an amount and a rate:

%macro valamnt;
  if amount < 0 or amount > 500 then do;
    erroron amount;
    _msg_='Amount must be between $0 and $500.';
    stop;
  end;
  else erroroff amount;
%mend;
%macro rateamnt;
  if rate<0 or rate>1 then do;
    erroron rate;
    _msg_='Rate must be between 0 and 1.';
    stop;
  end;
  else erroroff rate;
%mend;

INIT:
  control error;
  amount=0;
  rate=.5;
return;

MAIN:
  payment=amount*rate;
return;

TERM:
return;

AMOUNT:
  %valamnt
return;

RATE:
  %rateamnt
return;


Chapter Contents

Previous

Next

Top of Page

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