Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004."— Presentation transcript:

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

2 Getting 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

3 What do SAS Macros do? Write SAS code

4 Two Examples Simple use of macro variables Basic macro

5 Today’s Topics Macro Basics Macro Variables Macro Expressions Debugging Macros Efficient Macros

6 2 Components of Macro Facility 1. Macro Processor: Does the work 2. Macro Language: Communicates to processor How do we trigger the processor? Macro Variable: &name Macro: %name

7 Things 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

8 Example 4.0.1 Creating a Simple Macro Begin with %MACRO name; End with %MEND name; (name is optional here) Invoke macro with %name

9 Example 4.0.2 Creating a Macro Variable %LET name-of-variable=value-of-variable; Reference variable with: &name-of- variable

10 Example 4.0.3 Adding Comments to Macros /* */ convention works within macros %* ; also comments code within macros

11 Example 4.0.4 Enclosing Code in Macros Same as Example 4.0.1 Use code between %MACRO and %MEND statements

12 Example 4.0.5 Using Parameters in Macros Parameters allow easy modification of macro Naming conventions for parameters are same as for dataset variables Define parameters in %MACRO statement: %MACRO name(parameter, parameter, …) Parameters are macro variables

13 Example 4.0.6 Conditionally Generate Code Use %IF/%THEN/%ELSE inside macros The condition in %IF statement cannot be based on a dataset variable

14 Macro Variables Text-only except under certain conditions Generated two ways: – Automatic – User-defined Scope of macro variables – Global – Local

15 Example 4.1.1 Automatic Macro Variables Created when SAS session is started Available anywhere in SAS code List of automatic macro variables on pp274, 275 in text

16 User-Defined Macro Variables Many ways to create macro variables including: – %LET – Iterative %DO – %GLOBAL – %INPUT – %LOCAL – %MACRO – SYMPUT

17 Examples from text 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 stepCALL SYMPUT routine

18 Examples 4.1.2, 4.1.3, 4.1.4 Macro 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

19 Examples 4.1.6, 4.1.7, 4.1.8, 4.1.9 Scope of Macro Variables Global: – 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

20 3 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

21 From Table 6.1, p. 294 Where are Arithmetic and Logical Operators Used? Iterative %DO loops %DO %UNTIL and %DO %WHILE %EVAL( ) and %SYSEVALF( ) %IF %THEN %SUBSTR( )

22 Example 4.2.1 Resolution of Expressions Text is resolved first Arithmetic and Logical operators are resolved next (see p. 295 for order)

23 Examples 4.2.2, 4.2.3 Evaluating Arithmetic Expressions Use %EVAL for integers Use %SYSEVALF for floating point arithmetic

24 Example 4.2.4 Logical Expressions Evaluated by comparing text If numbers are used, %EVAL is invoked automatically If non-integers are used, %SYSEVALF should be coded

25 Examples 4.3.1, 4.3.2, 4.3.3, 4.3.4 4 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

26 Tips 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

27 Elements Available in Open Code %*comment; %GLOBAL %LET %PUT %MACRO

28 Elements Available in Macros %DO Iterative %DO %DO %UNTIL, %DO %WHILE %END; %GOTO and %label %IF %THEN %ELSE %LOCAL %MEND

29 Example 4.4.1 and p.302 in text Macro Functions %EVAL, %SYSEVALF %BQUOTE, %NRBQUOTE, %QUOTE, %NRQUOTE %LENGTH %SUBSTR, %QSUBSTR %UPCASE, %QUPCASE

30 Examples 4.5.1, 4.5.2 4 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

31 More Information Course notes from Stat Packages: http://comp.uark.edu/~duncan/finn636/Stat5 322Notes.pdf


Download ppt "Introduction to SAS Macros Center for Statistical Consulting Short Course April 15, 2004."

Similar presentations


Ads by Google