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

Post on 05-Jan-2016

246 Views

Category:

Documents

5 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Introduction to SAS MacrosIntroduction 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

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

Write SAS code

Two ExamplesTwo Examples

Simple use of macro variablesBasic macro

Today’s TopicsToday’s Topics

Macro BasicsMacro VariablesMacro ExpressionsDebugging MacrosEfficient Macros

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

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

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

Example 4.0.2

Creating a Macro VariableCreating a Macro Variable

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

variable

Example 4.0.3

Adding Comments to MacrosAdding Comments to Macros

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

Example 4.0.4

Enclosing Code in MacrosEnclosing Code in Macros

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

statements

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

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

Macro VariablesMacro Variables

Text-only except under certain conditionsGenerated two ways:

– Automatic– User-defined

Scope of macro variables– Global– Local

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

User-Defined Macro VariablesUser-Defined Macro Variables

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

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

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

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

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

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( )

Example 4.2.1

Resolution of ExpressionsResolution of Expressions

Text is resolved firstArithmetic and Logical operators are

resolved next (see p. 295 for order)

Examples 4.2.2, 4.2.3

Evaluating Arithmetic Evaluating Arithmetic ExpressionsExpressions

Use %EVAL for integersUse %SYSEVALF for floating point

arithmetic

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

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

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

Elements Available in Open CodeElements Available in Open Code

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

Elements Available in MacrosElements Available in Macros

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

Example 4.4.1 and p.302 in text

Macro FunctionsMacro Functions

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

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

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

More InformationMore Information

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

top related