3 Macro Parameters.

Slides:



Advertisements
Similar presentations
Haas MFE SAS Workshop Lecture 3:
Advertisements

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Statistical Methods Lynne Stokes Department of Statistical Science Lecture 7: Introduction to SAS Programming Language.
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.
Debugging What can debuggers do? Run programs Make the program stops on specified places or on specified conditions Give information about current variables’
True or false A variable of type char can hold the value 301. ( F )
VBA Modules, Functions, Variables, and Constants
True BASIC Ch. 6 Practice Questions. What is the output? PRINT X LET X = -1 PRINT X FOR X = 4 TO 5 STEP 2 PRINT X NEXT X PRINT X END.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Database Systems More SQL Database Design -- More SQL1.
“SAS macros are just text substitution!” “ARRRRGGHHH!!!”
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.
1 Chapter 5: Macro Programs 5.1 Conditional Processing 5.2 Parameter Validation 5.3 Iterative Processing 5.4 Global and Local Symbol Tables.
Document Type Definitions Kanda Runapongsa Dept. of Computer Engineering Khon Kaen University.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
chap13 Chapter 13 Programming in the Large.
 What is a formula in Excel?  A formula is statement written by the user to be calculated. Formulas can be as simple or as complex as the user wants.
Lecture 4 PL/SQL language. PL/SQL – procedural SQL Allows combining procedural and SQL code PL/SQL code is compiled, including SQL commands PL/SQL code.
Chapter 6: User-Defined Functions
5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.
Define your Own SAS® Command Line Commands Duong Tran – Independent Contractor, London, UK Define your Own SAS® Command Line Commands Duong Tran – Independent.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Guide to Oracle 10g ITBIS373 Database Development Lecture 4a - Chapter 4: Using SQL Queries to Insert, Update, Delete, and View Data.
Macro Variable Resolution Enio Presutto York University, Toronto, Canada.
Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,
BMTRY 789 Lecture 10: SAS MACRO Facility Annie N. Simpson, MSc.
Tips & Tricks From your fellow SAS users 9/30/2004.
Variables and control statements in PL\SQL Chapter 10.
Copyright © 2010, SAS Institute Inc. All rights reserved. SAS ® Using the SAS Grid.
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.
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.
1 CS 430 Database Theory Winter 2005 Lecture 13: SQL DML - Modifying Data.
Based on Learning SAS by Example: A Programmer’s Guide Chapters 1 & 2
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapter 25 By Tasha Chapman, Oregon Health Authority.
 The real power of PHP comes from its functions; it has more than 1000 built-in functions.  PHP User Defined Functions  Besides the built-in PHP functions,
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapters 16 & 17 By Tasha Chapman, Oregon Health Authority.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapters 3 & 4 By Tasha Chapman, Oregon Health Authority.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
More SQL: Complex Queries,
Applied Business Forecasting and Regression Analysis
Chapter 2: Getting Data into SAS
JavaScript: Functions.
Creating Macro Variables in SQL (Review)
3 Macro Storage.
Conditional Processing
Chapter 4: Sorting, Printing, Summarizing
Introduction to Classes
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 ©
Chapter 3 Introduction to Classes, Objects Methods and Strings
Event Driven Programming & User Defined Functions
WEB PROGRAMMING JavaScript.
variables and control statements in PL\SQL
Creating Macro Variables in the DATA Step
Macro Variable’s scope
Chapter 4 Summary Query.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Defining and Calling a Macro
Global and Local Symbol Tables
Retrieving Macro Variables in the DATA Step
3 Iterative Processing.
Using Macros to Solve the Collation Problem
Producing Descriptive Statistics
Passing Simple and Complex Parameters In and Out of Macros
JavaScript: Introduction to Scripting
Introduction to Computer Science
Presentation transcript:

3 Macro Parameters

Define and call macros with parameters Define and call macros with parameters. The difference between positional parameters and keyword parameters.

proc means data=orion.order_item &stats; var &vars; run; %mend calc; %macro calc; proc means data=orion.order_item &stats; var &vars; run; %mend calc; Example: Note macro variable references within the CALC macro.

%let stats=min max; %let vars=quantity; %calc %let stats=n mean; %let vars=discount; Example: Call the macro twice, each time with different values of the macro variables STATS and VARS. The user must submit three lines each time. How can this be simplified?

Macro Parameters %macro calc(stats,vars); proc means data=mac1.order_item &stats; var &vars; run; %mend calc; Example: Define a macro with a parameter list of macro variables referenced within the macro.

Positional Parameters %macro calc(stats,vars); proc means data=orion.order_item &stats; var &vars; run; %mend calc; %calc(min max,quantity) Positional parameters use a one-to-one correspondence between: parameter names supplied on the macro definition parameter values supplied on the macro call

Positional Parameters %MACRO macro-name(parameter-1, … parameter-n); macro text %MEND <macro-name>; Parameter names are In Parentheses Comma delimited. Parameter values can be any text, null values, macro variable references, or macro calls.

Local Symbol Tables When a macro with a parameter list is called, the parameters are created in a separate local symbol table. The macro call %calc(min max, quantity) Global Table Local symbol tables are covered more thoroughly in chapter 5. SYSDAY Tuesday SYSLAST _NULL_ CITY Dallas AMOUNT 975 Local Table STATS min max VARS quantity

Local Symbol Tables A local symbol table is Created when a macro with a parameter list is called Deleted when the macro finishes execution. Macro variables in the local table are available only during macro execution and can be referenced only within the macro. Local symbol tables are short-lived.

Positional Parameters %macro count(opts, start, stop); proc freq data=orion.orders; where order_date between "&start"d and "&stop"d; table order_type / &opts; title1 "Orders from &start to &stop"; run; %mend count; options mprint; %count(nocum,01jan2004,31dec2004) %count(,01jul2004,31dec2004) title; In the second call, a comma is used as a placeholder for the OPTS parameter, which receives a null value. A null value after a slash is not a problem for the PROC FREQ TABLE statement.

proc catalog cat=work.sasmacr; contents; title "My Temporary Macros"; quit; title;

Keyword Parameters

Keyword Parameters %MACRO macro-name(keyword=value, …, keyword=value); macro text %MEND <macro-name>; Keyword parameters are assigned a default value after an equal (=) sign. The main advantage of keyword parameters is that default values can be assigned. If you omit a keyword parameter from a macro call, that parameter will receive whatever default value was specified on the macro definition.

Keyword Parameters %macro-name(keyword=value, …, keyword=value) keyword=value combinations can be specified in any order omitted from the call without placeholders. If omitted from the call, a keyword parameter receives its default value.

Keyword Parameters %macro count(opts=,start=01jan04,stop=31dec04); proc freq data=orion.orders; where order_date between "&start"d and "&stop"d; table order_type / &opts; title1 "Orders from &start to &stop"; run; %mend count; options mprint; %count(opts=nocum) %count(stop=01jul04,opts=nocum nopercent) %count() title; The first parameter has a null value by default. On the final call, all parameters receive their default value. The empty parens are important because this macro "knows" it has a parameter list. If you omit the parens, the macro will not execute but patiently await its expected parameter list. If the next token submitted does not begin a parameter list, the macro will "know" that a parameter list is not forthcoming and will execute using default parameter values. Parens, even if empty, are recommended as explicit, unambiguous, and guarantee immediate execution of the macro.

Mixed Parameter Lists You can use a combination of positional and keyword parameters. In a mixed parameter list, positional parameters must be listed before keyword parameters in both the macro definition and the macro call.

Mixed Parameter Lists Example: Use a combination of positional and keyword parameters. %macro count(opts,start=01jan04,stop=31dec04); proc freq data=orion.orders; where order_date between "&start"d and "&stop"d; table order_type / &opts; title1 "Orders from &start to &stop"; run; %mend count; options mprint; %count(nocum) %count(stop=30jun04,start=01apr04) %count(nocum nopercent,stop=30jun04) %count() title;