Presentation is loading. Please wait.

Presentation is loading. Please wait.

INTRODUCTION TO SAS MACRO PROCESSING James R. Bence, Ph.D., Co-Director Quantitative Fisheries Center Professor Department of Fisheries and Wildlife March.

Similar presentations


Presentation on theme: "INTRODUCTION TO SAS MACRO PROCESSING James R. Bence, Ph.D., Co-Director Quantitative Fisheries Center Professor Department of Fisheries and Wildlife March."— Presentation transcript:

1 INTRODUCTION TO SAS MACRO PROCESSING James R. Bence, Ph.D., Co-Director Quantitative Fisheries Center Professor Department of Fisheries and Wildlife March 28, 2008

2 Overview/Outline Learn basics of SAS macro facility Two main things –Macro variables – contain character strings –Macros – functions that can be passed variables Useful for automation of repeated tasks Basic introduction for those who know some SAS but little or nothing about macros.

3 Getting help Open SAS Help and Documentation SAS Products  Base SAS  SAS Macro Reference

4 First use of the macro facility In this example we –Create a macro variable and assign it a value –Resolve the value of the variable –Use the value to do something in SAS

5 Turning a block of code into the value of a macro variable The value of a macro variable can be very long. To read a set of SAS statements each ending in a semicolon into a macro variable we need to use the %str quote function. References within the block of statements are resolved when the value is assigned to the macro variable.

6 Some first macros Starts with %macro name; Ends with %mend; Stuff it does in between Embellishments –One argument –Multiple arguments –Arguments with keywords

7 From SAS help

8

9

10

11 A bit fancier macro Number of arguments not specified via use of parmbuff option Arguments put into automatic variable syspbuff Use of a %do %while loop Use of %eval Use of %scan

12 Building Macro Variables Leading and trailing blanks Imbedded blanks Use of period to force resolution Combining macro variables with and without blanks

13 Scope Local versus global scope Forcing local or global scope Using %put to find out what is going on with specific variables Using %put to get symbol tables

14 Scopes of Macro Variables Previous Page | Next Page | Top of Page Copyright 2003 by SAS Institute Inc., Cary, NC, USA. All rights reserved. From SAS help

15

16 Some debugging tips and tricks Take small steps and work modularly Check for common errors Getting out of macro black holes – *'; *"; *); */; %mend; run; Using system options to get output useful debugging

17 Even more on resolving macro variables && and &&& Using &&& to pass the name of a macro variable and resolve it and then scan through the result (using %scan or %qscan)

18 A bit more on quoting functions Special characters and mnemonics that might need to be masked. Our old problem with ; and solving it with %str. Other quoting functions to solve problems %str cannot.

19 Logical expressions and conditional execution %IF expression %THEN statement; Logical conditions Integer arithmetic in logical conditions Forcing floating point arithmetic by %sysevalf

20 Passing values between macro and data set variables Standard data set variable assignment using macro resolution; Use of symget function to pass macro variable value to data set variable Use of symput routine to pass data set variable to macro variable Use of select as analog of symput for Proc SQL


Download ppt "INTRODUCTION TO SAS MACRO PROCESSING James R. Bence, Ph.D., Co-Director Quantitative Fisheries Center Professor Department of Fisheries and Wildlife March."

Similar presentations


Ads by Google