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 ©

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

VBA Modules, Functions, Variables, and Constants
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
JavaScript, Third Edition
“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 10:Processing Macro Variables at Execution Time 1 STAT 541 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
INTRODUCTION TO SAS MACRO PROCESSING James R. Bence, Ph.D., Co-Director Quantitative Fisheries Center Professor Department of Fisheries and Wildlife March.
Input, Output, and Processing
Linux Operations and Administration
Macro Overview Mihaela Simion. Macro Facility Overview Definition : The SAS Macro Facility is a tool within base SAS software that contains the essential.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
BMTRY 789 Lecture 10: SAS MACRO Facility Annie N. Simpson, MSc.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
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.
Session 2: PHP Language Basics iNET Academy Open Source Web Development.
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.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
SAS and Other Packages SAS can interact with other packages in a variety of different ways. We will briefly discuss SPSSX (PASW) SUDAAN IML SQL will be.
More about comments Review Single Line Comments The # sign is for comments. A comment is a line of text that Python won’t try to run as code. Its just.
Chapter Topics The Basics of a C++ Program Data Types
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Fall 2015
Topics Designing a Program Input, Processing, and Output
User-Written Functions
Chapter 5: Enhancing Your Output with ODS
Chapter 1: Introduction to computers and C++ Programming
Python Let’s get started!.
Introduction to C++ Systems Programming.
Shell Scripting March 1st, 2004 Class Meeting 7.
Basic Elements of C++.
UNIT - V STORED PROCEDURE.
Chapter 2: Getting Data into SAS
JavaScript Syntax and Semantics
Two “identical” programs
Chapter 3: Working With Your Data
Intro to PHP & Variables
MACRO Processors CSCI/CMPE 3334 David Egle.
Basic Elements of C++ Chapter 2.
CS190/295 Programming in Python for Life Sciences: Lecture 1
Conditional Processing
Lesson 4 Using Basic Formulas
Functions BIS1523 – Lecture 17.
Topics Introduction to File Input and Output
Number and String Operations
Chapter 24 (4th ed.): Creating Functions
Chapter 4 void Functions
Variables ICS2O.
Unit 1: Introduction Lesson 1: PArts of a java program
Chapter 1: Computer Systems
Macro Variable’s scope
PHP.
Defining and Calling a Macro
Global and Local Symbol Tables
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
Topics Introduction to Functions Defining and Calling a Function
Topics Designing a Program Input, Processing, and Output
Topics Introduction to File Input and Output
Topics Designing a Program Input, Processing, and Output
Compiler vs linker The compiler translates one .c file into a .o file
Topics Introduction to File Input and Output
CIS 136 Building Mobile Apps
Instructor: Alexander Stoytchev
Presentation transcript:

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 © 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

Macro statements 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) The next several slides provide an overview

Macros vs Macro Variables 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

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

Local and Global Macro Variables 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” ; Run SimpleLETmacro.sas

Macro Variable assignment 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

Macro variable substitution 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 Show IncludeMacro.sas Analogous to LaTeX.

Concatenating Text to Macro Variables Often a useful future when generating data sets and files Add text to the front of a macro variable (textstring&macrovar) Add text to the end of a macro variable ($macrovar.textstring) Resolve embedded periods in file names ($macrovar..txt) Some of our later programs include examples

%INCLUDE %INCLUDE is another simple macro statement 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;

Invoking a macro 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

Repeated macro calls 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

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

Referencing parameters in macros 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; The first call could also be %macroname(7.5,January) Run SafetyMacro.sas referring to equation in Class Exercise 10. Class Exercise 10 reviews the next two macros in detail.

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 Run RegMacro.sas

Automatic macro variables Automatic macro variables such as &SYSDATE stores current date in character form &SYSDAY stores current day of week It can be useful to run a SAS program that performs different tasks depending on the date or day of the week &SYSLAST stores current SAS data set

CALL SYMPUT(“macrovarname”, value); 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); SYMPUTN works for numeric assignments

CALL SYMPUT placement Read in variables (say, var1, var2, etc.) in a DATA step In a second DATA step, use CALL SYMPUT FlowerMacro.sas is identical to book example. I think it’s gratuitous, so I include a non-macro alternative.

Using CALL SYMPUT assignment Invoke the macro variable (&comparison) in a separate step (it cannot be invoked in the same DATA step) IF var1>var2 THEN CALL SYMPUT(“comparison”,”greater”); ELSE CALL SYMPUT(“comparison”,”less”); IF var1>var2 THEN CALL SYMPUT(“comparison”,var3); Run IFMacro.sas and SafetySymputMacro.sas For numeric values, we have to force a numeric format with PUT, then trim leasing spaces.