Defining and Calling a Macro

Slides:



Advertisements
Similar presentations
Chapter 9: Introducing Macro Variables 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Advertisements

1 Compiler Construction Intermediate Code Generation.
Macro Processor.
Chapter 11: Creating and Using Macro Programs 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
CHAPTER 1: AN OVERVIEW OF COMPUTERS AND LOGIC. Objectives 2  Understand computer components and operations  Describe the steps involved in the programming.
Environments and Evaluation
Topic 15 Implementing and Using Stacks
Guide To UNIX Using Linux Third Edition
Welcome to SAS…Session..!. What is SAS..! A Complete programming language with report formatting with statistical and mathematical capabilities.
“SAS macros are just text substitution!” “ARRRRGGHHH!!!”
I OWA S TATE U NIVERSITY Department of Animal Science Writing Flexible Codes with the SAS Macro Facility (Chapter in the 7 Little SAS Book) Animal Science.
1 Chapter 3: Macro Definitions 3.1 Defining and Calling a Macro 3.2 Macro Parameters 3.3 Macro Storage (Self-Study)
Computing for Research I Spring 2014 January 22, 2014.
Copyright © 2008, SAS Institute Inc. All rights reserved. SAS ® Macros: Top-Five Questions (and Answers!) Kim Wilson –Technical Support Analyst SAS Institute.
SAS Macros ® 101 How I learned to stop worrying and love macros Alex Chaplin BCS USA Section.
Chapter 10:Processing Macro Variables at Execution Time 1 STAT 541 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
1 Chapter 1: Introduction 1.1 Course Logistics 1.2 Purpose of the Macro Facility 1.3 Program Flow.
PYTHON. Python is a high-level, interpreted, interactive and object- oriented scripting language. Python was designed to be highly readable which uses.
SAS Macro: Some Tips for Debugging Stat St. Paul’s Hospital April 2, 2007.
CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali.
Debugging in Java. Common Bugs Compilation or syntactical errors are the first that you will encounter and the easiest to debug They are usually the result.
5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.
Macro Overview Mihaela Simion. Macro Facility Overview Definition : The SAS Macro Facility is a tool within base SAS software that contains the essential.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
Introduction to SAS Essentials Mastering SAS for Data Analytics Alan Elliott and Wayne Woodward SAS ESSENTIALS -- Elliott & Woodward1.
Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
BMTRY 789 Lecture 10: SAS MACRO Facility Annie N. Simpson, MSc.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Chapter 7: Macros in SAS  Macros provide for more flexible programming in SAS  Macros make SAS more “object-oriented”, like R 1 © Fall 2011 John Grego.
FOR MONDAY: Be prepared to hand in a one-page summary of the data you are going to use for your project and your questions to be addressed in the project.
FORMAT statements can be used to change the look of your output –if FORMAT is in the DATA step, then the formats are permanent and stored with the dataset.
C language + The Preprocessor. + Introduction The preprocessor is a program that processes that source code before it passes through the compiler. It.
SAS Programming Training Instructor:Greg Grandits TA: Textbooks:The Little SAS Book, 5th Edition Applied Statistics and the SAS Programming Language, 5.
Based on Learning SAS by Example: A Programmer’s Guide Chapters 1 & 2
Chapter 1: Introduction to Computers and Programming.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapter 25 By Tasha Chapman, Oregon Health Authority.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Lecture 3 Translation.
Session 1 Retrieving Data From a Single Table
By Sasikumar Palanisamy
Chapter 5: Passing and Processing Macro Parameters
A Simple Syntax-Directed Translator
-by Nisarg Vasavada (Compiled*)
Two “identical” programs
Intro to PHP & Variables
Assembler Design Options
Some ways to encourage quality programming
Chapter 18: Modifying SAS Data Sets and Tracking Changes
3 Macro Storage.
Conditional Processing
Chapter 7: Macros in SAS Macros provide for more flexible programming in SAS Macros make SAS more “object-oriented”, like R Not a strong suit of text ©
SAS Essentials How SAS Thinks
3 Macro Parameters.
Chapter 24 (4th ed.): Creating Functions
Exploring Microsoft® Access® 2016 Series Editor Mary Anne Poatsy
C Preprocessor(CPP).
Creating Macro Variables in the DATA Step
Macro Variable’s scope
Global and Local Symbol Tables
How are your SAS Skills? Chapter 1: Accessing Data (Question # 1)
Retrieving Macro Variables in the DATA Step
3 Iterative Processing.
PL/SQL Declaring Variables.
Passing Simple and Complex Parameters In and Out of Macros
CodePainter Revolution Trainer Course
Conditional Compilation
Writing Robust SAS Macros
CMPE 152: Compiler Design September 17 Class Meeting
Presentation transcript:

Defining and Calling a Macro 3 Defining and Calling a Macro

Defining a Macro A macro or macro definition enables you to write macro programs. macro-name follows SAS naming conventions. macro-text can include : Any combination of: text SAS statements macro variable references macro statements, expressions, or calls %MACRO macro-name; macro-text %MEND <macro-name>; This slide says "A macro or macro definition enables you to write macro programs", but we don't write any macro programs in this chapter, unless you count a single %put statement as a program, so students must wait till chapter 5 to see macro programs. In this chapter, we lay the groundwork for chapter 5. A macro, or macro definition, is the container that holds a macro program. We'll learn about that container in this chapter, and we'll fill in the container with macro programs in chapter 5.

Macro Compilation When a macro definition is submitted: Macro language statements, if any, are Checked for syntax errors Compiled. SAS statements and other text are not checked for syntax errors not compiled. The macro is stored as a SAS catalog entry in the temporary catalog work.sasmacr by default. There are only two fundamental things you can put in a macro: 1) macro language statements; and 2) anything else. Anything else is text. SAScode is text. The macro processor knows nothing about SAScode. It only knows about stuff that begins with a percent sign or an ampersand. Just as a macro variable must be created before it can be referenced, a macro must be created, or defined, before it can be called.

Macro Compilation The MCOMPILENOTE=ALL option issues a note to the SAS log after a macro definition has compiled. The default setting is MCOMPILENOTE=NONE. OPTIONS MCOMPILENOTE=ALL|NONE;

Macro Compilation %macro time; %put The current time is %sysfunc(time(),timeampm.).; %mend time; This is a simple macro program because it contains a macro language statement. It is the only macro program we will see in this chapter.

Macro Compilation options mcompilenote=all; %macro time; %put The current time is %sysfunc(time(),timeampm.).; %mend time; This is a simple macro program because it contains a macro language statement. It is the only macro program we will see in this chapter.

Macro Storage Example: Produce a list of compiled macros stored in the default temporary catalog work.sasmacr. proc catalog cat=work.sasmacr; contents; title "My Temporary Macros"; quit; title;

Calling a Macro %macro-name A macro call Causes the macro to execute Is specified by placing a percent sign before the name of the macro Can be made anywhere in a program (similar to a macro variable reference) Represents a macro trigger Is not a statement (no semicolon required). Having previously defined a macro, we can call it. You call a macro the same way you call your dog. By name. %macro-name

%time Calling the time macro causes the %put statement to execute.

Simple Macro A macro often generates SAS code. %macro calc; proc means data=mac1.order_item &stats; var &vars; run; %mend calc; proc catalog cat=work.sasmacr; contents; title "My Temporary Macros"; quit; title; Strictly speaking, this macro does not contain a macro program, because there are no macro language statements. The macro contains only text, including two macro variable references. When the macro definition is submitted, there is nothing for the macro processor to compile. This macro simply stores the text, much as you would store a text file. This is not a good use of a macro, although people do this sort of thing all the time. This macro contains no macro language statements.

Simple Macro Example: Call the CALC macro. Precede the call with %LET statements that create the macro variables referenced within the macro. %let stats=min max; %let vars=quantity; %calc

Macro Execution The MPRINT option writes to the SAS log the text sent to the SAS compiler as a result of macro execution. General form of the MPRINT|NOMPRINT option: The default setting is NOMPRINT. OPTIONS MPRINT; OPTIONS NOMPRINT;

Program Flow, When the macro processor receives %macro-name Searches the designated SAS catalog (work.sasmacr by default) for an entry named macro-name.MACRO Executes compiled macro language statements, if any Sends other text to the input stack for word scanning Pauses while the word scanner tokenizes inserted text, and SAS code, if any, compiles and executes Resumes execution of macro language statements after SAS code executes Here again, a macro stores only two things: 1) macro language statements; or 2) text. When you call a macro, the rule is simple: compiled macro language statements are executed. Anything else is sent to the input stack.

Program Flow work.sasmacr Compiler Symbol Table Word Scanner Macro Processor Input Stack work.sasmacr %let stats=min max; %let vars=quantity; %calc # Name Type 1 CALC MACRO 2 TIME MACRO ...

Program Flow work.sasmacr The macro processor executes the %LET statements and populates the symbol table. Compiler Symbol Table STATS min max VARS quantity Word Scanner Macro Processor Input Stack work.sasmacr %calc # Name Type 1 CALC MACRO 2 TIME MACRO ...

Program Flow work.sasmacr When the macro processor receives %CALC, it locates CALC.MACRO within the work.sasmacr catalog. Compiler Symbol Table STATS min max VARS quantity Word Scanner Macro Processor %calc Input Stack work.sasmacr # Name Type 1 CALC MACRO 2 TIME MACRO ...

Program Flow The macro processor opens CALC.MACRO. There are no macro language statements to execute. Compiler Symbol Table STATS min max VARS quantity Word Scanner Macro Processor Input Stack CALC.MACRO %macro calc; proc means data=orion.order_item &stats; var &vars; run; %mend calc; ...

Program Flow The macro processor places the macro text on the input stack. Compiler Symbol Table STATS min max VARS quantity Word Scanner Macro Processor Input Stack CALC.MACRO proc means data=orion.order_item &stats; var &vars; run; %macro calc; proc means data=orion.order_item &stats; var &vars; run; %mend calc; ...

Program Flow Macro activity pauses while the word scanner tokenizes text and passes it to the compiler. Compiler Symbol Table proc means data=orion.order_item STATS min max VARS quantity Word Scanner Macro Processor Input Stack CALC.MACRO &stats; var &vars; run; %macro calc; proc means data=orion.order_item &stats; var &vars; run; %mend calc; ...

Program Flow Macro variable references are passed to the macro processor. Compiler Symbol Table proc means data=orion.order_item STATS min max VARS quantity Word Scanner Macro Processor &stats Input Stack CALC.MACRO ; var &vars; run; %macro calc; proc means data=orion.order_item &stats; var &vars; run; %mend calc; ...

Program Flow Symbolic substitution is performed. Compiler Symbol Table proc means data=orion.order_item STATS min max VARS quantity Word Scanner Macro Processor &stats Input Stack CALC.MACRO min max; var &vars; run; %macro calc; proc means data=orion.order_item &stats; var &vars; run; %mend calc; ...

Program Flow The word scanner tokenizes the resolved value and passes it to the compiler. Compiler Symbol Table proc means data=orion.order_item min max; STATS min max VARS quantity Word Scanner Macro Processor Input Stack CALC.MACRO var &vars; run; %macro calc; proc means data=orion.order_item &stats; var &vars; run; %mend calc; ...

Program Flow Word scanning continues. Compiler Symbol Table proc means data=orion.order_item min max; var STATS min max VARS quantity Word Scanner Macro Processor Input Stack CALC.MACRO &vars; run; %macro calc; proc means data=orion.order_item &stats; var &vars; run; %mend calc; ...

Program Flow Macro variable references are passed to the macro processor. Compiler Symbol Table proc means data=orion.order_item min max; var STATS min max VARS quantity Word Scanner Macro Processor &vars Input Stack CALC.MACRO ; run; %macro calc; proc means data=orion.order_item &stats; var &vars; run; %mend calc; ...

Program Flow Symbolic substitution is performed. Compiler Symbol Table proc means data=orion.order_item min max; var STATS min max VARS quantity Word Scanner Macro Processor &vars Input Stack CALC.MACRO quantity; run; %macro calc; proc means data=orion.order_item &stats; var &vars; run; %mend calc; ...

Program Flow The word scanner tokenizes the resolved value and passes it to the compiler. Compiler Symbol Table proc means data=orion.order_item min max; var quantity; STATS min max VARS quantity Word Scanner Macro Processor Input Stack CALC.MACRO run; %macro calc; proc means data=orion.order_item &stats; var &vars; run; %mend calc; ...

Program Flow When a step boundary is encountered, SAS executes the compiled step as macro activity remains paused. Macro activity stops when the %MEND statement is encountered. Compiler Symbol Table proc means data=orion.order_item min max; var quantity; STATS min max VARS quantity Word Scanner Macro Processor run; Input Stack CALC.MACRO %macro calc; proc means data=orion.order_item &stats; var &vars; run; %mend calc;

Macro Execution The SAS log reports execution of the PROC MEANS step. 52 %let stats=min max; 53 %let vars=quantity; 54 %calc NOTE: There were 732 observations read from the data set ORION.ORDER_ITEM. NOTE: PROCEDURE MEANS used (Total process time): real time 0.03 seconds cpu time 0.03 seconds PROC MEANS source code does not appear in the SAS log.

Macro Execution The MPRINT option writes to the SAS log the text sent to the SAS compiler as a result of macro execution. General form of the MPRINT|NOMPRINT option: The default setting is NOMPRINT. OPTIONS MPRINT; OPTIONS NOMPRINT;

Macro Execution options mprint; %calc options nomprint;