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.

Slides:



Advertisements
Similar presentations
Agenda Definitions Evolution of Programming Languages and Personal Computers The C Language.
Advertisements

Chapter 9: Introducing Macro Variables 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Chapter 11: Creating and Using Macro Programs 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Advanced Topics Object-Oriented Programming Using C++ Second Edition 13.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
VBA Modules, Functions, Variables, and Constants
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
Guide To UNIX Using Linux Third Edition
JavaScript, Third Edition
Introduction to C Programming
“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.
Computing for Research I Spring 2014 January 22, 2014.
CHAPTER 4: INTRODUCTION TO COMPUTER ORGANIZATION AND PROGRAMMING DESIGN Lec. Ghader Kurdi.
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.
Lesson 1: Introduction to ABAP OBJECTS Todd A. Boyle, Ph.D. St. Francis Xavier University.
C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using Functions.
INTRODUCTION TO SAS MACRO PROCESSING James R. Bence, Ph.D., Co-Director Quantitative Fisheries Center Professor Department of Fisheries and Wildlife March.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 3 Simple.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 7 PL/SQL Packages.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
Input, Output, and Processing
Linux Operations and Administration
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.
CPS120: Introduction to Computer Science Decision Making in Programs.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
CPS120: Introduction to Computer Science Lecture 14 Functions.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
BMTRY 789 Lecture 10: SAS MACRO Facility Annie N. Simpson, MSc.
1 CS161 Introduction to Computer Science Topic #9.
Chapter 6 Functions 6.1 Modular Design A valuable strategy when writing complex programs is to break down the program into several smaller modules. A module.
Chapter 4 concerns various SAS procedures (PROCs). Every PROC operates on: –the most recently created dataset –all the observations –all the appropriate.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
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.
Chapter 8 Functions in Depth. Chapter 8 A programmer-defined function is a block of statements, or a subprogram, that is written to perform a specific.
Session 2: PHP Language Basics iNET Academy Open Source Web Development.
C language + The Preprocessor. + Introduction The preprocessor is a program that processes that source code before it passes through the compiler. It.
Shell Scripting September 27, 2004 Class Meeting 6, Part II * Notes adapted by Lenwood Heath from previous work by other members of the CS faculty at Virginia.
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
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.
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.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Chapter 5: Enhancing Your Output with ODS
UNIT - V STORED PROCEDURE.
JavaScript Syntax and Semantics
Chapter 3: Working With Your Data
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
Chapter 24 (4th ed.): Creating Functions
Chapter 4 void Functions
Unit 1: Introduction Lesson 1: PArts of a java program
Macro Variable’s scope
PHP.
Defining and Calling a Macro
Topics Introduction to Functions Defining and Calling a Function
Prof. Arfaoui. COM390 Chapter 7
Topics Introduction to File Input and Output
Presentation transcript:

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 and the University of South Carolina

Macros in SAS In some ways, macros serve similar purposes as functions in R – –They can change a piece of a program and have that change be reflected throughout the program – –They can write repeatable code that can be used in many places efficiently – –They can allow programs to be dependent on data values that may be unknown at the time the program is written

Macros in SAS  Macros are initially odd-looking (all those &’s and %’s) and intimidating—and can stay that way  Macro statements consist of special code that SAS interprets and translates to standard SAS code before executing the program (this is done internally)

Macros in SAS  Macros are subprograms that are placed in key positions in the main SAS program simply by specifying the name of the macro in a special way  Macro variables are similar to SAS variables, but do not belong to a data set. They are typically substituted into the program in key places

Macros in SAS Macros are specified with a % prefix: %macroname; Macro variables are specified with a & prefix: &mymacrovar

Macros in SAS If a macro variable is defined in a macro, it is local—it can only be used in that macro If a macro variable is defined in “open code” (outside any macros), it is global and may be used anywhere

Defining Simple Macro Variables The simplest way to assign a value to a macro variable is using a %LET statement Double quotes are needed for text, because SAS will not find macros inside single quotes %LET macrovarname= value; %LET mypower=3; %LET powerword=cube; Y=X**&mypower; TITLE “Taking the &powerword of each X value” ;

Defining Simple Macro Variables Note that the macro variables are referenced with an ampersand symbol. When SAS sees &macrovarname, it will replace this with the value that was assigned to macrovarname in the &LET statement

Defining Simple Macro Variables This way, if the value needs to be changed, we can change it once (in the %LET statement) rather than everywhere the variable is used To make a variable global, simply define it (with a %LET statement) outside of a macro

Defining Simple Macro Variables %INCLUDE is another simple macro variable that allows us to insert sections of SAS code into a program; it can improve development and readability of long complex programs

Macros as a repeatable set of SAS statements One important use of macros is packaging a piece of SAS code to be placed in various places in the SAS program. A macro has the syntax: %MACRO macroname;..SAS statements %MEND macroname;

Macros as a repeatable set of SAS statements This set of SAS statements that lies between the %MACRO and %MEND statements can be placed anywhere in a program by “invoking” the macro (often after the macro has been defined and initial DATA steps and PROC steps have been run: %macroname

Macros as a repeatable set of SAS statements This serves as a substitute for rewriting all those statements multiple times

Macros with parameters Macros that, when invoked, simply repeat the same statements can be helpful, but not too flexible Using parameters can add flexibility to macros Parameters are macro variables whose values are set each time the macro is invoked

Macros with parameters Parameters of macros are similar to arguments of R functions %MACRO macroname (param1=.., param2=..); SAS statements.. %MEND macroname;

Macros with parameters When invoking the macro, we also provide the values of the parameters %macroname(param1=7.5, param2=January) Within the macro, the parameter name is referred to with an ampersand: Month=&param2;

Conditional Logic in Macros  Macro equivalents of conditions statements exist: %IF, %THEN, %ELSE, %DO, %END.  These are used in a similar fashion as non-macro equivalents, except: –They can only be used within a macro –They can contain entire DATA steps or PROC steps; this extraordinary flexibility is why we need special notation for these statements

Conditional Logic in Macros Automatic macro variables such as &SYSDATE gives current date in character form &SYSDAY gives current day of week can be useful (with conditional logic) to run a SAS program that should do different tasks depending on the date or on the day of the week

CALL SYMPUT The CALL SYMPUT statements assigns a value to a macro variable, typically on the basis of data read in a previous DATA step CALL SYMPUT(“macrovarname”, value);

CALL SYMPUT Read in variables (say, var1, var2, etc.) in a DATA step In a second DATA step, use CALL SYMPUT

CALL SYMPUT Invoke the macro variable (&comparison ) in a separate step (it cannot be invoked in the same DATA step) IF var1>var2 THEN CALL SYMPUT(“comparis on”,”greater”); ELSE CALL SYMPUT(“comparis on”,”less”); IF var1>var2 THEN CALL SYMPUT(“comparis on”,var3);