“SAS macros are just text substitution!” “ARRRRGGHHH!!!”

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

Chapter 3: Editing and Debugging SAS Programs. Some useful tips of using Program Editor Add line number: In the Command Box, type num, enter. Save SAS.
Classes  All code in a Java program is part of a class  A class has two purposes  Provide functions to do work for the programmer  Represent data.
Chapter 11: Creating and Using Macro Programs 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
The Web Warrior Guide to Web Design Technologies
Lecture-5 Though SQL is the natural language of the DBA, it suffers from various inherent disadvantages, when used as a conventional programming language.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Computer Science 1620 Programming & Problem Solving.
Introduction to a Programming Environment
Introduction to SQL Session 1 Retrieving Data From a Single Table.
Introduction to C Programming
QUOTATION This chapter teaches you about a unique feature of the shell programming language: the way it interprets quote characters. Basically, the shell.
Welcome to SAS…Session..!. What is SAS..! A Complete programming language with report formatting with statistical and mathematical capabilities.
 2007 Pearson Education, Inc. All rights reserved C Preprocessor.
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.
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.
SAS Macro: Some Tips for Debugging Stat St. Paul’s Hospital April 2, 2007.
INTRODUCTION TO SAS MACRO PROCESSING James R. Bence, Ph.D., Co-Director Quantitative Fisheries Center Professor Department of Fisheries and Wildlife March.
The Java Programming Language
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Writing Shell Scripts ─ part 3 CSE 2031 Fall October 2015.
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.
Introduction to ABAP Selection Screens. Slide 2 Screens (Types) There are three types of screens Selection screens get parameter input for reports List.
Programming Fundamentals. Today’s Lecture Why do we need Object Oriented Language C++ and C Basics of a typical C++ Environment Basic Program Construction.
SQL Chapter Two. Overview Basic Structure Verifying Statements Specifying Columns Specifying Rows.
Define your Own SAS® Command Line Commands Duong Tran – Independent Contractor, London, UK Define your Own SAS® Command Line Commands Duong Tran – Independent.
Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
Introduction to Computer Programming
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
BMTRY 789 Lecture 11: Debugging Readings – Chapter 10 (3 rd Ed) from “The Little SAS Book” Lab Problems – None Homework Due – None Final Project Presentations.
BMTRY 789 Lecture 10: SAS MACRO Facility Annie N. Simpson, MSc.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
Asking the USER for values to use in a software 1 Input.
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.
Computing with SAS Software A SAS program consists of SAS statements. 1. The DATA step consists of SAS statements that define your data and create a SAS.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapter 25 By Tasha Chapman, Oregon Health Authority.
SAS ® Global Forum 2014 March Washington, DC.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapters 14 & 19 By Tasha Chapman, Oregon Health Authority.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
A MORE COMPLETE ODS REPORT. What we’re going to talk about…  Creating a title page  Creating an introduction page  Creating long-form text descriptions.
PROBLEM SOLVING WARM-UP Fill in the spaces using any operation to solve the following (!, (), -/+,÷,×): = 6.
13 C Preprocessor.
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.
Whatcha doin'? Aims: To start using Python. To understand loops.
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
© 2016 Pearson Education, Ltd. All rights reserved.
Chapter 2, Part I Introduction to C Programming
Two “identical” programs
Writing Shell Scripts ─ part 3
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 ©
C Arrays.
Number and String Operations
Unit 1: Introduction Lesson 1: PArts of a java program
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.
C Preprocessor(CPP).
Macro Variable’s scope
T. Jumana Abu Shmais – AOU - Riyadh
Defining and Calling a Macro
3 Iterative Processing.
Passing Simple and Complex Parameters In and Out of Macros
How Best to Use Macro Quoting Functions?
Introduction to Computer Science
Tips and Tricks for Using Macros to Automate SAS Reporting.
Presentation transcript:

“SAS macros are just text substitution!” “ARRRRGGHHH!!!”

Center of Balance?

Center of Balance!

Introduction Do your SAS jobs require you to change the same pieces of code each reporting period? If someone else had to run your code, would s/he have to search through the entire program to make the necessary updates?

TODAY’S GOAL Learn how to use SAS macro variables to: 1. Reduce programmer intervention 2. Add flexibility to your programs 3. Reduce coding errors

WHY? (I’ve been programming just fine, thank you very much!)

Example 1: data mylib.year_1999; set sashelp.orsales; if year = 1999; run; proc print data = mylib.year_1999 (obs=3); run; We want to run this 4 times, each time with a different year.

Example 1 continued: Change the year in 3 places and rerun: data mylib.year_2000; set sashelp.orsales; if year = 2000; run; proc print data = mylib.year_2000 (obs=3); run;

Example 1 continued: This program is easy to modify, but what about programs that are longer, more complex, and/or shared with other users? Using macro variables will eliminate these concerns.

Example 1 continued: 1. Define the macro variable: %let the_year = 1999 ; macro trigger macro statement macro variable name (you chose it) value of your macro variable

Example 1 continued: 2. Reference (or call) the macro variable: &the_year macro trigger the macro variable you want to call

"Toto, I've a feeling we're not in SAS programming language anymore!"

Programming language versus Macro language SAS programming language is what you already know and use (DATA and PROC statements) The SAS macro language has its own statements and syntax There are a lot of similarities between the two

SAS Macro Language These two characters, % & or triggers, tell SAS that we are using the macro language.

Timing is everything! 1. Execute global statements (options statement, title/footnote) 2. Check for macro triggers (%, &)

Timing is everything! 3. If found, compile/store/resolve/execute all macro definitions/statements/variables/calls 4. Pass results back to the programming language for compiling and executing of SAS statements

Example 1 continued : * define a macro variable for the reporting year; %let the_year = 1999 ; * replace the year by making a call to the macro variable, using &; data mylib.year_1999; data mylib.year_&the_year ; set sashelp.orsales; if year = &the_year ; run; proc print data = mylib.year_&the_year (obs=3); run;

Example 1 continued : Defined macro variable the_year Called macro variable in 3 places with &the_year Macro facility substituted the text 1999 for each instance of &the_year

Example 1 continued : And that’s what is meant by “text substitution”!!

Example 1 continued: After substitution, the result is the program we had in the beginning: data mylib.year_1999; set sashelp.orsales; if year = 1999; run; proc print data = mylib.year_1999 (obs=3); run;

Example 1 concluded: To run for a different year, you only need to change the value of the macro variable and submit. %let the_year = 2000 ; (submit the program too)

View it in the SAS log options symbolgen; Writes results of resolved macro variable to log.

See it in the SAS log proc print data = mylib.year_&the_year (obs=3); run; 270 options symbolgen; 271 proc print data = mylib.year_&the_year (obs=3); SYMBOLGEN: Macro variable THE_YEAR resolves to run;

symbolgen cautions log can become very large if your password is stored as a macro variable it will be displayed in the log To turn off this option: options nosymbolgen;

See it in the SAS log Akin to the PUT statement in a DATA step, the %PUT statement writes the value of the macro variable to the log (does not need to be in a DATA step).

See it in the SAS log %put I am running year &the_year; 44 %put I am running year &the_year; I am running year 1999

See it in the SAS log Notice that there were no quotes around the phrase: I am running year, because it is just a text string.

See it in the SAS log Macro facility doesn’t need to make a distinction between character strings and variable names. Everything is either text or some macro reference/trigger.

Using macro variables: Define macro variables at the top of your program Use normal SAS naming convention Examples: interest_expense_analysis_ver4 _2008_10_SALES

Using macro variables: All macro variable values are text values, including numbers. A call to a macro variable will not be resolved if it is in single quotes. You must use double quotes.

Example 2 options nodate nonumber; proc print data = year_&the_year (obs=3) noobs; var Product_Group Profit; title ' &the_year Profit per Product Group ' ; run;

Example 2: SAS output &the_year Profit per Product Group Product_Group Profit A-Team, Kids Bathing Suits, Kids Eclipse, Kid's Clothes

Example 2 continued options nodate nonumber; proc print data = year_&the_year (obs=3) noobs; var Product_Group Profit; title “ &the_year Profit per Product Group ” ; run;

Example 2 concluded 1999 Profit per Product Group Product_Group Profit A-Team, Kids Bathing Suits, Kids Eclipse, Kid's Clothes

Intermission

An aside To avoid mismatched quotation marks, type the set of quotation marks (or parentheses) at the same time, then fill them.

Aside concluded 1. title “” ; 2. title “Avoid Mismatched Quotation Marks” ; or 1. data temp(()); 2. data temp(rename=(old_var = new_var));

Example 3 Using a macro variable before other text. data mylib.orion_&the_year_sales; set sashelp.orsales; if year = &the_year; run;

Example 3 (SAS LOG) WARNING: Apparent symbolic reference THE_YEAR_SALES not resolved. 24 data mylib.orion_&the_year_sales; ERROR : Syntax error, expecting one of the following ERROR : The option or parameter is not recognized and will be ignored.

Example 3 continued: The macro facility did not know where the end of the macro variable name was. data mylib.orion_&the_year_sales; It continued to read until it came to a space/semi-colon.

Example 3 continued: If text comes immediately after a macro variable, we have to tell SAS where the end of the macro variable name is with a period.

Example 3 continued: data mylib.orion_&the_year. _sales ; The period denotes the end of the macro variable name.

Example 3 continued: There are several characters that denote the end of a macro variable name.. ; % & a space + - * / ** numbers and dates SAS code words…more

Example 3 concluded: When in doubt, put the period in. You won’t get a warning/error by adding it, …only by omitting it

Example 4 %let lib = mylib ; data temp; set &lib. sales_&the_year ; run; (This won’t work)

Example 4 concluded %let lib = mylib ; data temp; set & lib..s ales_&the_year ; run; period for the end of the macro variable period for the two-level libname.dataset

Intro to Macro Programs Goal: Further reduce programmer intervention of changing years: 1999, 2000, 2001, 2002 Add a DO loop step

Intro to Macro Programs This won’t work! do j = 1999 to 2002 ; data mylib.year_j; set sashelp.orsales; if year = j; run; proc print data = mylib.year_j (obs=3); run; end;

Intro to Macro Programs You cannot use DO/END statements outside of the DATA step. But you CAN use macro %DO/%END statements outside of the DATA step!

Creating a Macro (program) Define a macro with a %MACRO statement and the name of the macro. End the macro with %MEND (Macro END) statement.

Creating a Macro %macro annual_sales ; %mend annual_sales;

Creating a Macro (This won’t work) %macro annual_sales ; do j = 1999 to 2002; data mylib.year_&j; set sashelp.orsales; if year = &j; run; end; %mend annual_sales;

Creating a Macro %macro annual_sales ; %do j = 1999 %to 2002; data mylib.year_&j; set sashelp.orsales; if year = &j; run; %end; %mend annual_sales;

Creating a Macro Because the %DO statement is a macro statement, the index variable J is a macro variable. This makes it a very powerful tool.

Creating a Macro %macro annual_sales ; %do j = 1999 %to 2002; data mylib.year_&j; set sashelp.orsales; if year = &j; run; %end; %mend annual_sales;

Invoking a Macro Submit percent sign with the name of the macro: %annual_sales notice no semi-colon

Creating a Macro We’re not much better off than before. Still have hard-coding (1999 and 2002) in our program.

Macro keyword parameters 1. Define the parameters in the %MACRO statement: %macro annual_sales( Start=, Stop= ) ; (Parameter names are specified by the user, separated by commas)

Macro keyword parameters 2. Replace hard-coded values with macro variables of same names as parameters: %do j = &Start %to &Stop ;

Macro keyword parameters 3. Specify values when invoking the macro: %annual_sales(Start= 1999, Stop= 2002 ) Still have hard-coding, possibly buried in long program.

Macro keyword parameters 4. Replace hard-coded values with new macro variables: %annual_sales(Start= &mstart, Stop= &mstop )

Macro keyword parameters 5. Define the new macro variables at top of code: %let mstart= 1999; %let mstop= 2002;

Macro keyword parameters %let mstart=1999; %let mstop=2002; %macro annual_sales(Start=, Stop= ) ; %do j = &Start %to &Stop; data mylib.year_&j; set sashelp.orsales; if year = &j; run; %end; %mend annual_sales; %annual_sales(Start= &mstart, Stop= &mstop )

Log NOTE: There were 912 observations read from the data set SASHELP.ORSALES. NOTE: The data set MYLIB.YEAR_1999 has 228 observations and 8 variables. NOTE: There were 912 observations read from the data set SASHELP.ORSALES. NOTE: The data set MYLIB.YEAR_2002 has 228 observations and 8 variables.

Summary: Macros and Macro Variables: provide control at the top of the program allow you to change multiple occurrences of a text reference all in one line reduce programmer intervention and the chance of errors in your programs AND

Summary: Really are just text substitution!

Recommended books Sas(r) Macro Programming Made Easy, Second Edition Author: Michele M. Burlew Carpenter's Complete Guide to the SAS(R) Macro Language, Second Edition Author: Art Carpenter

Enjoy the ride!

The Author Dianne Piaskoski Chinook Consulting Ltd.