Processing Macro Variables

SAS Processing

You have seen how to create and reference macro variables. In order to work with macro variables in the programs that you write, you need to understand how macro variables are processed and stored. First, it is important that you understand how SAS processing works.
A SAS program can be any combination of the following:
  • DATA steps and PROC steps
  • global statements
  • SAS Component Language (SCL) code
  • Structured Query Language (SQL) code
  • SAS macro language code.
When you submit a program, it goes to an area of memory called the input stack. This is true for all code that you submit, such as a DATA step, SCL code, or SQL code.
input stack
Once SAS code is in the input stack, SAS
  • reads the text in the input stack (left-to-right, top-to-bottom)
  • routes text to the appropriate compiler upon demand
  • suspends this activity when a step boundary such as a RUN statement is reached
  • executes the compiled code if there are no compilation errors
  • repeats this process for any subsequent steps.
compiler

Tokenization

Between the input stack and the compiler, SAS programs are tokenized into smaller pieces. A component of SAS known as the word scanner divides program text into fundamental units called tokens.
  • Tokens are passed on demand to the compiler.
  • The compiler requests tokens until it receives a semicolon.
  • The compiler performs a syntax check on the statement.
compiler
SAS stops sending statements to the compiler when it reaches a step boundary. Examples of step boundaries include a RUN statement (run;) or the beginning of a new DATA or PROC step. Once the entire step has been compiled, it is executed.
The word scanner recognizes four types of tokens:
  • A literal token is a string of characters that are treated as a unit. The string is enclosed in either single or double quotation marks.
    Examples: "Any text" 'Any text'
  • A number token is a string of numerals that can include a period or E notation (real numbers). Date constants, time constants, datetime constants, and hexadecimal constants are also number tokens.
    Examples: 23 109 '01jan2002'd 5e8 42.7
  • A name token is a string of characters that begins with a letter or underscore and that continues with underscores, letters, or digits. A period can sometimes be part of a name.
    Examples: infile _n_ item3 univariate dollar10.2
  • A special token is any character or group of characters that has a reserved meaning to the compiler.
    Examples: * / + - ** ; $ ( ) . & %
A token ends when the word scanner detects
  • the beginning of another token
  • a blank after a token.
The maximum length of any token is 32,767 characters.

Examples

  • var x1-x10      z   ;
    This example contains six tokens: var x1 - x10 z ;
  • title 'Report for May';
    This example contains three tokens: title 'Report for May' ;

Macro Triggers

Macro variable references and %LET statements are part of the macro language. The macro facility includes a macro processor that is responsible for handling all macro language elements. Certain token sequences, known as macro triggers, alert the word scanner that the subsequent code should be sent to the macro processor.
The word scanner recognizes the following token sequences as macro triggers:
  • % followed immediately by a name token (such as %let)
  • & followed immediately by a name token (such as &amt).
When a macro trigger is detected, the word scanner passes it to the macro processor for evaluation. The macro processor
  • examines these tokens
  • requests additional tokens as necessary
  • performs the action indicated.
For macro variables, the processor does one of the following:
  • creates a macro variable in the symbol table and assigns a value to the variable
  • changes the value of an existing macro variable in the symbol table
  • looks up an existing macro variable in the symbol table and returns the variable's value to the input stack in place of the original reference.
The word scanner then resumes processing tokens from the input stack.
Note: The word scanner does not recognize macro triggers that are enclosed in single quotation marks. Remember that if you need to reference a macro variable within a literal token, such as the title text in a TITLE statement, you must enclose the text string in double quotation marks or the macro variable reference is not resolved.
..................Content has been hidden....................

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