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

What's hot (20)

PPT
Understanding SAS Data Step Processing
guest2160992
 
PDF
Introduction To Sas
halasti
 
PPTX
Sas Functions INDEX / INDEXC / INDEXW
THARUN PORANDLA
 
DOCX
Base sas interview questions
Dr P Deepak
 
PPT
SDTM - Adverse Events Vs. Clinical Events
Vijayaraghava Karpurapu
 
PPT
Statistics-1.ppt
GabrielMDOTHI
 
PPT
Utility Procedures in SAS
guest2160992
 
PPT
Data Match Merging in SAS
guest2160992
 
PPTX
SAS basics Step by step learning
Venkata Reddy Konasani
 
PDF
SAS cheat sheet
Ali Ajouz
 
PPTX
Understanding sas data step processing.
Ravi Mandal, MBA
 
PPTX
SDTM (Study Data Tabulation Model)
SWAROOP KUMAR K
 
PPTX
ADaM - Where Do I Start?
Dr.Sangram Parbhane
 
PPT
Where Vs If Statement
Sunil Gupta
 
PDF
Introduction to sas
Ajay Ohri
 
PDF
Introduction to SAS
izahn
 
PPTX
Metadata and ADaM
Kevin Lee
 
PDF
A Step-By-Step Introduction to SAS Report Procedure
YesAnalytics
 
PPT
Trial Design Domains
Ankur Sharma
 
PPT
CDISC SDTM Domain Presentation
Ankur Sharma
 
Understanding SAS Data Step Processing
guest2160992
 
Introduction To Sas
halasti
 
Sas Functions INDEX / INDEXC / INDEXW
THARUN PORANDLA
 
Base sas interview questions
Dr P Deepak
 
SDTM - Adverse Events Vs. Clinical Events
Vijayaraghava Karpurapu
 
Statistics-1.ppt
GabrielMDOTHI
 
Utility Procedures in SAS
guest2160992
 
Data Match Merging in SAS
guest2160992
 
SAS basics Step by step learning
Venkata Reddy Konasani
 
SAS cheat sheet
Ali Ajouz
 
Understanding sas data step processing.
Ravi Mandal, MBA
 
SDTM (Study Data Tabulation Model)
SWAROOP KUMAR K
 
ADaM - Where Do I Start?
Dr.Sangram Parbhane
 
Where Vs If Statement
Sunil Gupta
 
Introduction to sas
Ajay Ohri
 
Introduction to SAS
izahn
 
Metadata and ADaM
Kevin Lee
 
A Step-By-Step Introduction to SAS Report Procedure
YesAnalytics
 
Trial Design Domains
Ankur Sharma
 
CDISC SDTM Domain Presentation
Ankur Sharma
 

Viewers also liked (20)

PPTX
Direct linking loader
babyparul
 
PPTX
System Programming Unit II
Manoj Patil
 
PPT
System software
Senthil Kanth
 
PPT
Operating system.ppt (1)
Vaibhav Bajaj
 
PPT
Software tools
ravindravekariya
 
PPT
Introduction to compiler
Abha Damani
 
PPTX
Lecture 11 semantic analysis 2
Iffat Anjum
 
PPT
Module 11
bittudavis
 
PPTX
Introduction to loaders
Tech_MX
 
PPT
Lex (lexical analyzer)
Sami Said
 
PPTX
Top down and botttom up Parsing
Gerwin Ocsena
 
PPTX
Compiler vs Interpreter-Compiler design ppt.
Md Hossen
 
PPT
Lexical analyzer
Ashwini Sonawane
 
PDF
Phases of the Compiler - Systems Programming
Mukesh Tekwani
 
PPTX
Specification-of-tokens
Dattatray Gandhmal
 
PPTX
Compiler Chapter 1
Huawei Technologies
 
PPT
When best to use the %let statement, the symput routine, or the into clause t...
Arthur8898
 
PDF
15ss
Harish Khodke
 
PPTX
JavaOne 2014 - CON2013 - Code Generation in the Java Compiler: Annotation Pro...
Jorge Hidalgo
 
Direct linking loader
babyparul
 
System Programming Unit II
Manoj Patil
 
System software
Senthil Kanth
 
Operating system.ppt (1)
Vaibhav Bajaj
 
Software tools
ravindravekariya
 
Introduction to compiler
Abha Damani
 
Lecture 11 semantic analysis 2
Iffat Anjum
 
Module 11
bittudavis
 
Introduction to loaders
Tech_MX
 
Lex (lexical analyzer)
Sami Said
 
Top down and botttom up Parsing
Gerwin Ocsena
 
Compiler vs Interpreter-Compiler design ppt.
Md Hossen
 
Lexical analyzer
Ashwini Sonawane
 
Phases of the Compiler - Systems Programming
Mukesh Tekwani
 
Specification-of-tokens
Dattatray Gandhmal
 
Compiler Chapter 1
Huawei Technologies
 
When best to use the %let statement, the symput routine, or the into clause t...
Arthur8898
 
JavaOne 2014 - CON2013 - Code Generation in the Java Compiler: Annotation Pro...
Jorge Hidalgo
 
Ad

Similar to SAS Macros part 1 (20)

PPT
Sas macros part 4.1
venkatam
 
PPTX
SAS macro processing vs with out macro processing
SAYAN DAS
 
PDF
Macro-processor
Temesgen Molla
 
PPTX
Unit 4 sp macro
Deepmala Sharma
 
PDF
Handout#05
Sunita Milind Dol
 
PPTX
Macro Processor
Saranya1702
 
PPT
SAS Macros part 4.1
venkatam
 
PDF
Handout#04
Sunita Milind Dol
 
PDF
33443223 system-software-unit-iv
Shaniya Fathimuthu
 
PPTX
Unit ii-111206004636-phpapp01
riddhi viradiya
 
PPTX
SPOS UNIT 2 MACRO NOTES SPOS UNIT 2 MACRO NOTESSPOS UNIT 2 MACRO NOTESSPOS...
22510707dypit
 
PDF
Unit 2
pm_ghate
 
PPTX
MACRO PROCESSOR
Bhavik Vashi
 
PDF
handout6.pdf
ssuser700533
 
PPT
Ss4
Jaya Chavan
 
PDF
Module 5.pdf
SE14Darshan
 
PPT
Cp0675 03 may-2012-rm04
Parth Mudgal
 
PPT
Macros in system programing
Brijesh__patel
 
PPTX
System software - macro expansion,nested macro calls
SARASWATHI S
 
Sas macros part 4.1
venkatam
 
SAS macro processing vs with out macro processing
SAYAN DAS
 
Macro-processor
Temesgen Molla
 
Unit 4 sp macro
Deepmala Sharma
 
Handout#05
Sunita Milind Dol
 
Macro Processor
Saranya1702
 
SAS Macros part 4.1
venkatam
 
Handout#04
Sunita Milind Dol
 
33443223 system-software-unit-iv
Shaniya Fathimuthu
 
Unit ii-111206004636-phpapp01
riddhi viradiya
 
SPOS UNIT 2 MACRO NOTES SPOS UNIT 2 MACRO NOTESSPOS UNIT 2 MACRO NOTESSPOS...
22510707dypit
 
Unit 2
pm_ghate
 
MACRO PROCESSOR
Bhavik Vashi
 
handout6.pdf
ssuser700533
 
Module 5.pdf
SE14Darshan
 
Cp0675 03 may-2012-rm04
Parth Mudgal
 
Macros in system programing
Brijesh__patel
 
System software - macro expansion,nested macro calls
SARASWATHI S
 
Ad

Recently uploaded (20)

PPTX
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
Timothy Rottach - Ramp up on AI Use Cases, from Vector Search to AI Agents wi...
AWS Chicago
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Python basic programing language for automation
DanialHabibi2
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PPTX
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
Timothy Rottach - Ramp up on AI Use Cases, from Vector Search to AI Agents wi...
AWS Chicago
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Python basic programing language for automation
DanialHabibi2
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 

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.