Copyright © 2008, SAS Institute Inc. All rights reserved. SAS ® Macros: Top-Five Questions (and Answers!) Kim Wilson –Technical Support Analyst SAS Institute.

Slides:



Advertisements
Similar presentations
1 Jerry Tsai This presentation and code available at: clintuition.com/pubs/
Advertisements

Chapter 9: Introducing Macro Variables 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
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.
Copyright © 2010 SAS Institute Inc. All rights reserved. SAS ® Macros: Top Ten Questions (and Answers!) Kevin Russell –Technical Support Analyst SAS Institute.
The Assembly Language Level
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Macro Processor.
Chapter 11: Creating and Using Macro Programs 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Session 7, Chapter 6 From SAS 9.3 Macro Language Reference Macro Expressions P 71.
A Review. a review of lessons learned so far… ( 2 steps forward - 1 step back) Software Development Cycle: design, implement, test, debug, document Large.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Data types and variables
Chapter 4 Macro Processors
Chapter 2 Data Types, Declarations, and Displays
JavaScript, Third Edition
Guide To UNIX Using Linux Third Edition
Basic Elements of C++ Chapter 2.
“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.
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.
Objectives You should be able to describe: Data Types
SCSUG Avoiding Hand Cramps: Name2 macro and arrays for related variables November 9, am South Center SAS Users Group Austin, Texas.
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.
Writing Maintainable Code with ‘Style’ Allan Page Senior Marketing Analyst Canadian Tire Bank.
Introduction to Shell Script Programming
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Computer Science 101 Introduction to Programming.
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.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
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.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Security Panel Application Introducing the Select Case Multiple-Selection Statement.
Macro Variable Resolution Enio Presutto York University, Toronto, Canada.
Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004.
BMTRY 789 Lecture 10: SAS MACRO Facility Annie N. Simpson, MSc.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 2 Variables.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Macro Processors Basic Functions Machine-Independent Features Design Options Implementation Examples.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapter 25 By Tasha Chapman, Oregon Health Authority.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Using the Macro Facility to Create HTML, XML and RTF output Rick Langston, SAS Institute Inc.
Chapter 2 Variables.
Chapter Topics The Basics of a C++ Program Data Types
Chapter 5: Passing and Processing Macro Parameters
Chapter 6 JavaScript: Introduction to Scripting
Completing the Problem-Solving Process
Basic Elements of C++.
Two “identical” programs
Basic Elements of C++ Chapter 2.
3 Macro Storage.
Conditional Processing
Creating Macro Variables and Assigning Value
Creating Macro Variables in the DATA Step
Macro Variable’s scope
Chapter 2 Variables.
Defining and Calling a Macro
Global and Local Symbol Tables
How Best to Use Macro Quoting Functions?
Chapter 2 Variables.
Presentation transcript:

Copyright © 2008, SAS Institute Inc. All rights reserved. SAS ® Macros: Top-Five Questions (and Answers!) Kim Wilson –Technical Support Analyst SAS Institute Inc.

Copyright © 2008, SAS Institute Inc. All rights reserved. Q: Can you use SAS functions within the macro facility?

Copyright © 2008, SAS Institute Inc. All rights reserved. A: %SYSFUNC Macro Function The %SYSFUNC macro function enables you to execute SAS functions as well as user-written functions. In the following example, %SYSFUNC enables you to execute the DATE function: %put %sysfunc(putn(%sysfunc(date()),worddate20.)); This %PUT statement generates the following output: 95 %put %sysfunc(putn(%sysfunc(date()),worddate20.)); February 10, 2009

Copyright © 2008, SAS Institute Inc. All rights reserved. Q: What quoting function should I use to mask special characters such as the ampersand, percent sign, parentheses, and quotation marks?

Copyright © 2008, SAS Institute Inc. All rights reserved. A: Compile-Time Quoting Functions or Execution-Time Quoting Functions To mask special characters, one of the following types of quoting functions is required. Which type of quoting function you use depends on the situation:  Compile-time quoting functions: %STR–masks commas, mnemonics, and unmatched quotation marks and parentheses %NRSTR–masks percent signs and ampersands  Execution-time quoting functions: %BQUOTE–masks special characters and mnemonics in resolved values during macro execution %SUPERQ–masks special characters and mnemonics during macro execution. However, it prevents resolution of the value

Copyright © 2008, SAS Institute Inc. All rights reserved. Example 1: Unmatched Quotation Mark (‘) The following %LET statement contains a single quotation mark in the macro variable value: %let singleq=O'neill; %put &singleq; When you submit these statements, you do not receive any output because SAS expects a matching closing quotation mark.

Copyright © 2008, SAS Institute Inc. All rights reserved. Solution: Use the %STR Function To solve this problem, include the %STR function around the SINGLEQ macro variable’s value: %let singleq=%str(O%'neill) %put &singleq; Using %STR results in the following output: 3120 %put &singleq; O'neill

Copyright © 2008, SAS Institute Inc. All rights reserved. Example 2: Percent Sign (%) The following %LET statement contains a percent sign in the macro variable value: %let ex=This macro is called %showme; %put ex=&ex; When you submit these statements, SAS cannot resolve the macro invocation and you receive the following warnings: 20 %let ex= This macro is called %showme; WARNING: Apparent invocation of macro SHOWME not resolved. 21 %put ex=&ex; WARNING: Apparent invocation of macro SHOWME not resolved.

Copyright © 2008, SAS Institute Inc. All rights reserved. 23 %let ex=%nrstr(This macro is called %showme); 24 %put ex=&ex; ex=This macro is called %showme Solution: Use the %NRSTR Function To solve this problem, include the %NRSTR function around the SINGLEQ macro variable’s value: %let ex=%nrstr(This macro is called %showme); %put ex=&ex; Using %NRSTR results in the following output:

Copyright © 2008, SAS Institute Inc. All rights reserved. 31 %put WITH NO QUOTING FUNCTION:; WITH NO QUOTING FUNCTION: 32 %test(&x); ERROR: More positional parameters found than defined. Example 3: Commas (,) In the following %LET statement, commas are used as separators between the values A, B, and C : %macro test(value); %put &value; %mend; %let x=a,b,c; %put WITH NO QUOTING FUNCTION:; %test(&x); SAS interprets these commas as additional parameters, and you receive the following error message:

Copyright © 2008, SAS Institute Inc. All rights reserved. 34 %put WITH THE CORRECT QUOTING FUNCTION:; WITH THE CORRECT QUOTING FUNCTION: 35 %test(%bquote(&x)) a,b,c Solution: Use the %BQUOTE Function To solve this problem, mask the commas by using the %BQUOTE execution-time function: %put WITH THE CORRECT QUOTING FUNCTION:; %test(%bquote(&x)); Using %BQUOTE results in the following output:

Copyright © 2008, SAS Institute Inc. All rights reserved. In the following CALL statement, an ampersand is used to replace the word and in the value Beer&Brats : data _null_; call symputx('milwaukee','Beer&Brats'); run; %put NOT quoted:; %put &milwaukee; When you submit these statements, the macro processor interprets the ampersand as an indication that the name following it is a symbolic reference for a macro variable entry. The processor cannot find the entry in the macro symbols table, so you receive the following warning: 5 %put NOT quoted:; NOT quoted: 6 %put &milwaukee; WARNING: Apparent symbolic reference BRATS not resolved. Beer&Brats Example 4: Ampersand (&)

Copyright © 2008, SAS Institute Inc. All rights reserved. 8 %put THIS is quoted:; THIS is quoted: 9 %put %superq(milwaukee); Beer&Brats Solution: Use the %SUPERQ Function To solve this problem, include the %SUPERQ function around the macro variable: %put THIS is quoted:; %put %superq(milwaukee); %SUPERQ prevents the macro processor from attempting to resolve &Brats, and you receive the following output:

Copyright © 2008, SAS Institute Inc. All rights reserved. Q: Output that was generated with the MPRINT system option looks fine, so why are errors generated?

Copyright © 2008, SAS Institute Inc. All rights reserved. Quoting functions, such as the %STR function in the following example, can cause problems with output generated with the MPRINT option: options mprint; %macro test; %let val=aaa; %let test = %str(%'&val%'); data _null_; val = &test; put val=; run; %mend test; %test The output generated is as follows: Note: Line generated by the macro variable “TEST”. 1 ‘aaa’ ERROR : Expecting an arithmetic expression. ERROR : The option or parameter is not recognized and will be ignored.

Copyright © 2008, SAS Institute Inc. All rights reserved. To solve this problem, use the %UNQUOTE function: options mprint; %macro test; %let val = aaa; %let test = %unquote(%str(%'&val%')); OR data _null_; val = %unquote(&test); put val=; run; %mend test; %test This example outputs VAL=‘aaa’, and no error messages are generated. A: %UNQUOTE Function

Copyright © 2008, SAS Institute Inc. All rights reserved. Q: How do you conditionally execute a macro from within a DATA step?

Copyright © 2008, SAS Institute Inc. All rights reserved. A: CALL EXECUTE Routine Use the CALL EXECUTE routine to conditionally execute a macro from within a DATA step, as shown in this example: /* Compile the macro BREAK. The BYVAL */ /* parameter is generated in the CALL */ /* EXECUTE routine. */ %macro break(byval); data age_&byval; set sorted(where=(age=&byval)); run; %mend; proc sort data=sashelp.class out=sorted; by age; run; options mprint; data _null_; set sorted; by age; if first.age then call execute(‘%break(‘||age||’)’); run; (continued)

Copyright © 2008, SAS Institute Inc. All rights reserved. MPRINT(BREAK): data age_11; MPRINT(BREAK): set sorted(where=(age=11)); MPRINT(BREAK): run; MPRINT(BREAK): data age_12; MPRINT(BREAK): set sorted(where=(age=12)); MPRINT(BREAK): run; MPRINT(BREAK): data age_13; MPRINT(BREAK): set sorted(where=(age=13)); MPRINT(BREAK): run; MPRINT(BREAK): data age_14; MPRINT(BREAK): set sorted(where=(age=14)); MPRINT(BREAK): run; MPRINT(BREAK): data age_15; MPRINT(BREAK): set sorted(where=(age=15)); MPRINT(BREAK): run; MPRINT(BREAK): data age_16; MPRINT(BREAK): set sorted(where=(age=16)); MPRINT(BREAK): run; A: CALL EXECUTE Routine Output generated by the previous program:

Copyright © 2008, SAS Institute Inc. All rights reserved. Q: Can I use DATA step variables in a %IF-%THEN statement?

Copyright © 2008, SAS Institute Inc. All rights reserved. A: Data Step Variables Cannot Be Used in %IF- %THEN Statements In the following example, the %IF condition contains the DATA step variable named TEXT: %macro test; data sample; text=“OPEN”; %if TEXT=OPEN %then %do; %put TRUE?; %end; run; %mend; This is a literal comparison, and TEXT=OPEN will never be true.

Copyright © 2008, SAS Institute Inc. All rights reserved.  This statement is the part of the SAS macro language that conditionally generates text.  An expression can only contain operands that are constant text or text expressions that generate text.  This statement cannot refer to DATA step variables.  This statement executes during macro execution. If the statement is contained in a DATA step, it is executed before DATA step compilation.  This statement is the part of the SAS language that conditionally executes SAS statements during DATA step execution.  An expression can only contain operands that are DATA step variables, character or numeric constants, or date and time constants.  This statement executes during DATA step execution. If the statement is contained within a macro, it is stored as text. %IF-%THEN Statement IF-THEN Statement Comparison of %IF-%THEN Statement with IF-THEN statement

Copyright © 2008, SAS Institute Inc. All rights reserved. Recommended Reading Burlew, Michelle SAS Macro Programming Made Easy, Second Edition. Cary, NC: SAS Press. Carpenter, Art Carpenter's Complete Guide to the SAS ® Macro Language, Second Edition. Cary, NC: SAS Press. Tyndall, Russ. Give Your Macro Code an Extreme Makeover: Tips for even the most seasoned macro programmer. Cary, NC: SAS Institute Inc. Available at support.sas.com/techsup/technote/ts739.pdf.

Copyright © 2008, SAS Institute Inc. All rights reserved. Questions?

Copyright © 2008, SAS Institute Inc. All rights reserved.