SlideShare a Scribd company logo
SAS/MACROS
Chapter 1. Overview of the Macro Facility Venkata Maguluri
Applications of the Macro Facility Section 1.1 Applications of the Macro Facility
Objectives Identify different applications of the SAS  macro facility. Applications of the Macro Facility
Purpose of the Macro Facility Using the macro language, you can write SAS programs that are  dynamic , or capable of self-modification. Specifically, the macro language enables you to create and resolve  macro variables   anywhere in a SAS program write special programs ( macros ) that generate tailored SAS code. Applications of the Macro Facility
Displaying System Information Using the macro language, you can utilize  automatic macro variables  that contain information regarding your system environment. For example, some of these variables contain date and time of SAS session version of SAS operating system. Applications of the Macro Facility
Displaying System Information 1 2 2 2 2 1 3 5 4 5 2 3 4 1 time of day day of week date (day, month, and year) operating system release of SAS software Applications of the Macro Facility
Substitute Information Multiple  Times The macro facility can substitute the same user-defined information into multiple locations within a single program. Example: Substitute a four-digit year of interest into multiple locations in a program. proc print data=perm.schedule; where year(begin_date)= Year-of-Interest  ; title "Scheduled Classes for  Year-of-Interest   "; run; proc means data=perm.all sum; where year(begin_date)=  Year-of-Interest  ; class location; var fee; title "Total Fees for  Year-of-Interest  Classes"; title2 "by Training Center"; run; Applications of the Macro Facility
Conditional Processing The macro facility controls whether certain portions of a SAS program are processed based on specific conditions. Example: Generate the detailed registration report on a daily basis, but generate the revenue summary report only on Friday. Applications of the Macro Facility
Conditional Processing Is it Friday? Yes Always Print  the  Daily Report Applications of the Macro Facility
Repetitive Processing The macro facility generates portions of a SAS program repetitively, making each iteration perform differently. Example: Generate the same summary report for each year between 2000 and 2002. Applications of the Macro Facility
Repetitive Processing Applications of the Macro Facility
Repetitive Processing Applications of the Macro Facility
Data-driven Applications To summarize a different time period on an enrollment report, certain portions of the report require modification : Starting date - explicitly specified by user Ending date - explicitly specified by user Total number of students during time period - dynamically determined Average class size during time period - dynamically determined. 1 2 3 4 Applications of the Macro Facility
Data-driven Applications 4 1 2 3 Applications of the Macro Facility
Tips on Writing Macro-based Programs If a macro-based program is used to generate SAS code, write and debug the desired SAS program without any macro coding make sure the SAS program runs with hard-coded programming constants on a fixed set of data. Program Flow
Section 1.2 Program Flow
Objectives Identify the tokens in a SAS program. Describe how a SAS program is tokenized, compiled, and executed. Program Flow
Compilation and Execution A SAS program can be any combination of DATA steps and PROC steps global statements Screen Control Language (SCL) Structured Query Language (SQL) SAS macro language. When you submit a program, it goes to an area of memory called the  input stack . Program Flow
Compilation and Execution Input Stack data new; set perm.mast; bonus=wage*1.1; run; proc print; run; libname perm ‘.’; options nodate; proc sql; select * from perm.mast; MAIN: erroroff wage; if wage gt 20 then erroron wage; return; Display Manager SUBMIT  Command SCL COMPILE Command Batch or Noninteractive Submission Program Flow
Compilation and Execution Once SAS code is in the input stack, the SAS System 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 is reached executes the compiled code if there are no compilation errors repeats this process as necessary. Program Flow
Compilation and Execution data new; set perm.mast; bonus=wage *1.1; run; proc print; run; Compiler SAS System Input Stack Program Flow
Tokenization A component of SAS known as the  word scanner   breaks program text into fundamental units called   token s . 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. Program Flow
Tokenization How does SAS know when to stop sending statements  to the compiler? How would processing be affected if the RUN statement were omitted for a noninteractive submission an interactive submission? Compiler data new; SAS Word Scanner Input Stack bonus=wage*1.1;  proc print;  run; set perm . mast ; Program Flow
Tokenization The word scanner recognizes four classes of tokens: literals are a string of characters treated as a unit. The string is enclosed in single quotes or double quotes. ’ Any text’  "Any text" numbers are a string of digits (integers). Date, time,  datetime constants, and hexadecimal constants are also integer tokens. 23  3  100  ’01jan2002’d are strings of digits that also include a period or E-notation (real numbers). 23.5  3.  .11  5E8  7.2E-4 Program Flow
names consist of a string of characters beginning with a letter or underscore and continuing with underscores, letters, or digits. (A period can sometimes be part of a name.)  infile  _n_  item3  univariate  dollar10.2 special is any character or group of characters that have reserved meaning to the compiler. *  /  +  -  **  ;  $  (  )  .  &  % 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  32767  characters. Tokenization Program Flow
Tokenization: Examples 1. Blanks are not tokens. One or more blanks only serve to separate tokens. 2. The text of a literal is treated as a unit  by the  word scanner  when it is enclosed in single quotes. Input Stack Tokens  var   x1-x10   z  ;   (1) VAR  (2) X1  (3)  -  (4) X10  (5) Z  (6)  ; Input Stack Tokens   title 'Report for May'; (1) TITLE  (2) 'Report for May'  (3) ; Program Flow
Tokenization: Exercise 1.1 How many tokens are present in each of these  statements? input @10 ssn comma11. name $30-50; bonus=3.2*(wage-2000); plot date*revenue='$'/vref='30jun2001'd; Program Flow
Tokenization: Exercise Answers How many tokens are present in each of these  statements? input @10 ssn comma11. name $30-50; bonus=3.2*(wage-2000); plot date*revenue=‘$’/vref=‘30jun2001’d; 11 10 11 Program Flow
Section 1.3 Macro Processing
Objectives Describe how the macro processor affects program flow. Micro Processing
Macro Triggers The  macro processor   is a part of the macro facility that acts upon certain token sequences detected during word scanning: %  followed by a name token (example:  %let ) &  followed by a name token (example:  &amt ) Each of these token sequences is called a  macro trigge r . Micro Processing
Macro Triggers When the word scanner encounters a macro trigger, the macro processor examines those tokens requests additional tokens if necessary  performs the action indicated. Micro Processing
How the Macro Processor Works Macro Processor Input Stack SYSDAY SYSLAST Tuesday _NULL_ Symbol Table ... Compiler Word Scanner %let amt=1.1; data new; set perm.mast; bonus=wage*&amt; proc print; run; Micro Processing
Macro Processor SYSDAY SYSLAST Tuesday _NULL_ Symbol Table When a macro trigger is encountered, it is passed to the macro processor for evaluation. %let ... Input Stack How the Macro Processor Works Compiler Word Scanner amt=1.1; data new; set perm.mast; bonus=wage*&amt; proc print; run; Micro Processing
How the Macro Processor Works Macro Processor data new; set perm.mast; bonus=wage*&amt; proc print; run; %let  amt=1.1;   %LET is the keyword of a macro statement that creates a macro variable. The macro processor requests tokens until a semicolon is encountered. ... Compiler Word Scanner Input Stack Micro Processing SYSDAY SYSLAST Tuesday _NULL _ Symbol Table
When the %LET statement executes, a macro variable AMT is given the value  1.1  and stored in a memory location called a  symbol tabl e .  ... Macro Processor Input Stack Compiler Word Scanner data new; set perm.mast; bonus=wage*&amt; proc print; run; How the Macro Processor Works Micro Processing SYSDAY SYSLAST AMT Tuesday _NULL _ 1.1 Symbol Table
; proc print; run; Word scanning continues until another macro trigger is found. data new; set perm.mast; bonus=wage* &amt ... Macro Processor Compiler Word Scanner Input Stack How the Macro Processor Works Micro Processing SYSDAY SYSLAST AMT Tuesday _NULL_ 1.1 Symbol Table
The trigger  &amt  is called a  macro variable reference . The macro processor attempts to find the AMT variable in the symbol table. ... Macro Processor Compiler Word Scanner Input Stack data new; set perm.mast; bonus=wage* ; proc print; run; &amt How the Macro Processor Works Micro Processing SYSDAY SYSLAST AMT Tuesday _NULL_ 1.1 Symbol Table
Word scanning continues. If the AMT variable is found, its value is passed back to the input stack. ... Compiler Word Scanner Input Stack Macro Processor data new; set perm.mast; bonus=wage* 1.1 ; proc print; run; How the Macro Processor Works Micro Processing SYSDAY SYSLAST AMT Tuesday _NULL_ 1.1 Symbol Table
Macro Processor When a step boundary is recognized, the DATA step compilation phase ends and execution begins. ; run; proc print ... Compiler Word Scanner Input Stack data new; set perm.mast; bonus=wage*1.1 How the Macro Processor Works Micro Processing SYSDAY SYSLAST AMT Tuesday _NULL_ 1.1 Symbol Table
The macro processor may write messages to the SAS log if it cannot act upon a macro trigger. Example: Suppose the macro variable reference was coded as  &ant  instead of  &amt . Macro Processor &ant WARNING: Apparent symbolic reference ANT not resolved. Input Stack data new; set perm.mast; bonus=wage* Compiler Word Scanner ; proc print; run; How the Macro Processor Works Micro Processing SYSDAY SYSLAST AMT Tuesday _NULL_ 1.1 Symbol Table
If the macro processor cannot interpret the trigger, 1. it passes the tokens back to the word scanner  2. the word scanner passes them to the compiler 3. the DATA step compiler writes an error message.  ERROR: Expecting a variable name. ... Compiler Word Scanner Input Stack data new; set perm.mast; bonus=wage *&amt ; ; proc print; run; Macro Processor How the Macro Processor Works Micro Processing SYSDAY SYSLAST AMT Tuesday _NULL_ 1.1 Symbol Table
The %INCLUDE Statement The special token pair  %include  requests that SAS statements stored in an external file be inserted at that location in the input stack. External File %let amt=1.1; data new; set perm.mast; bonus=wage*&amt; Input Stack %include ' external-file '; proc print; run; Micro Processing
file-specification describes the location of the SAS code to be inserted: ' external-file ' is the physical name of the file. fileref  is the file reference supplied  through a host command or  FILENAME statement. SOURCE2 causes the inserted SAS statements  to appear in the SAS log. The %INCLUDE statement retrieves SAS source code from an external file. General form of the %INCLUDE statement: %INCLUDE  file-specification  <  /  SOURCE2   >; The %INCLUDE Statement Micro Processing
The word scanner encounters the  %  and  include  tokens, passes them to the macro processor that passes the tokens to the %INCLUDE handling routines. External File %let amt=1.1; data new; set perm.mast; bonus=wage*&amt; Word  Scanner Macro Processor %INCLUDE Handling  Routines ‘ external file’; proc print; run;  %include Input Stack ... The %INCLUDE Statement Micro Processing
The %INCLUDE handling routines obtain the external file specification. External File %let amt=1.1; data new; set perm.mast; bonus=wage*&amt; Word  Scanner Macro Processor %INCLUDE Handling  Routines proc print; run;  %include ‘ external file ’; Input Stack ... The %INCLUDE Statement Micro Processing
The contents of the external file are placed into the input stack. The word scanner begins to read the newly inserted statements. External File %let amt=1.1; data new; set perm.mast; bonus=wage*&amt; Word  Scanner Macro Processor %let amt=1.1; data new; set perm.mast; bonus=wage*&amt; proc print; run;  Input Stack Symbol Table SYSDAY  Tuesday SYSLAST  _NULL_ ... The %INCLUDE Statement Micro Processing
 

More Related Content

PPT
SAS Macros part 2
PDF
Introduction to SAS
PDF
Sas cheat
PDF
SAS cheat sheet
PPT
SAS Macros
PPT
Base SAS Statistics Procedures
PPT
Basics Of SAS Programming Language
DOCX
Sas practice programs
SAS Macros part 2
Introduction to SAS
Sas cheat
SAS cheat sheet
SAS Macros
Base SAS Statistics Procedures
Basics Of SAS Programming Language
Sas practice programs

What's hot (20)

DOCX
Learn SAS Programming
PPT
Data Match Merging in SAS
DOCX
Base sas interview questions
PPTX
SAS Macro
PPT
Arrays in SAS
PDF
Introduction To Sas
PDF
Proc report
PPT
Understanding SAS Data Step Processing
PPT
SAS BASICS
PPTX
Data mining tasks
PPTX
Big data frameworks
PPTX
Data warehouse architecture
PPT
SAS Access / SAS Connect
PDF
View & index in SQL
PPTX
Database architecture
PPTX
pl/sql Procedure
PPTX
PPT
Utility Procedures in SAS
PPTX
3 Data Mining Tasks
Learn SAS Programming
Data Match Merging in SAS
Base sas interview questions
SAS Macro
Arrays in SAS
Introduction To Sas
Proc report
Understanding SAS Data Step Processing
SAS BASICS
Data mining tasks
Big data frameworks
Data warehouse architecture
SAS Access / SAS Connect
View & index in SQL
Database architecture
pl/sql Procedure
Utility Procedures in SAS
3 Data Mining Tasks
Ad

Viewers also liked (20)

PPTX
Direct linking loader
PPTX
System Programming Unit II
PPT
System software
PPT
Operating system.ppt (1)
PPT
Software tools
PPT
Introduction to compiler
PPTX
Lecture 11 semantic analysis 2
PPT
Module 11
PPTX
Introduction to loaders
PPT
Lex (lexical analyzer)
PPTX
Top down and botttom up Parsing
PPTX
Compiler vs Interpreter-Compiler design ppt.
PPT
Lexical analyzer
PDF
Phases of the Compiler - Systems Programming
PPTX
Specification-of-tokens
PPTX
Compiler Chapter 1
PPT
When best to use the %let statement, the symput routine, or the into clause t...
PDF
PPTX
JavaOne 2014 - CON2013 - Code Generation in the Java Compiler: Annotation Pro...
Direct linking loader
System Programming Unit II
System software
Operating system.ppt (1)
Software tools
Introduction to compiler
Lecture 11 semantic analysis 2
Module 11
Introduction to loaders
Lex (lexical analyzer)
Top down and botttom up Parsing
Compiler vs Interpreter-Compiler design ppt.
Lexical analyzer
Phases of the Compiler - Systems Programming
Specification-of-tokens
Compiler Chapter 1
When best to use the %let statement, the symput routine, or the into clause t...
JavaOne 2014 - CON2013 - Code Generation in the Java Compiler: Annotation Pro...
Ad

Similar to SAS Macros part 1 (20)

PPT
Sas macros part 4.1
PPTX
SAS macro processing vs with out macro processing
PDF
Macro-processor
PPTX
Unit 4 sp macro
PDF
Handout#05
PPTX
Macro Processor
PPT
SAS Macros part 4.1
PDF
Handout#04
PDF
33443223 system-software-unit-iv
PPTX
Unit ii-111206004636-phpapp01
PPTX
SPOS UNIT 2 MACRO NOTES SPOS UNIT 2 MACRO NOTESSPOS UNIT 2 MACRO NOTESSPOS...
PDF
Unit 2
PPTX
MACRO PROCESSOR
PDF
handout6.pdf
PPT
PDF
Module 5.pdf
PPT
Cp0675 03 may-2012-rm04
PPT
Macros in system programing
PPTX
System software - macro expansion,nested macro calls
Sas macros part 4.1
SAS macro processing vs with out macro processing
Macro-processor
Unit 4 sp macro
Handout#05
Macro Processor
SAS Macros part 4.1
Handout#04
33443223 system-software-unit-iv
Unit ii-111206004636-phpapp01
SPOS UNIT 2 MACRO NOTES SPOS UNIT 2 MACRO NOTESSPOS UNIT 2 MACRO NOTESSPOS...
Unit 2
MACRO PROCESSOR
handout6.pdf
Module 5.pdf
Cp0675 03 may-2012-rm04
Macros in system programing
System software - macro expansion,nested macro calls

Recently uploaded (20)

PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Spectroscopy.pptx food analysis technology
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Electronic commerce courselecture one. Pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Encapsulation theory and applications.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
Cloud computing and distributed systems.
PDF
Machine learning based COVID-19 study performance prediction
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Programs and apps: productivity, graphics, security and other tools
MYSQL Presentation for SQL database connectivity
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Empathic Computing: Creating Shared Understanding
Spectroscopy.pptx food analysis technology
Diabetes mellitus diagnosis method based random forest with bat algorithm
MIND Revenue Release Quarter 2 2025 Press Release
Electronic commerce courselecture one. Pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Spectral efficient network and resource selection model in 5G networks
Encapsulation theory and applications.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Cloud computing and distributed systems.
Machine learning based COVID-19 study performance prediction
Review of recent advances in non-invasive hemoglobin estimation
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf

SAS Macros part 1

  • 2. Chapter 1. Overview of the Macro Facility Venkata Maguluri
  • 3. Applications of the Macro Facility Section 1.1 Applications of the Macro Facility
  • 4. Objectives Identify different applications of the SAS macro facility. Applications of the Macro Facility
  • 5. Purpose of the Macro Facility Using the macro language, you can write SAS programs that are dynamic , or capable of self-modification. Specifically, the macro language enables you to create and resolve macro variables anywhere in a SAS program write special programs ( macros ) that generate tailored SAS code. Applications of the Macro Facility
  • 6. Displaying System Information Using the macro language, you can utilize automatic macro variables that contain information regarding your system environment. For example, some of these variables contain date and time of SAS session version of SAS operating system. Applications of the Macro Facility
  • 7. Displaying System Information 1 2 2 2 2 1 3 5 4 5 2 3 4 1 time of day day of week date (day, month, and year) operating system release of SAS software Applications of the Macro Facility
  • 8. Substitute Information Multiple Times The macro facility can substitute the same user-defined information into multiple locations within a single program. Example: Substitute a four-digit year of interest into multiple locations in a program. proc print data=perm.schedule; where year(begin_date)= Year-of-Interest ; title &quot;Scheduled Classes for Year-of-Interest &quot;; run; proc means data=perm.all sum; where year(begin_date)= Year-of-Interest ; class location; var fee; title &quot;Total Fees for Year-of-Interest Classes&quot;; title2 &quot;by Training Center&quot;; run; Applications of the Macro Facility
  • 9. Conditional Processing The macro facility controls whether certain portions of a SAS program are processed based on specific conditions. Example: Generate the detailed registration report on a daily basis, but generate the revenue summary report only on Friday. Applications of the Macro Facility
  • 10. Conditional Processing Is it Friday? Yes Always Print the Daily Report Applications of the Macro Facility
  • 11. Repetitive Processing The macro facility generates portions of a SAS program repetitively, making each iteration perform differently. Example: Generate the same summary report for each year between 2000 and 2002. Applications of the Macro Facility
  • 12. Repetitive Processing Applications of the Macro Facility
  • 13. Repetitive Processing Applications of the Macro Facility
  • 14. Data-driven Applications To summarize a different time period on an enrollment report, certain portions of the report require modification : Starting date - explicitly specified by user Ending date - explicitly specified by user Total number of students during time period - dynamically determined Average class size during time period - dynamically determined. 1 2 3 4 Applications of the Macro Facility
  • 15. Data-driven Applications 4 1 2 3 Applications of the Macro Facility
  • 16. Tips on Writing Macro-based Programs If a macro-based program is used to generate SAS code, write and debug the desired SAS program without any macro coding make sure the SAS program runs with hard-coded programming constants on a fixed set of data. Program Flow
  • 18. Objectives Identify the tokens in a SAS program. Describe how a SAS program is tokenized, compiled, and executed. Program Flow
  • 19. Compilation and Execution A SAS program can be any combination of DATA steps and PROC steps global statements Screen Control Language (SCL) Structured Query Language (SQL) SAS macro language. When you submit a program, it goes to an area of memory called the input stack . Program Flow
  • 20. Compilation and Execution Input Stack data new; set perm.mast; bonus=wage*1.1; run; proc print; run; libname perm ‘.’; options nodate; proc sql; select * from perm.mast; MAIN: erroroff wage; if wage gt 20 then erroron wage; return; Display Manager SUBMIT Command SCL COMPILE Command Batch or Noninteractive Submission Program Flow
  • 21. Compilation and Execution Once SAS code is in the input stack, the SAS System 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 is reached executes the compiled code if there are no compilation errors repeats this process as necessary. Program Flow
  • 22. Compilation and Execution data new; set perm.mast; bonus=wage *1.1; run; proc print; run; Compiler SAS System Input Stack Program Flow
  • 23. Tokenization A component of SAS known as the word scanner breaks program text into fundamental units called token s . 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. Program Flow
  • 24. Tokenization How does SAS know when to stop sending statements to the compiler? How would processing be affected if the RUN statement were omitted for a noninteractive submission an interactive submission? Compiler data new; SAS Word Scanner Input Stack bonus=wage*1.1; proc print; run; set perm . mast ; Program Flow
  • 25. Tokenization The word scanner recognizes four classes of tokens: literals are a string of characters treated as a unit. The string is enclosed in single quotes or double quotes. ’ Any text’ &quot;Any text&quot; numbers are a string of digits (integers). Date, time, datetime constants, and hexadecimal constants are also integer tokens. 23 3 100 ’01jan2002’d are strings of digits that also include a period or E-notation (real numbers). 23.5 3. .11 5E8 7.2E-4 Program Flow
  • 26. names consist of a string of characters beginning with a letter or underscore and continuing with underscores, letters, or digits. (A period can sometimes be part of a name.) infile _n_ item3 univariate dollar10.2 special is any character or group of characters that have reserved meaning to the compiler. * / + - ** ; $ ( ) . & % 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 32767 characters. Tokenization Program Flow
  • 27. Tokenization: Examples 1. Blanks are not tokens. One or more blanks only serve to separate tokens. 2. The text of a literal is treated as a unit by the word scanner when it is enclosed in single quotes. Input Stack Tokens var x1-x10 z ; (1) VAR (2) X1 (3) - (4) X10 (5) Z (6) ; Input Stack Tokens title 'Report for May'; (1) TITLE (2) 'Report for May' (3) ; Program Flow
  • 28. Tokenization: Exercise 1.1 How many tokens are present in each of these statements? input @10 ssn comma11. name $30-50; bonus=3.2*(wage-2000); plot date*revenue='$'/vref='30jun2001'd; Program Flow
  • 29. Tokenization: Exercise Answers How many tokens are present in each of these statements? input @10 ssn comma11. name $30-50; bonus=3.2*(wage-2000); plot date*revenue=‘$’/vref=‘30jun2001’d; 11 10 11 Program Flow
  • 30. Section 1.3 Macro Processing
  • 31. Objectives Describe how the macro processor affects program flow. Micro Processing
  • 32. Macro Triggers The macro processor is a part of the macro facility that acts upon certain token sequences detected during word scanning: % followed by a name token (example: %let ) & followed by a name token (example: &amt ) Each of these token sequences is called a macro trigge r . Micro Processing
  • 33. Macro Triggers When the word scanner encounters a macro trigger, the macro processor examines those tokens requests additional tokens if necessary performs the action indicated. Micro Processing
  • 34. How the Macro Processor Works Macro Processor Input Stack SYSDAY SYSLAST Tuesday _NULL_ Symbol Table ... Compiler Word Scanner %let amt=1.1; data new; set perm.mast; bonus=wage*&amt; proc print; run; Micro Processing
  • 35. Macro Processor SYSDAY SYSLAST Tuesday _NULL_ Symbol Table When a macro trigger is encountered, it is passed to the macro processor for evaluation. %let ... Input Stack How the Macro Processor Works Compiler Word Scanner amt=1.1; data new; set perm.mast; bonus=wage*&amt; proc print; run; Micro Processing
  • 36. How the Macro Processor Works Macro Processor data new; set perm.mast; bonus=wage*&amt; proc print; run; %let amt=1.1; %LET is the keyword of a macro statement that creates a macro variable. The macro processor requests tokens until a semicolon is encountered. ... Compiler Word Scanner Input Stack Micro Processing SYSDAY SYSLAST Tuesday _NULL _ Symbol Table
  • 37. When the %LET statement executes, a macro variable AMT is given the value 1.1 and stored in a memory location called a symbol tabl e . ... Macro Processor Input Stack Compiler Word Scanner data new; set perm.mast; bonus=wage*&amt; proc print; run; How the Macro Processor Works Micro Processing SYSDAY SYSLAST AMT Tuesday _NULL _ 1.1 Symbol Table
  • 38. ; proc print; run; Word scanning continues until another macro trigger is found. data new; set perm.mast; bonus=wage* &amt ... Macro Processor Compiler Word Scanner Input Stack How the Macro Processor Works Micro Processing SYSDAY SYSLAST AMT Tuesday _NULL_ 1.1 Symbol Table
  • 39. The trigger &amt is called a macro variable reference . The macro processor attempts to find the AMT variable in the symbol table. ... Macro Processor Compiler Word Scanner Input Stack data new; set perm.mast; bonus=wage* ; proc print; run; &amt How the Macro Processor Works Micro Processing SYSDAY SYSLAST AMT Tuesday _NULL_ 1.1 Symbol Table
  • 40. Word scanning continues. If the AMT variable is found, its value is passed back to the input stack. ... Compiler Word Scanner Input Stack Macro Processor data new; set perm.mast; bonus=wage* 1.1 ; proc print; run; How the Macro Processor Works Micro Processing SYSDAY SYSLAST AMT Tuesday _NULL_ 1.1 Symbol Table
  • 41. Macro Processor When a step boundary is recognized, the DATA step compilation phase ends and execution begins. ; run; proc print ... Compiler Word Scanner Input Stack data new; set perm.mast; bonus=wage*1.1 How the Macro Processor Works Micro Processing SYSDAY SYSLAST AMT Tuesday _NULL_ 1.1 Symbol Table
  • 42. The macro processor may write messages to the SAS log if it cannot act upon a macro trigger. Example: Suppose the macro variable reference was coded as &ant instead of &amt . Macro Processor &ant WARNING: Apparent symbolic reference ANT not resolved. Input Stack data new; set perm.mast; bonus=wage* Compiler Word Scanner ; proc print; run; How the Macro Processor Works Micro Processing SYSDAY SYSLAST AMT Tuesday _NULL_ 1.1 Symbol Table
  • 43. If the macro processor cannot interpret the trigger, 1. it passes the tokens back to the word scanner 2. the word scanner passes them to the compiler 3. the DATA step compiler writes an error message. ERROR: Expecting a variable name. ... Compiler Word Scanner Input Stack data new; set perm.mast; bonus=wage *&amt ; ; proc print; run; Macro Processor How the Macro Processor Works Micro Processing SYSDAY SYSLAST AMT Tuesday _NULL_ 1.1 Symbol Table
  • 44. The %INCLUDE Statement The special token pair %include requests that SAS statements stored in an external file be inserted at that location in the input stack. External File %let amt=1.1; data new; set perm.mast; bonus=wage*&amt; Input Stack %include ' external-file '; proc print; run; Micro Processing
  • 45. file-specification describes the location of the SAS code to be inserted: ' external-file ' is the physical name of the file. fileref is the file reference supplied through a host command or FILENAME statement. SOURCE2 causes the inserted SAS statements to appear in the SAS log. The %INCLUDE statement retrieves SAS source code from an external file. General form of the %INCLUDE statement: %INCLUDE file-specification < / SOURCE2 >; The %INCLUDE Statement Micro Processing
  • 46. The word scanner encounters the % and include tokens, passes them to the macro processor that passes the tokens to the %INCLUDE handling routines. External File %let amt=1.1; data new; set perm.mast; bonus=wage*&amt; Word Scanner Macro Processor %INCLUDE Handling Routines ‘ external file’; proc print; run; %include Input Stack ... The %INCLUDE Statement Micro Processing
  • 47. The %INCLUDE handling routines obtain the external file specification. External File %let amt=1.1; data new; set perm.mast; bonus=wage*&amt; Word Scanner Macro Processor %INCLUDE Handling Routines proc print; run; %include ‘ external file ’; Input Stack ... The %INCLUDE Statement Micro Processing
  • 48. The contents of the external file are placed into the input stack. The word scanner begins to read the newly inserted statements. External File %let amt=1.1; data new; set perm.mast; bonus=wage*&amt; Word Scanner Macro Processor %let amt=1.1; data new; set perm.mast; bonus=wage*&amt; proc print; run; Input Stack Symbol Table SYSDAY Tuesday SYSLAST _NULL_ ... The %INCLUDE Statement Micro Processing
  • 49.