5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

47
5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran

Transcript of 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

Page 1: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

5/30/2010

SAS Macro Language

Group 6Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran

Page 2: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

5/30/2010

Index• What is a SAS Macro?• Defining & Calling a Macro• Macro Compilation• Defining & Calling Macros with Positional & Keyword

Parameters • Global & Local Macro Symbol Table•  Stored Compiled Macro Facility• The Autocall Facility

Page 3: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

5/30/2010

• What is a SAS Macro?• Defining & Calling a Macro• Macro Compilation

Page 4: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

5/30/2010

What is a Macro?• It is a piece of program that contain complex logic including Data

steps or Proc steps , macro statements, macro variables or any combination of these four.

• Macro Statements :● begin with % e.g. %IF-%THEN/%ELSE , %DO-%END● are passed on to Macro Processor.• Macro Variables:

● begin with &. e.g. &Sales ● are passed on to Macro Processor.• Macro Statements, macro variables together form Macro Language

statements.

Page 5: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

5/30/2010

Defining & Calling a MacroGeneral form: %MACRO macro-name;macro-text%MEND <macro-name>;Example:%macro sales;proc print data=bicycles;var country model totalsales;sum totalsales;where Country="&country" and Model="&model";run;%mend;

Macro Sales is created and is stored in the temporary catalogwork.sasmacr by default.

Page 6: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

5/30/2010

OPTIONS MCOMPILENOTE=ALL |NONE issues a note to SAS log after a macro definition is compiled. Default is NONE.

How to call a Macro?• By name i.e. %macro-name• % sign at the beginning and without the semicolon at the end.• It executes the macro.• Can be made anywhere in a program.• Represents a macro trigger.

Example: %Sales

Defining & Calling a Macro

Page 7: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

5/30/2010

Macro CompilationSAS Program:proc print data=bicycles;var country model totalsales;sum totalsales;where Country="United Kingdom" and Model="Road Bike";run;

SAS program is submitted

Does it contain % or &?

Yes

No

Program is compiled and executed

Nothing happens at this end

Page 8: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

5/30/2010

Macro CompilationSAS program with Macro:%let country=United Kingdom;%let model=Road Bike;%sales;

SAS program is submitted

Does it contain % or &?

Yes

No

Program is compiled and executed

Macro Processoris invoked

%let populates Symbol table

Searches work.sasmacrfor an entry named Sales.MACRO

Page 9: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

5/30/2010

proc print data=bicycles;var country model totalsales;sum totalsales;where Country="&country“and Model="&model";run;

Road BikeModel

United KingdomCountry

Symbol Table

Separates words(Words with &,%

go to Macro processor)

proc print data=bicycles;

Word Scanner

Input Stack

Compiler

Words with &,%

Macro Processor

Page 10: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

5/30/2010

where Country="&country“and Model="&model";run;

Road BikeModel

United KingdomCountry

Symbol Table

Separates words

proc print data=bicycles;var country model totalsales;sum totalsales;where Country=“United Kingdom

Word Scanner

Input Stack

Compiler

&country

Macro Processor

Goes to Symbol Table and replaces

Page 11: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

5/30/2010

where Country="&country“and Model="&model";run;

Road BikeModel

United KingdomCountry

Symbol Table

Separates words

proc print data=bicycles;var country model totalsales;sum totalsales;where Country=“United Kingdom”and Model=“Road Bike

Word Scanner

Input Stack

Compiler

&model

Macro Processor

Goes to Symbol Table and replaces

Page 12: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

5/30/2010

Final Program in Compiler:

proc print data=bicycles;var country model totalsales;sum totalsales;where Country=“United Kingdom”;and Model=“Road Bike”;run;

By default this source doesn’t appear in the SAS log.The MPRINT option writes to the SAS log the text sent to SAS compiler.General Form: Options MPRINT | NOPRINT;

Page 13: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

5/30/2010

Macro Parameters

Page 14: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

5/30/2010

Objectives• Review• Defining & Calling Macros with Positional

Parameters • Global & Local Macro Symbol Table• Defining & Calling Macros with Keyword Parameters

Page 15: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

5/30/2010

Review: Macro with Macro Variables

Call the macro twice, each time with different values of the macro variables SUBJECT and NAME. The user must submit three lines each time. How can this be simplified?

%macro claim;%put &name is my &subject..;%mend claim;

%let name=Spotty; %let subject=dog;%claim;

%let name=Mini;%let subject=cat;%claim;

Page 16: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

5/30/2010

Positional Parameters - example

• Note: Positional parameters use a one-to-one correspondence between parameter names and parameter values

%macro claim (name, subject);%put &name is my &subject..;%mend claim;

%claim( Spotty, dog)

Page 17: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

5/30/2010

Positional Parameters in Macro Definition

• Parameter names is supplied in the macro definition

• Parameter names are parenthesized and comma delimited

Syntax: %MACRO macro-name(parameter-1,…parameter-n); macro text %mend <macro-name>;

Page 18: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

5/30/2010

Positional Parameters in Macro Call

• Parameter values are supplied in macro call• Parameter values are parenthesized and delimited• Parameter values can be any

Syntax: %macro-name(value-1,…value-n)

• Text • Null values

• Macro value reference • Macro calls

Page 19: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

5/30/2010

Local symbol Table - Example

Code:

%let subject=dog;%let name=Spotty;

%put &name is my &subject..;

%macro name (name, subject);%put &name is my &subject..;%mend name;

%name(Mini, cat)

%put &name is my &subject..;

Result:

Spotty is my dog.

Mini is my cat.

Spotty is my dog.

Page 20: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

5/30/2010

Local Symbol Tables

Global Macro Symbol Table…SYSDATE9 03MAY2010SYSDAY FridaySYSVER 9.2……

Local Macro Symbol TableNAME SpottySUBJECT Dog….

Page 21: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

5/30/2010

Local Symbol TablesA 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 symbol table • are available only during macro execution• can be referenced only within the macro

Page 22: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

5/30/2010

Keyword Parameters - Example

• Macro version of program 11-9

%macro subset(seed=0,pct=0.1);data subset;set learn.blood;if ranuni(&seed) le &pct;run;%mend subset;

%subset%subset(pct=.2)%subset(pct=.2,seed=1234567)

Page 23: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

5/30/2010

Keyword Parameters in Macro Definition

• Keyword parameters are assigned a default value after an equal(=) sign

Syntax: %MACRO macro-name(keyword=value,…,keyword=value); macro text; %MEND <macro-name>;

Page 24: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

5/30/2010

Keyword Parameters in Macro Call• Keyword=value combination can be

o Specified in any ordero Omitted from the call without placeholdero If omitted from the call, a keyword parameter receives

the default value

Syntax: %macro-name(keyword=value,…keyword=value)

Page 25: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

5/30/2010

Mixed Parameter Lists - Example

%macro subset(dataset, seed=0,pct=0.1);data subset;set &dataset;if ranuni(&seed) le &pct;run;%mend subset;

%subset(learn.blood)%subset(learn.blood, pct=.2)%subset(learn.blood, pct=.2,seed=1234567)

Page 26: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

5/30/2010

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.

Page 27: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

Macro Storage

Page 28: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

Review-temporary storage

Proc catalog Output:

Proc catalog cat=work.sasmacr; contents; title “My Temporary Macros ”; Quit;

Session-compiled macros are stored in the default temporary catalog-work.sasmacr.

Page 29: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

Macro Storage

• Stored compiled macro facility

• The autocall facility

Page 30: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

Stored compiled macro facility

• Stored Compiled macro facility consists of SAS catalogs that contain compiled macro programs.

• Macro source code is NOT stored by default with the compiled macro program.

Page 31: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

Creating Stored compiled macros

• The MSTORED option enables storage of compiled macros in a permanent library.

• The SASMSTORE option designates a permanent library to store the compiled macros.

• libref points to the location of SAS catalog (data library) containing the compiled macro program.

OPTIONS MSTORED SASMSTORE=libref;

Page 32: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

Creating Stored compiled macros

• The STORE option stores the compiled macro in the library indicted by the “SASMSTORE=” system option.

• The SOURCE option tells the macro processor to save a copy of the macro program’s source code in the same macro entry as the compiles macros.

%MACRO macro-name/STORE <source>; macro-text %MEND macro-name;

Page 33: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

Stored compiled macros-Example• Store the CALC macro in a permanent library

• Call the CALC macro in a new SAS session.

options mstored sasmstore=orion; %macro calc /store; proc means data=orion.order_item &stats; var &vars; run; %mend calc;

Options mstored sasmstore=orion; %let stats=min max; %let vars=quantity; %calc

Page 34: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

The autocall facility

• The autocall facility consists of external files or SOURCE entries in SAS catalogs that contain your macro programs.

• When storing a macro in an autocall library, you do NOT need to submit the macro for compilation before you reference the macro program.

• You can make macros accessible to your SAS session or job by concatenating your own autocall library or your organization’s autocall library (or both) with the autocall library supplied with SAS software.

Page 35: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

The Autocall library by SAS• SAS software includes an autocall library of utility macros

Page 36: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

Defining an Autocall Library

To define the autocall library:• Specify the “MAUTOSOURCE” SAS system

option.• Use the “SASAUTOS=” option to identify

autocall library location(s).

Page 37: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

Autocall Facility Options

• The MAUTOSOURCE option controls autocall facility availability.

• The default setting is MAUTOSOURCE;

OPTIONS MAUTOSOURCE; OPTIONS NOMAUTOSOURCE;

Page 38: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

Autocall Facility Options

• The “SASAUTOS=” option specifies the location of autocall macros.

• Library_1 through Library_n are references to source libraries containing macro definitions.

• Specify a source library:placing actual directory name in quotation marksor pointing to it with a fileref.

OPTIONS SASAUTOS=(Library_1,…,Library_n)

Page 39: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

Autocall Facility Options• Windows

: options mautosource sasautos=(‘e:\learn’, sasautos);

options mautosource sasautos=(‘/learn’, ‘!SASROOT/sasautos’);

options mautosource sasautos=(‘my.macros’, sasautos);

• Unix:

• z/OS:

The reserved fileref SASAUTOS is assigned to the autocall library supplied by SAS.

Page 40: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

Save Macros in an Autocall Library

• In a Window or Unix environment, save each macro definition as a separate file within the directory specified in the “SASAUTOS= ”option.

• Filenames have a .sas extension.

• The filename and the macro name match.

• Unix filename are lowercase.

Page 41: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

Save Macros in an Autocall Library

Step2:

options mautosource sasautos=(‘e:\learn’, sasautos);

%macro calc; proc means

data=orion.order_item &stats; var &vars; run; %mend calc;

Step1:

Step3:

Page 42: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

Call macro from the autocall library

options mautosource sasautos=(‘e:\learn’, sasautos);

%let stats=min max; %let vars=quantity; %calc

Step4: call the CALC macro in a new SAS session

Page 43: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

Accessing Autocall Macros

Within the autocall facility in effect, you can call any macro in the autocall library. If you call a macro that was not previously compiled, the macro facility takes these actions:• Searches the autocall library for a member with the

same name as the called macro.• Issues an error message if the member is not found. • Executes the macro source statements to compile

the macro if the member is found.• Calls the macro.

Page 44: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

Calls to macro programs

Fig10.1 in SAS Macro Programming Made Easy

Page 45: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

Macro Storage Advice

Advantages of stored compiled macros:• You avoid re-compiling lengthy macro source code.• Macro source code can be protected or hidden.

Advantages of autocall macros:• They are available cross-platform.• Macro source code can be edited in any text editor

without invoking SAS.

If none of the above considerations apply, use whichever technique is preferred

Page 46: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

Conclusion

•When using the autocall macro facility, it is important to remember: The naming of the macro file and that of the macro itself must be the same. If the name of the program is different from macro we submit, SAS will not find the file in the SASAUTOS directory and therefore never find the macro to compile.•While we can put open code and other macros within the macro file, it is sometimes confusing to the end user to do so. Open code will only execute the first time the macro is compiled rather than each time the macro is called.

Page 47: 5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran.

Reference• Michele M. Burlew, SAS Macro Programming Made Easy, 2006,

pp.270-284

• Ron Cody, Learning SAS by Example. A Programmers  Guide, 2007, pp522-532