Macro Basics. Introduction. Defining and Using Macro Variables. Defining and Using Macros. Macro Parameters. Part 1. Chapter 1. Chapter 2.

Similar documents
Contents. About This Book...1

PLA YING WITH MACROS: TAKE THE WORK OUT OF LEARNING TO DO MACROS. Arthur L. Carpenter

Understanding the Concepts and Features of Macro Programming 1

The 'SKIP' Statement

Going Under the Hood: How Does the Macro Processor Really Work?

CHAPTER 7 Using Other SAS Software Products

Sandra Hendren Health Data Institute

SAS Macro. SAS Training Courses. Amadeus Software Ltd

An Animated Guide: An Introduction to SAS Macro Quoting Russ Lavery Bryn Mawr, PA

SAS Macro Dynamics - From Simple Basics to Powerful Invocations Rick Andrews, Office of the Actuary, CMS, Baltimore, MD

SAS Macro Language: Reference

Acknowledgments xi Preface xiii About the Author xv About This Book xvii New in the Macro Language xxi

Introduction. Getting Started with the Macro Facility CHAPTER 1

2.2 Syntax Definition

EDIABAS BEST/2 LANGUAGE DESCRIPTION. VERSION 6b. Electronic Diagnostic Basic System EDIABAS - BEST/2 LANGUAGE DESCRIPTION

4. Inputting data or messages to a function is called passing data to the function.

SAS Macro Programming for Beginners

An Introduction to Macros Deb Cassidy

4. Structure of a C++ program

More About SAS Macros

The DATA Statement: Efficiency Techniques

Introduction to the SAS Macro Facility

What Is SAS? CHAPTER 1 Essential Concepts of Base SAS Software

Fundamental Concepts and Definitions

Using ANNOTATE MACROS as Shortcuts

Base and Advance SAS

Typescript on LLVM Language Reference Manual

Appendix A. The Preprocessor


Job Security: Using the SAS Macro Language to Full Advantage

HOW TO DEVELOP A SAS/AF APPLICATION

ABSTRACT MORE THAN SYNTAX ORGANIZE YOUR WORK THE SAS ENTERPRISE GUIDE PROJECT. Paper 50-30

A Simple Syntax-Directed Translator

Automation of makefile For Use in Clinical Development Nalin Tikoo, BioMarin Pharmaceutical Inc., Novato, CA

Compilers Project 3: Semantic Analyzer

Honu. Version November 6, 2010

Motivation was to facilitate development of systems software, especially OS development.

Lecture Outline. COMP-421 Compiler Design. What is Lex? Lex Specification. ! Lexical Analyzer Lex. ! Lex Examples. Presented by Dr Ioanna Dionysiou

SCH Filter. Summary. Panel Access. Modified by Susan Riege on Jan 19, SCH Inspector. Parent page: Panels

A simple syntax-directed

Have examined process Creating program Have developed program Written in C Source code

Lecture 18 Tao Wang 1

CSCE 314 Programming Languages. Type System

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS

)454 : 4(% #(!2!#4%2 3%4!.$ "!3)# %,%-%.43 -!.-!#().%,!.'5!'% )454 Recommendation : INTERNATIONAL TELECOMMUNICATION UNION

A Time Saver for All: A SAS Toolbox Philip Jou, Baylor University, Waco, TX

C Programming SYLLABUS COVERAGE SYLLABUS IN DETAILS

SAS Macro Dynamics: from Simple Basics to Powerful Invocations Rick Andrews, Office of Research, Development, and Information, Baltimore, MD

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

SAS Clinical Data Integration Server 2.1

KLiC C++ Programming. (KLiC Certificate in C++ Programming)

Using PROC FCMP to the Fullest: Getting Started and Doing More

Table of Contents. The RETAIN Statement. The LAG and DIF Functions. FIRST. and LAST. Temporary Variables. List of Programs.

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Introduction. Introduction to SCL. SCL Elements CHAPTER 1

The SAS Interface to REXX

Getting started 7. Writing macros 23

C++ Programming: From Problem Analysis to Program Design, Third Edition

Appendix to Topic IV

Are You Missing Out? Working with Missing Values to Make the Most of What is not There

LECTURE NOTES ON COMPILER DESIGN P a g e 2

Part (04) Introduction to Programming

IBM Tivoli Decision Support for z/os Version Messages and Problem Determination IBM SH

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

SAS Clinical Data Integration 2.4

Chapter 17. Fundamental Concepts Expressed in JavaScript

Chapter 2, Part III Arithmetic Operators and Decision Making

Using Macro Functions

flex is not a bad tool to use for doing modest text transformations and for programs that collect statistics on input.

Get Started Writing SAS Macros Luisa Hartman, Jane Liao, Merck Sharp & Dohme Corp.

Programming Languages Third Edition. Chapter 10 Control II Procedures and Environments

Basic concepts. Chapter Toplevel loop

for (i=1; i<=100000; i++) { x = sqrt (y); // square root function cout << x+i << endl; }

AS-2883 B.Sc.(Hon s)(fifth Semester) Examination,2013 Computer Science (PCSC-503) (System Software) [Time Allowed: Three Hours] [Maximum Marks : 30]

Chapter 1 INTRODUCTION. SYS-ED/ Computer Education Techniques, Inc.

Amie Bissonett, inventiv Health Clinical, Minneapolis, MN

A Short Summary of Javali

title1 "Visits at &string1"; proc print data=hospitalvisits; where sitecode="&string1";

Expr Language Reference

SAS Online Training: Course contents: Agenda:

x. The optional Cross-compiler word set x.1 Introduction x.2 Additional terms and notation x.2.1 Definitions of terms

CIS 130 Exam #2 Review Suggestions

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Compiler Design

Getting Started With Syntax October 15, 2015

Programming Languages

Macro Quoting: Which Function Should We Use? Pengfei Guo, MSD R&D (China) Co., Ltd., Shanghai, China

Extending SystemVerilog Data Types to Nets


B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

CS232 VHDL Lecture. Types

Modelica Change Proposal MCP-0019 Flattening (In Development) Proposed Changes to the Modelica Language Specification Version 3.

Programming Languages Third Edition

CMPE 152 Compiler Design

A Tutorial on the SAS Macro Language

COMPILER DESIGN. For COMPUTER SCIENCE

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

Macro Architecture in Pictures Mark Tabladillo PhD, marktab Consulting, Atlanta, GA Associate Faculty, University of Phoenix

Journey to the center of the earth Deep understanding of SAS language processing mechanism Di Chen, SAS Beijing R&D, Beijing, China

Transcription:

Part 1 Macro Basics Chapter 1 Chapter 2 Chapter 3 Chapter 4 Introduction Defining and Using Macro Variables Defining and Using Macros Macro Parameters

2 Carpenter s Complete Guide to the SAS Macro Language

3 Chapter 1 Introduction 1.1 Macro Facility Overview 1.2 Terminology 1.3 Macro Execution Phases 1.4 Referencing Environments 1.5 Chapter Summary This chapter introduces you to the fundamentals of the SAS macro language, and it includes an overview and some of the terminology of the language. Because the behavior of macros is different from that of code that is written for base SAS, sections are also included on macro execution and how the SAS System sees and uses macros. 1.1 Macro Facility Overview The SAS Macro Facility is a tool within base SAS software that contains the essential elements that enable you to use macros. The macro facility contains a macro processor that translates macro code into statements that can be used by the SAS System, and the macro language. The macro language provides the means to communicate with the macro processor. The macro language consists of its own set of commands, options, syntax, and compiler. While macro statements have similarities to the statements in the DATA step, you must understand the differences in behavior in order to effectively write and use macros. The macro language provides tools that pass information between SAS steps dynamically create code at execution time conditionally execute DATA or PROC steps create generalizable and flexible code. The tools made available through the macro facility include macro (or symbolic) variables, macro statements, and macro functions. These tools are included as part of the SAS code, or program, where they are detected when the code is sent to the SAS Supervisor for execution. 1.2 Terminology The statement and syntax structure that is used by the macro facility is known as the macro language and like any language it has its own terminology. The SAS user familiar with the programming language used in base SAS, however, will discover quickly that

4 Carpenter s Complete Guide to the SAS Macro Language much of the syntax and content of the macro language is familiar. The following terms will be used throughout this book. text a collection of characters and symbols that can contain variable names, data set names, SAS statement fragments, complete SAS statements, or even complete DATA and PROC steps. macro variable the names of macro variables are almost always preceded by an ampersand (&) in SAS code. Macro variables often are used to store text. macro stored text that contains SAS statements and macro language statements. macro program statement controls what actions take place during the macro execution. They are always preceded by a percent sign (%) and are often syntactically similar to statements used in the DATA step. macro expression one or more macro variable names, text, and/or macro functions combined together by one or more operators and/or parentheses. Macro expressions are very analogous to the expressions used in standard SAS programming. macro function predefined routines for processing text in macros and macro variables. Many macro functions are similar to functions used in the DATA step. operators symbols that are used for comparisons, logical operation, or arithmetic calculations. The operators are the same ones used in the DATA step. automatic macro variable special-purpose macro variables. These are automatically defined and provided by the SAS System. These variable names should be considered as reserved. open code SAS program statements that exist outside of any macro definition. resolving macro references during the resolution process, elements of the macro language (or references) are replaced with text. You can find additional terminology in the glossary. 1.3 Macro Execution Phases When you run a SAS program, it is executed in a series of DATA and PROC steps, one step at a time. GLOBAL statements (for example, TITLE, FOOTNOTE, %LET), which can exist outside of these steps, are executed immediately when they are encountered. For each step, the SAS System first checks to see if macro references exist. Macro references may be macro variables, macro statements, macro definitions, or macro calls. If the program does not contain any macro references, then processing continues with the DATA or PROC step processor. If the program

Chapter 1: Introduction 5 does contain macro references, then the macro processor intercepts and resolves them prior to execution. The resolved macro references then become part of the SAS code that is passed to the DATA or PROC step processor. When code is passed to the SAS supervisor, the following takes place for each step: Global statements are executed. Macro definitions are compiled and stored until they are called. A check is made to see if there are any macro statements, macro variables, or macro calls. If there are, then macro variables are resolved called macros are executed (resolved) macro statements are executed. The DATA or PROC step that contains resolved macro references (if there were any) is compiled and executed. SEE ALSO SAS Macro Language Reference, First Edition contains a detailed discussion of how SAS processes statements with macro activity on pp.14 19 and 33 41. 1.4 Referencing Environments Unlike the values of data set variables, the values of macro variables are stored in memory in a symbol table. Each macro variable s definition in the symbol table is also associated with a referencing environment or scope, which is determined by where and how the macro variable is defined. There are two environments for macro variables: global and local. A global macro variable has a single value available to all macros within the program. Macro variables that are defined outside of any macro will be global. Local macro variables have values that are available only within the macro in which they are defined. Because each macro creates its own local referencing environment, macro variable values that are defined in one macro may be undefined within another. Indeed, macro variable names need not be unique even among nested macros. This means that the specific value associated with a given macro variable may depend on how the macro variable is used in the program. In the following schematic, the macro variable DSN is defined globally and is, therefore, also known inside of the shaded macro. The macro variable COLOR, however, is only defined inside of the shaded macro and is not known outside of the macro.

6 Carpenter s Complete Guide to the SAS Macro Language Outside of all macros Global values DSN ---> clinics Inside a macro Local values DSN ----> clinics COLOR ---> blue DSN ---> clinics COLOR ---> undefined You can control the referencing environment for a macro variable through the use of the %GLOBAL and %LOCAL statements, which are described in Section 5.4.2. SEE ALSO Extensive examples can be found in SAS Guide to Macro Processing, Version 6, Second Edition (pp. 37 54) and the newer SAS Macro Language: Reference, First Edition (pp. 50 66). SUGI presentations that specifically cover referencing environments include Bercov (1993) and Hubbell (1990). An example of a macro variable that takes on more than one value at the same time is given in Carpenter (1996, p. 1637). 1.5 Chapter Summary You can think of the macro facility as a part of the SAS System that passively waits to be evoked. If your SAS code contains no macros and no references to macro variables or macro statements, the macro facility is not used. When the code does contain macro language references, the macro facility wakes up, intercepts the job stream, interprets or executes the macro references, and then releases its control. The macro facility is made up of two primary components. The macro processor provides the ability to compile and execute the macro language statements that you use to write macros.