Macro Variable’s scope

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

Macro Processor.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Chapter 11: Creating and Using Macro Programs 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
VBA Modules, Functions, Variables, and Constants
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
“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.
Call-by-Value vs. Call-by-Reference Call-by-value parameters are used for passing information from the calling function to the called function (input parameters).
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.
C++ for Engineers and Scientists Third Edition
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
INTRODUCTION TO SAS MACRO PROCESSING James R. Bence, Ph.D., Co-Director Quantitative Fisheries Center Professor Department of Fisheries and Wildlife March.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 7 PL/SQL Packages.
5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.
18. DECLARATIONS.
CpSc 462/662: Database Management Systems (DBMS) (TEXNH Approach) Stored Procedure James Wang.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Macro Variable Resolution Enio Presutto York University, Toronto, Canada.
Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004.
Week 6: Functions - Part 2 BJ Furman 01OCT2012. The Plan for Today Comments on midterm exam (next week in lab!) Review of functions Scope of identifiers.
BMTRY 789 Lecture 10: SAS MACRO Facility Annie N. Simpson, MSc.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
JavaScript and Ajax (Control Structures) Week 4 Web site:
C language + The Preprocessor. + Introduction The preprocessor is a program that processes that source code before it passes through the compiler. It.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapter 25 By Tasha Chapman, Oregon Health Authority.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Oracle9i Developer: PL/SQL Programming Chapter 6 PL/SQL Packages.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Test 2 Review Outline.
Java Primer 1: Types, Classes and Operators
Two “identical” programs
Scripts & Functions Scripts and functions are contained in .m-files
Programmazione I a.a. 2017/2018.
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
3 Macro Storage.
By Don Henderson PhilaSUG, June 18, 2018
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 ©
Topics Introduction to File Input and Output
3 Macro Parameters.
Creating Macro Variables and Assigning Value
6 Chapter Functions.
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
Creating Macro Variables in the DATA Step
Defining and Calling a Macro
Global and Local Symbol Tables
3 Iterative Processing.
Classes and Objects.
Detecting Runtime Errors and Exiting from Nested Macros Gracefully
7 Arrays.
Chapter 8 Advanced SQL.
Introduction to Computer Science
Prof. Arfaoui. COM390 Chapter 7
Topics Introduction to File Input and Output
The Three Attributes of an Identifier
Conditional Compilation
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Presentation transcript:

Macro Variable’s scope Every macro variable has a scope. A macro variable’s scope determines how it is assigned values and how the macro processor resolves references to it. Two types of scopes exist for macro variables: global and local. Global macro variables exist for the duration of the SAS session and can be referenced anywhere in the program–either inside or outside a macro. Local macro variables exist only during the execution of the macro in which the variables are created and have no meaning outside the defining macro.

Nested Macro Scopes can be nested, like boxes within boxes. For example, suppose you have a macro A that creates the macro variable LOC1 and a macro B that creates the macro variable LOC2. If the macro B is nested (executed) within the macro A, LOC1 is local to both A and B. However, LOC2 is local only to B.

Symbol Tables Macro variables are stored in symbol tables, which list the macro variable name and its value. There is a global symbol table, which stores all global macro variables. Local macro variables are stored in a local symbol table that is created at the beginning of the execution of a macro. !!! You can use the %SYMEXIST function to indicate whether or not a macro variable exists.

Global macro variables all automatic macro variables except SYSPBUFF. macro variables created outside of any macro. macro variables created in %GLOBAL statements. most macro variables created by the CALL SYMPUT routine.

Global macro variable In most cases, once you define a global macro variable, its value is available to you anywhere in the SAS session or job and can be changed anywhere. So, a macro variable referenced inside a macro definition is global if a global macro variable already exists by the same name (assuming the variable is not explicitly defined as local with the %LOCAL statement or in a parameter list). The new macro variable definition simply updates the existing global one. The following are exceptions that prevent you from referencing the value of a global macro variable: when a macro variable exists both in the global symbol table and in the local symbol table, you cannot reference the global value from within the macro that contains the local macro variable. In this case, the macro processor finds the local value first and uses it instead of the global value. if you create a macro variable in the DATA step with the SYMPUT routine, you cannot reference the value with an ampersand until the program reaches a step boundary. You can use the %SYMGLOBL function to indicate whether or not an existing macro variable resides in the global symbol table.

Local Macro Variables Local macro variables are defined within an individual macro. Each macro you invoke creates its own local symbol table. Local macro variables exist only as long as a particular macro executes. When the macro stops executing, all local macro variables for that macro cease to exist. A macro’s local symbol table is empty until the macro creates at least one macro variable. A local symbol table can be created by any of the following: the presence of one or more macro parameters a %LOCAL statement macro statements that define macro variables, such as %LET and the iterative %DO statement (assuming the variable does not already exist globally or a %GLOBAL statement is not used).

Local Macro Variables Macro parameters are always local to the macro that defines them. You cannot make macro parameters global. When you invoke one macro inside another, you create nested scopes. Because you can have any number of levels of nested macros, your programs can contain any number of levels of nested scopes. You can use the %SYMLOCAL function to indicate whether or not an existing macro variable resides in an enclosing local symbol table.

Writing the Contents of Symbol Tables to the SAS Log

Search Order When Assigning or Creating Macro Variables

Search Order When Resolving Macro Variable References

Changing the Values of Existing Macro Variables When the macro processor executes a macro program statement that can create a macro variable (such as a %LET statement), the macro processor attempts to change the value of an existing macro variable rather than create a new macro variable. The %GLOBAL and %LOCAL statements are exceptions. %let new=inventry; %macro name1; %let new=report; %mend name1; Suppose you submit the following statements: %name1; data &new; These statements produce the following statement: data report; data inventry; NOT TIP: Avoid a Macro accidentally change a global macro variable, which is going to be used later.

Scope with the CALL SYMPUT Routine Most problems with CALL SYMPUT involve the lack of an explicit step boundary between the CALL SYMPUT statement that creates the macro variable and the macro variable reference that uses that variable. Two rules control where CALL SYMPUT creates its variables: 1 CALL SYMPUT creates the macro variable in the current symbol table available while the DATA step is executing, provided that symbol table is not empty. If it is empty (contains no local macro variables), usually CALL SYMPUT creates the variable in the closest nonempty symbol table. 2 However, there are three cases where CALL SYMPUT creates the variable in the local symbol table, even if that symbol table is empty: if CALL SYMPUT is used after a PROC SQL, the variable will be created in a local symbol table. If the macro variable SYSPBUFF is created at macro invocation time, the variable will be created in the local symbol table. If the executing macro contains a computed %GOTO statement, the variable will be created in the local symbol table. A computed %GOTO statement is one that uses a label that contains an & or a % in it. That is, a computed %GOTO statement contains a macro variable reference or a macro call that produces a text expression.

SYMPUT %macro env3; data _null_; x = ’a token’; call symput(’myvar3’,x); run; %put ** Inside the macro: **; %put _user_; %mend env3; %env3 %put ** In open code: **; data temp; y="&myvar3"; In this case, the DATA step is complete and executes within the macro, but the local symbol table is empty. So, CALL SYMPUT creates MYVAR3 in the closest available nonempty symbol table—the global symbol table. Both %PUT statements show that MYVAR3 exists in the global symbol table: LOG: ** Inside the macro: ** GLOBAL MYVAR3 a token ** In open code: **

Resolving Macro Resolution Problems Occurring During DATA Step Compilation use CALL SYMPUT to conditionally assign a macro variable a value in a DATA step. So, you submit the following program: %let sr_age = 0; data senior; set census; if age > 65 then do; call symput("sr_age",age); put "This data set contains data about a person"; put "who is &sr_age years old."; /* ERROR */ end; run; If AGE was 67, you’d expect to see a log message like this one: This data set contains data about a person who is 67 years old. However, no matter what AGE is, the following message is sent to the log: This data set contains data about a person who is 0 years old. When the DATA step is being compiled, &SR_AGE is sent to the macro facility for resolution, and the result is passed back before the DATA step executes.

To achieve the desired result, submit this corrected program instead: %let sr_age = 0; data senior; set census; if age > 65 then do; call symput("sr_age",age); stop; end; run; data _null_; put "This data set contains data about a person"; put "who is &sr_age years old."; Note: Use double quotation marks in statements like PUT, because macro variables do not resolve when enclosed in single quotation marks.

Submitting these statements writes the following lines to the SAS log: Here is another example of erroneously referring to a macro variable in the same step that creates it: data _null_; retain total 0; set mydata end=final; total=total+price; call symput("macvar",put(total,dollar14.2)); if final then put "*** total=&macvar ***"; /* ERROR */ run; Submitting these statements writes the following lines to the SAS log: WARNING: Apparent symbolic reference MACVAR not resolved. *** total=&macvar ***