introduction to sas macros center for statistical consulting short course april 15, 2004

31
Introduction to SAS Introduction to SAS Macros Macros Center for Statistical Consulting Short Course April 15, 2004

Upload: bartholomew-dennis

Post on 05-Jan-2016

248 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004

Introduction to SAS MacrosIntroduction to SAS Macros

Center for Statistical Consulting Short Course

April 15, 2004

Page 2: Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004

Getting StartedGetting Started

Download SAS Code from: http://www.uark.edu/csc/shortcourses

In SAS: Tools Options Enhanced Editor, Check Box to show line numbers

Change pathname in LIBNAME statement if needed

Run initial part of code as indicated in SAS file

Page 3: Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004

What do SAS Macros do?What do SAS Macros do?

Write SAS code

Page 4: Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004

Two ExamplesTwo Examples

Simple use of macro variablesBasic macro

Page 5: Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004

Today’s TopicsToday’s Topics

Macro BasicsMacro VariablesMacro ExpressionsDebugging MacrosEfficient Macros

Page 6: Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004

2 Components of Macro Facility2 Components of Macro Facility1. Macro Processor: Does the work

2. Macro Language: Communicates to processor

How do we trigger the processor?How do we trigger the processor?Macro Variable: &nameMacro: %name

Page 7: Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004

Things to RememberThings to Remember

Add MPRINT and/or SERROR to options line to see errors involving the macro facility

The macro processor performs text substitution before any code is compiled

Page 8: Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004

Example 4.0.1

Creating a Simple MacroCreating a Simple Macro

Begin with %MACRO name;End with %MEND name; (name is optional

here)Invoke macro with %name

Page 9: Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004

Example 4.0.2

Creating a Macro VariableCreating a Macro Variable

%LET name-of-variable=value-of-variable;Reference variable with: &name-of-

variable

Page 10: Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004

Example 4.0.3

Adding Comments to MacrosAdding Comments to Macros

/* */ convention works within macros%* ; also comments code within macros

Page 11: Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004

Example 4.0.4

Enclosing Code in MacrosEnclosing Code in Macros

Same as Example 4.0.1Use code between %MACRO and %MEND

statements

Page 12: Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004

Example 4.0.5

Using Parameters in MacrosUsing Parameters in Macros

Parameters allow easy modification of macroNaming conventions for parameters are same

as for dataset variablesDefine parameters in %MACRO statement:

%MACRO name(parameter, parameter, …)Parameters are macro variables

Page 13: Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004

Example 4.0.6

Conditionally Generate CodeConditionally Generate Code

Use %IF/%THEN/%ELSE inside macrosThe condition in %IF statement cannot be

based on a dataset variable

Page 14: Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004

Macro VariablesMacro Variables

Text-only except under certain conditionsGenerated two ways:

– Automatic– User-defined

Scope of macro variables– Global– Local

Page 15: Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004

Example 4.1.1

Automatic Macro VariablesAutomatic Macro Variables

Created when SAS session is startedAvailable anywhere in SAS codeList of automatic macro variables on pp274,

275 in text

Page 16: Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004

User-Defined Macro VariablesUser-Defined Macro Variables

Many ways to create macro variables including:– %LET– Iterative %DO– %GLOBAL– %INPUT– %LOCAL– %MACRO– SYMPUT

Page 17: Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004

Examples from text

Table 3.2 from SAS Doc.Table 3.2 from SAS Doc.To assign… Use…

Constant text %LET

Digits %LET

Arithmetic Expressions %LET with %EVAL or %SYSEVALF function

A null value %LET

A macro variable reference %LET

A macro invocation %LET

Blanks and special characters %LET with %STR function

Value from DATA step CALL SYMPUT routine

Page 18: Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004

Examples 4.1.2, 4.1.3, 4.1.4

Macro Variables with TextMacro Variables with Text

At the end of text string, add macro variable where needed

At beginning of text string, note end of macro variable with a period

Page 19: Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004

Examples 4.1.6, 4.1.7, 4.1.8, 4.1.9

Scope of Macro VariablesScope of Macro VariablesGlobal:

– Available anywhere in SAS Code– Automatic macro variables– User-defined macro variables created with

%LET, %GLOBAL, SYMPUT

Local:– Only available within macro where created– Created with %MACRO, %LET, Iterative

%DO, %LOCAL

Page 20: Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004

3 Types of Macro Expressions3 Types of Macro Expressions

1. Text– Examples: first example, PRINT, YES

2. Arithmetic Expressions– Examples: 1+2, 5*45, 6/7

3. Logical Expressions– Examples: &DAY=WEDNESDAY, C > H, 1

<= &NUM

Page 21: Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004

From Table 6.1, p. 294

Where are Arithmetic and Where are Arithmetic and Logical Operators Used?Logical Operators Used?

Iterative %DO loops%DO %UNTIL and %DO %WHILE%EVAL( ) and %SYSEVALF( )%IF %THEN%SUBSTR( )

Page 22: Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004

Example 4.2.1

Resolution of ExpressionsResolution of Expressions

Text is resolved firstArithmetic and Logical operators are

resolved next (see p. 295 for order)

Page 23: Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004

Examples 4.2.2, 4.2.3

Evaluating Arithmetic Evaluating Arithmetic ExpressionsExpressions

Use %EVAL for integersUse %SYSEVALF for floating point

arithmetic

Page 24: Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004

Example 4.2.4

Logical ExpressionsLogical Expressions

Evaluated by comparing textIf numbers are used, %EVAL is invoked

automaticallyIf non-integers are used, %SYSEVALF

should be coded

Page 25: Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004

Examples 4.3.1, 4.3.2, 4.3.3, 4.3.4

4 Types of Errors4 Types of Errors

1. Macro Variable Resolution– Example: Name of macro variable is misspelled

2. Macro Open Code Processing– Example: Referring to a local macro variable in

open code

3. Macro Compilation– Example: Referring to macro variable created with

SYMPUT within the data step

4. Macro Execution– Example: Forgetting the %MEND statement

Page 26: Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004

Tips for Debugging MacrosTips for Debugging Macros

1. Check the basics: %MEND, semicolons, etc. (see list on p. 309)

2. Use SERROR, MPRINT, MLOGIC, and/or SGEN in OPTIONS line

3. Place %PUT statements in strategic places in code

4. Still having problems? See pp. 310, 311 in text for list of common problems

Page 27: Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004

Elements Available in Open CodeElements Available in Open Code

%*comment;%GLOBAL%LET%PUT%MACRO

Page 28: Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004

Elements Available in MacrosElements Available in Macros

%DOIterative %DO%DO %UNTIL, %DO %WHILE%END;%GOTO and %label%IF %THEN %ELSE%LOCAL%MEND

Page 29: Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004

Example 4.4.1 and p.302 in text

Macro FunctionsMacro Functions

%EVAL, %SYSEVALF%BQUOTE, %NRBQUOTE, %QUOTE,

%NRQUOTE%LENGTH%SUBSTR, %QSUBSTR%UPCASE, %QUPCASE

Page 30: Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004

Examples 4.5.1, 4.5.2

4 Keys to Efficient Macros4 Keys to Efficient Macros

1. Use macros only when necessary

2. Do not nest macros, i.e., create a macro within a macro

3. Assign function results to macro variables

4. Turn off system options when appropriate

Page 31: Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004

More InformationMore Information

Course notes from Stat Packages: http://comp.uark.edu/~duncan/finn636/Stat5322Notes.pdf