The Ins and Outs of %IF

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

Intro. Scheme Basics. scm> 5 5. scm>

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

Unlock SAS Code Automation with the Power of Macros

Best Practice for Creation and Maintenance of a SAS Infrastructure

SAS Macro. SAS Training Courses. Amadeus Software Ltd

An Introduction to Macros Deb Cassidy

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

SAS Macro Programming for Beginners

Introduction to the SAS Macro Facility

Chapter 2 The SAS Environment

Reliable programming

CHAPTER 7 Using Other SAS Software Products

PharmaSUG Paper AD09

Copy That! Using SAS to Create Directories and Duplicate Files

CS 426 Fall Machine Problem 1. Machine Problem 1. CS 426 Compiler Construction Fall Semester 2017

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

These are notes for the third lecture; if statements and loops.

100 THE NUANCES OF COMBINING MULTIPLE HOSPITAL DATA

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

Functions and Decomposition

1: Introduction to Object (1)

Programming Project 1: Lexical Analyzer (Scanner)

Text Input and Conditionals

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

Table of Laplace Transforms

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

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

Programming Assignment II

COSC 2P91. Bringing it all together... Week 4b. Brock University. Brock University (Week 4b) Bringing it all together... 1 / 22

What you learned so far. Loops & Arrays efficiency for statements while statements. Assignment Plan. Required Reading. Objective 2/3/2018

Macros from Beginning to Mend A Simple and Practical Approach to the SAS Macro Facility

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

Files Arriving at an Inconvenient Time? Let SAS Process Your Files with FILEEXIST While You Sleep

OLAP Drill-through Table Considerations

QUIZ. What is wrong with this code that uses default arguments?

While You Were Sleeping, SAS Was Hard At Work Andrea Wainwright-Zimmerman, Capital One Financial, Inc., Richmond, VA

THE ULTIMATE FACEBOOK AD TEMPLATE LIBRARY. Digital Marketer Increase Engagement Series

Without further ado, let s go over and have a look at what I ve come up with.

CS125 : Introduction to Computer Science. Lecture Notes #4 Type Checking, Input/Output, and Programming Style

Laboratory 1: Eclipse and Karel the Robot

Creating a new form with check boxes, drop-down list boxes, and text box fill-ins. Customizing each of the three form fields.

6.001 Notes: Section 8.1

Sucuri Webinar Q&A HOW TO IDENTIFY AND FIX A HACKED WORDPRESS WEBSITE. Ben Martin - Remediation Team Lead

XP: Backup Your Important Files for Safety

CS2112 Fall Assignment 4 Parsing and Fault Injection. Due: March 18, 2014 Overview draft due: March 14, 2014

Mobile Computing Professor Pushpedra Singh Indraprasth Institute of Information Technology Delhi Andriod Development Lecture 09

Lab 7 Unit testing and debugging

Paper S Data Presentation 101: An Analyst s Perspective

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs.

CS61A Notes Week 6: Scheme1, Data Directed Programming You Are Scheme and don t let anyone tell you otherwise

KEYWORDS Metadata, macro language, CALL EXECUTE, %NRSTR, %TSLIT

Step through Your DATA Step: Introducing the DATA Step Debugger in SAS Enterprise Guide

(Python) Chapter 3: Repetition

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Testing is a very big and important topic when it comes to software development. Testing has a number of aspects that need to be considered.

Gearing Up for Development CS130(0)

The SD-WAN security guide

It s Proc Tabulate Jim, but not as we know it!

One SAS To Rule Them All

Amie Bissonett, inventiv Health Clinical, Minneapolis, MN

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Introduction to SAS. Cristina Murray-Krezan Research Assistant Professor of Internal Medicine Biostatistician, CTSC

Exercises: Instructions and Advice

Acing the QuickBooks Online Certification Exam

the NXT-G programming environment

CS103 Handout 29 Winter 2018 February 9, 2018 Inductive Proofwriting Checklist

Burning CDs in Windows XP

ORB Education Quality Teaching Resources

Programming Assignment I Due Thursday, October 7, 2010 at 11:59pm

Getting Up to Speed with PROC REPORT Kimberly LeBouton, K.J.L. Computing, Rossmoor, CA

Programming Assignment I Due Thursday, October 9, 2008 at 11:59pm

Notes on Simulations in SAS Studio

Game keystrokes or Calculates how fast and moves a cartoon Joystick movements how far to move a cartoon figure on screen figure on screen

It s not the Yellow Brick Road but the SAS PC FILES SERVER will take you Down the LIBNAME PATH= to Using the 64-Bit Excel Workbooks.

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Debugging Your Python Code: For Dummies

BI-09 Using Enterprise Guide Effectively Tom Miron, Systems Seminar Consultants, Madison, WI

Excel Basics: Working with Spreadsheets

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

Assignment II: Foundation Calculator

Here are a couple of warnings to my students who may be here to get a copy of what happened on a day that you missed.

Can you decipher the code? If you can, maybe you can break it. Jay Iyengar, Data Systems Consultants LLC, Oak Brook, IL

Selection the If Statement Try Catch and Validation

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

PAIRS AND LISTS 6. GEORGE WANG Department of Electrical Engineering and Computer Sciences University of California, Berkeley

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...

%MAKE_IT_COUNT: An Example Macro for Dynamic Table Programming Britney Gilbert, Juniper Tree Consulting, Porter, Oklahoma

1.7 Limit of a Function

Notes on Non-Chronologic Backtracking, Implication Graphs, and Learning

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

SAS Training Spring 2006

CS 61A Discussion 8: Scheme. March 23, 2017

Foundations, Reasoning About Algorithms, and Design By Contract CMPSC 122

Welcome to today s Webcast. Thank you so much for joining us today!

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

Homework 7: Subsets Due: 11:59 PM, Oct 23, 2018

Lastly, in case you don t already know this, and don t have Excel on your computers, you can get it for free through IT s website under software.

Stage 11 Array Practice With. Zip Code Encoding

The compiler is spewing error messages.

Transcription:

Paper 1135-2017 The Ins and Outs of %IF M. Michelle Buchecker, ThotWave Technologies, LLC. ABSTRACT Have you ever had your macro code not work and you couldn't figure out why? Even something as simple as %if &sysscp=win %then LIBNAME libref 'c:\temp';? This paper is designed for programmers who know %LET and can write basic macro definitions already. Now let's take your macro skills a step farther by adding to your skillset. The %IF statement can be a deceptively tricky statement due to how we are used to thinking IF statements are processed in the DATA step and how that differs from how %IF statements are processed by the macro processor. Focus areas of this paper will be: Emphasizing the importance of the macro facility as a code generation facility How a DATA step IF statement differs from a macro %IF statement and when to use which Why semicolons can be misinterpreted on a %IF statement INTRODUCTION The macro facility is a text processing language. It is a language unto itself with its own rules and syntax. The results of the macro language are often used to generate SAS programming statements. The first rule that cannot be emphasized enough is hardcode everything first. To make sure your macro program is successful, you first need to make sure that the SAS code you want to generate is correct. For example, LIBNAME libref c:\temp ; If the code doesn t work when you hardcode it, it s certainly not going to work when you add macro code. The purpose of this paper is to figure out after you hardcode your program when to use a %IF statement or a regular IF statement. This will save you countless debugging hours. WHEN TO USE IF VS. %IF There are a lot of subtleties when it comes to differentiating when to use a SAS IF statement versus a macro %IF statement. Table 1 IF vs. %IF Here is a handy chart. Use SAS IF Condition is testing on a DATA step variable Need to conditionally execute SAS code Use macro %IF Need to conditionally execute macro code Need to conditionally send SAS code to the compiler Outside of a DATA step Note what is missing from the table: 1. Condition is testing on a macro variable. Just because you are testing on a macro variable does not necessarily mean you should be using %IF in that situation. It depends on what that macro variable resolves to as well as what action you are performing if the condition is true. 1

2. Inside of a DATA step. Just because you are inside of a DATA step does not necessarily mean you should be using SAS IF in that situation. It depends on what the condition is, as well as what action you are performing if the condition is true. CONDITIONAL TESTING ON A DATA STEP VARIABLE Remember tip #1, Hardcode everything first and make sure it works. For example: set sashelp.heart; if cholesterol > 200 then status='follow up'; Now let s say we created two macro variables: %let factor=cholesterol; %let cholesterol=200; Tip #2 is punch holes in the program and substitute macro variable references. Since we hardcoded a SAS IF statement to test a DATA step variable, we continue to use a DATA step IF statement. We just substitute what we hardcoded. set sashelp.heart; if &factor > &&&factor then status='follow up'; Run; We ve met the rules from the above chart: testing on a DATA step variable, conditionally executing SAS code. CONDITIONALLY EXECUTE MACRO CODE Next let s discuss Need to conditionally execute macro code and Outside of a DATA step. The Outside of a DATA step is the easy one. If you want to execute a PROC step based on a condition, than %IF is the way to go since you cannot embed a PROC step in a DATA step (CALL EXECUTE is an exception to this but is far more complicated). For example: %IF &sysday=friday %THEN %DO; proc means data=sashelp.heart; %END; The Need to conditionally execute macro code gets slightly more complicated. Take the following incorrect example: if &sysday=friday then do; %let status=party!; else do; %let status=not Friday; Regardless of the day of the week, the value of status will always be Not Friday. The reason is that macro code is processed during the tokenization phase. That s the phase where SAS scans the code, looks for syntax errors, and compiles the step. The DATA step won t execute until the step boundary (the 2

RUN statement in this case) is encountered. Below, I ve put a C next to a statement that is compiled only, and a CE next to a statement that is compiled and then immediately executed. C C if &sysday=friday then do; CE %let status=party!; C C else do; CE %let status=not Friday; C C Which means the statements that got compiled first and immediately executed are: %let status=party!; %let status=not Friday; If you just saw that code, you realize that the text Not Friday will always be the last value assigned to the macro variable status. So what happens with the rest of the code? Well once the RUN statement is encountered, the statements that were previously just compiled are now executed. Here is that code: if &sysday=friday then do; else do; There are no statements to execute regardless if the condition is true or false. So essentially the timing is key here. Macro statements compile and execute as soon as they are encountered. To fix this example %if &sysday=friday %then %let status=party!; %else %let status=not Friday; WAIT! What if you have to test on a DATA step variable, but then create a macro variable? Then use CALL SYMPUT. CONDITIONALLY EXECUTE OR SEND SAS CODE Remember the chart from above: Use SAS IF Condition is testing on a DATA step variable Need to conditionally execute SAS code Use macro %IF Need to conditionally execute macro code Need to conditionally send SAS code to the compiler 3

Use SAS IF Use macro %IF Outside of a DATA step Let s compare and contrast Need to conditional execute code vs. Need to conditionally send code to the compiler, as that is a challenge to understand. The question I like to ask is, will it hurt if the compiler sees this code?. If the answer is no, then use a SAS IF statement. If the answer is yes, then use a %IF statement. A good example of this is conflicting statements in the DATA step, such as KEEP statements. If the compiler sees more than one KEEP statement in a step it uses the one with the most variables, which isn t what you want all the time (or you wouldn t have written the other KEEP statement). Example of using both %IF and regular IF in the same step: %macro mtest(type=yes); %if type=yes %then %do; keep chol_status sex weight height total; % %else %do; keep sex weight height; % set patient_info; if chol_status ne ' ' then Total=LDL+HDL; Run; %m SYNTAX OF %IF STATEMENT WHEN SENDING SAS CODE TO COMPILER Because the macro facility is a language that generates code in another language, you have to make sure that the macro language has the correct syntax and the generated code has the correct syntax. Syntax for macro %IF: %IF condition %THEN action; Or %IF condition %THEN %DO; %END; action1; action2; Notice that in the first %IF statement that there is a semicolon that the macro facility needs as part of the syntax for the %IF statement. Going back to the original hardcoded example, LIBNAME libref c:\temp ; notice there is a semicolon that ends the SAS LIBNAME statement. When combining these 2 pieces of code together, some people mistakenly code: %if condition %then LIBNAME libref c:\temp ; There is only one semicolon. So who gets the semicolon? The macro language or the SAS language? What happened to the second semicolon? Remember, we are combining two languages here. The answer is the macro language steals that semicolon. Which means the SAS compiler never sees a semicolon to end the LIBNAME statement. 4

There are three solutions to this. 1. %if condition %then LIBNAME libref 'c:\temp';; In this case the first semicolon is taken by the macro language for the %IF statement, and the second semicolon is passed to the SAS compiler as text. But doesn t it look weird? And if someone inherits this code, they may very likely delete that second semicolon thinking it is a typo. 2. %if condition %then LIBNAME libref 'c:\temp'%str(;); or %if condition %then %str(libname libref 'c:\temp';); In this situation we still have two semicolons. By using the %STR macro quoting function, it tells the macro facility Oh great macro processor: don t treat this semicolon inside the parenthesis as an end of statement, but instead treat it as constant text for you to ignore. So in this case this first semicolon will get passed to the SAS compiler, and the second semicolon is used by the macro facility. It does require use of a macro quoting function though. Quoting functions are complex and may not be suited for inexperienced macro programmers. 3. %if condition %then %do; % LIBNAME 'libref c:\temp'; In this last solution we have the macro facility perfectly happy with the semicolons for the %do and %end statements, and the code inside the %DO block will get passed to the SAS compiler. It is a normallooking statement which makes for easy readability. Now traditional SAS programmers are not necessarily used to having just one action statement inside of a DO block, so this may look peculiar to them. As you can tell, there are pros and cons with each solution. There isn t one best answer, as it is a personal preference. Although I think it is safe to say that solution one is generally the least preferable. I m a fan of solution two, and yet my manager tends to code solution three. So it really is up to you. OTHER DIFFERENCES Because %IF is part of the macro facility, it does not have the robustness that the DATA step IF has when it comes to calculations. For example, the DATA step can easily process IF 10 < var < 25, meaning var is between 10 and 25. However, %IF looks at each component separately. So %IF 10 < &var <25 will always evaluate to TRUE (1) regardless of the value of &var. Let s say &var has a value of 50. The macro facility will first evaluate 10 < 50. The result is TRUE or in the SAS world, 1. It then takes that value, 1, and compares it to 25, e.g. 1 < 25, and that answer is TRUE. The fix is to separate the expression to %IF 10 < &var and &var < 25. Ultimately remember that %IF is a more simplistic form of IF when it comes to doing math. CONCLUSION %IF is part of the macro facility. The purpose of the macro facility is a text processing facility to generate and manipulate SAS code. Don t forget to hardcode everything first. Next. strategically think about what is executable SAS code, and use IF statements on that. Lastly, determine what code may need to be conditionally submitted to the SAS compiler, or conditionally executed by the macro processor and that is more appropriate for %IF. ACKNOWLEDGMENTS I wouldn t have the macro skills I do if it weren t for the expertise of Warren Repole. 5

CONTACT INFORMATION Your comments and questions are valued and encouraged. Contact the author at: Michelle Buchecker ThotWave Technologies mbuchecker@thotwave.com SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. indicates USA registration. Other brand and product names are trademarks of their respective companies. 6