Download presentation
Presentation is loading. Please wait.
Published byBrian Edwards Modified over 9 years ago
1
5/30/2010 SAS Macro Language Group 6 Pradnya Nimkar, Li Lin, Linsong Zhang & Loc Tran
2
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
3
5/30/2010 What is a SAS Macro? Defining & Calling a Macro Macro Compilation
4
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.
5
5/30/2010 Defining & Calling a Macro General form: %MACRO macro-name; macro-text %MEND ; 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 catalog work.sasmacr by default.
6
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
7
5/30/2010 Macro Compilation SAS 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
8
5/30/2010 Macro Compilation SAS 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 Processor is invoked %let populates Symbol table Searches work.sasmacr for an entry named Sales.MACRO
9
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
10
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
11
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
12
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;
13
5/30/2010 Macro Parameters
14
5/30/2010 Objectives Review Defining & Calling Macros with Positional Parameters Global & Local Macro Symbol Table Defining & Calling Macros with Keyword Parameters
15
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;
16
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)
17
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 ;
18
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) TextNull values Macro value reference Macro calls
19
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.
20
5/30/2010 Local Symbol Tables Global Macro Symbol Table … SYSDATE9 03MAY2010 SYSDAY Friday SYSVER 9.2 … Local Macro Symbol Table NAME Spotty SUBJECT Dog ….
21
5/30/2010 Local Symbol Tables A 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
22
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)
23
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 ;
24
5/30/2010 Keyword Parameters in Macro Call Keyword=value combination can be o Specified in any order o Omitted from the call without placeholder o If omitted from the call, a keyword parameter receives the default value Syntax: %macro-name(keyword=value,…keyword=value)
25
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)
26
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.
27
Macro Storage
28
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.
29
Macro Storage Stored compiled macro facility The autocall facility
30
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.
31
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;
32
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 ; macro-text %MEND macro-name;
33
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
34
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.
35
The Autocall library by SAS SAS software includes an autocall library of utility macros
36
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).
37
Autocall Facility Options The MAUTOSOURCE option controls autocall facility availability. The default setting is MAUTOSOURCE; OPTIONS MAUTOSOURCE; OPTIONS NOMAUTOSOURCE;
38
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 marks or pointing to it with a fileref. OPTIONS SASAUTOS=(Library_1,…,Library_n)
39
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.
40
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.
41
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 :
42
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
43
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.
44
Calls to macro programs Fig10.1 in SAS Macro Programming Made Easy
45
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
46
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.
47
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.