Storing and Reusing Macros

Similar documents
Macro Facility. About the Macro Facility. Automatic Macro Variables CHAPTER 14

APPENDIX 2 Customizing SAS/ASSIST Software

External Files. Definition CHAPTER 38

Data Representation. Variable Precision and Storage Information. Numeric Variables in the Alpha Environment CHAPTER 9

Creating and Executing Stored Compiled DATA Step Programs

Chapter 28 Saving and Printing Tables. Chapter Table of Contents SAVING AND PRINTING TABLES AS OUTPUT OBJECTS OUTPUT OBJECTS...

Chapter 3 Managing Results in Projects. Chapter Table of Contents

Introduction. LOCK Statement. CHAPTER 11 The LOCK Statement and the LOCK Command

Routing the SAS Log and SAS Procedure Output

Using the SQL Editor. Overview CHAPTER 11

Using Cross-Environment Data Access (CEDA)

Permission Program. Support for Version 6 Only. Allowing SAS/SHARE Client Access to SAS Libraries or Files CHAPTER 40

APPENDIX 4 Migrating from QMF to SAS/ ASSIST Software. Each of these steps can be executed independently.

SAS Catalogs. Definition. Catalog Names. Parts of a Catalog Name CHAPTER 32

Using Data Transfer Services

SAS/ASSIST Software Setup

Formats. Formats Under UNIX. HEXw. format. $HEXw. format. Details CHAPTER 11

Graphics. Chapter Overview CHAPTER 4

Chapter 6 Creating Reports. Chapter Table of Contents

SAS Data Libraries. Definition CHAPTER 26

Tasks Menu Reference. Introduction. Data Management APPENDIX 1

CHAPTER 7 Examples of Combining Compute Services and Data Transfer Services

CHAPTER 13 Importing and Exporting External Data

from the source host, use the FTP put command to copy a file from the source host to the target host.

SAS File Management. Improving Performance CHAPTER 37

DBLOAD Procedure Reference

Customizing Your SAS Session

SAS I/O Engines. Definition. Specifying a Different Engine. How Engines Work with SAS Files CHAPTER 36

SAS Support for TELNET on Windows

QUEST Procedure Reference

Optimizing System Performance

Using SAS Files. Introduction CHAPTER 5

The SAS Interface to REXX

Windows: SPX Access Method

Introduction. Getting Started with the Macro Facility CHAPTER 1

OpenVMS Operating Environment

Data Set Options. Specify a data set option in parentheses after a SAS data set name. To specify several data set options, separate them with spaces.

SAS 101. Based on Learning SAS by Example: A Programmer s Guide Chapter 21, 22, & 23. By Tasha Chapman, Oregon Health Authority

Changes and Enhancements

OS/2: SPX Access Method

Chapter 25 PROC PARETO Statement. Chapter Table of Contents. OVERVIEW SYNTAX SummaryofOptions DictionaryofOptions...

Informats. Informats Under UNIX. HEXw. informat. $HEXw. informat. Details CHAPTER 13

Introduction. CHAPTER 3 Working in the SAS Windowing Environment

Overview. CHAPTER 2 Using the SAS System and SAS/ ASSIST Software

Locking SAS Data Objects

DATA Step Debugger APPENDIX 3

New Macro Features Added in SAS 9.3 and SAS 9.4

Changes and Enhancements

The SERVER Procedure. Introduction. Syntax CHAPTER 8

CHAPTER 7 Using Other SAS Software Products

OS/390 Platform Examples

SAS Strategy Management 5.2 Batch Maintenance Facility

Chapter 27 Saving and Printing Graphics

Introduction. Understanding SAS/ACCESS Descriptor Files. CHAPTER 3 Defining SAS/ACCESS Descriptor Files

Using Dynamic Data Exchange

Using MDP Extensions. What Is the Multidimensional Data Provider? CHAPTER 3

Procedures. PROC CATALOG CATALOG=<libref.>catalog <ENTRYTYPE=etype> <KILL>; CONTENTS <OUT=SAS-data-set> <FILE=fileref;>

SAS. IT Service Level Management 2.1: Migration Documentation

Defining Your Data Sources

Utilizing the Stored Compiled Macro Facility in a Multi-user Clinical Trial Setting

Chapter 2 User Interface Features. networks Window. Drawing Panel

SAS/ACCESS Interface to R/3

Chapter 23 Animating Graphs. Chapter Table of Contents ANIMATING SELECTION OF OBSERVATIONS ANIMATING SELECTED GRAPHS...347

What Is a Communications Access Method? CHAPTER 1 Using Communications Access Methods

Functions and CALL Routines

Functions vs. Macros: A Comparison and Summary

Batch Importing. Overview CHAPTER 4

UNIX Spawner Program. Starting the UNIX Spawner Program CHAPTER 36

Examples That Use Remote Objecting

CHAPTER 5 Macintosh: TCP/IP Access Method

Alternative ODS HTML Statements for Running Examples in Different Operating Environments

Performance Considerations

Introduction to MDDBs

Changes and Enhancements

Routing Output. Producing Output with SAS Software CHAPTER 6

The EXPLODE Procedure

SCL Arrays. Introduction. Declaring Arrays CHAPTER 4

SAS/FSP 9.2. Procedures Guide

UNIX Platform Error Messages

Introduction to the OpenVMS Operating Environment

Executing SAS/AF Applications

Choosing the Right Procedure

SAS Drug Development. SAS Macro API 1.3 User s Guide

The GTESTIT Procedure

SAS Web Infrastructure Kit 1.0. Overview

The correct bibliographic citation for this manual is as follows: SAS Institute Inc Proc EXPLODE. Cary, NC: SAS Institute Inc.

SAS Simulation Studio 14.1: User s Guide. Introduction to SAS Simulation Studio

SAS Drug Development 3.3_03. December 14, 2007

Chapter 7 File Access. Chapter Table of Contents

SAS Clinical Data Integration 2.4

Installation Instructions for SAS/ACCESS Interface to SAP BW

CV2ODBC Procedure. Overview. CV2ODBC Procedure Syntax APPENDIX 4

Using Unnamed and Named Pipes

10 The First Steps 4 Chapter 2

SAS Data Integration Studio 3.3. User s Guide

SAS Model Manager 2.3

Loading Data. Introduction. Understanding the Volume Grid CHAPTER 2

The GANNO Procedure. Overview CHAPTER 12

Macros to Manage your Macros? Garrett Weaver, University of Southern California, Los Angeles, CA

SAS Clinical Data Integration Server 2.1

Transcription:

101 CHAPTER 9 Storing and Reusing Macros Introduction 101 Saving Macros in an Autocall Library 102 Using Directories as Autocall Libraries 102 Using SAS Catalogs as Autocall Libraries 103 Calling an Autocall Macro 103 Saving Macros Using the Stored Compiled Macro Facility 104 Compiling and Storing a Macro Definition 104 Storing Autocall Macros Supplied by SAS Institute 105 Calling a Stored Compiled Macro 105 Introduction When you submit a macro definition, by default, the macro processor compiles and stores the macro in a SAS catalog in the WORK library. These macros, referred to as session compiled macros, exist only during the current SAS session. To save frequently used macros between sessions, you can use either the autocall macro facility or the stored compiled macro facility. The autocall macro facility stores the source for SAS macros in a collection of external files called an autocall library. The autocall facility is useful when you want to create a pool of easily maintained macros in a location that can be accessed by different applications and users. Autocall libraries can be concatenated together. The primary disadvantage of the autocall facility is that the first time that an autocall macro is called in a session, the macro processor compiles it. This compilation is overhead that you can avoid by using the stored compiled macro facility. The stored compiled macro facility stores compiled macros in a SAS catalog in a SAS data library that you specify. By using stored compiled macros, you may save macro compilation time in your production-level jobs. However, because these stored macros are compiled, you must save and maintain the source for the macro definitions in a different location. The autocall facility and the stored compiled macro facility each offer advantages. Some of the factors that determine how you choose to save a macro definition are how often you use a macro, how often you change it, how many users need to execute it, and how many compiled macro statements it has. If you are developing new programs, consider creating macros and compiling them during your current session. If you are running production-level jobs using name-style macros, consider using stored compiled macros. If you are allowing a group of users to share macros, consider using the autocall facility.

102 Saving Macros in an Autocall Library 4 Chapter 9 Note: For greater efficiency, store only name-style macros if you use the stored compiled macro facility. Storing statement-style and command-style macros is less efficient. 4 It is good practice, when you are programming stored compiled macros or autocall macros, to use the %LOCAL statement to define macro variables that will be used only inside that macro. Otherwise, values of macro variables defined outside of the current macro might be altered. See the discussion of macro variable scope in Chapter 5, Scope of Macro Variables. Saving Macros in an Autocall Library Generally, an autocall library is a directory containing individual files, each of which contains one macro definition. In Release 6.11 or later, an autocall library can also be a SAS catalog (see Using SAS Catalogs as Autocall Libraries below). Operating Environment Information: Autocall Libraries on Different Hosts The term directory refers to an aggregate storage location that contains files (or members) managed by the host operating system. Different host operating systems identify an aggregate storage location with different names, such as a directory, a subdirectory, a maclib, a text library, or a partitioned data set. For more information, see the SAS Companion for your operating system. 4 Using Directories as Autocall Libraries To use a directory as a SAS autocall library, do the following: 1 To create library members, store the source code for each macro in a separate file in a directory. The name of the file must be the same as the macro name. For example, the statements defining a macro you would call by submitting %SPLIT must be in a file named SPLIT. Operating Environment Information: Autocall Library Member Names On operating systems that allow filenames with extensions, you must name autocall macro library members with a special extension, usually.sas. Look at the autocall macros on your system provided by SAS Institute to determine whether names of files containing macros must have a special extension at your site. On MVS operating systems, you must assign the macro name as the name of the PDS member. 4 2 Set the SASAUTOS system option to specify the directory as an autocall library. On most hosts, the reserved fileref SASAUTOS is assigned at invocation time to the autocall library supplied by SAS Institute or another one designated by your site. If you are specifying one or more autocall libraries, remember to concatenate the autocall library supplied by the Institute with your autocall libraries so that these macros will also be available. For details, refer to your host documentation and SASAUTOS in Chapter 13, Macro Language Dictionary. When storing files in an autocall library, remember the following: 3 Although the SAS System does not restrict the type of material you place in an autocall library, you should store only autocall library files in it to avoid confusion and for ease of maintenance. 3 Although the SAS System lets you include more than one macro definition, as well as open code, in an autocall library member, you should generally keep only one

Storing and Reusing Macros 4 Calling an Autocall Macro 103 macro in any autocall library member. If you need to keep several macros in the same autocall library member, keep related macros together. Using SAS Catalogs as Autocall Libraries In Release 6.11 or later, you can use the CATALOG access method to store autocall macros as SOURCE entries in SAS catalogs. To create an autocall library using a SAS catalog, follow these steps: 1 Use a LIBNAME statement to assign a libref to the SAS library. 2 Use a FILENAME statement with the CATALOG argument to assign a fileref to the catalog that contains the autocall macros. For example, the following code creates a fileref, MYMACROS, that points to a catalog named MYMACS.MYAUTOS: libname mymacs SAS-data-library ; filename mymacros catalog mymacs.myautos ; 3 Store the source code for each macro in a SOURCE entry in a SAS catalog. (SOURCE is the entry type.) The name of the SOURCE entry must be the same as the macro name. 4 Set the SASAUTOS system option to specify the fileref as an autocall library. For more information, see SASAUTOS in Chapter 13. Calling an Autocall Macro To call an autocall macro, the system options MAUTOSOURCE must be set and SASAUTOS must be assigned. MAUTOSOURCE enables the autocall facility, and SASAUTOS specifies the autocall libraries. For more information on these options, see MAUTOSOURCE and SASAUTOS in Chapter 13. Once you have set the required options, calling an autocall macro is like calling a macro that you have created in your current session. However, it is important that you understand how the macro processor locates the called macro. When you call a macro, the macro processor searches first for a session compiled macro definition. Next, the macro processor searches for a permanently stored compiled macro. If compiled stored macros are enabled with the MSTORED option, the macro processor opens the macro catalog in the library specified in the SASMSTORE option. If the macro processor does not find a compiled macro, and if MAUTOSOURCE is set, the macro processor opens libraries specified by the SASAUTOS option in the order in which they are specified in the option. It then searches each library for a member with the same name as the macro you invoked. When SAS finds a library member with that name, the macro processor does the following: 1 compiles all of the source statements in that member, including any and all macro definitions, and stores the result in the session catalog. 2 executes any open code (macro statements or SAS source statements not within any macro definition) in that member. 3 executes the macro within it with the name you invoked. Note: If an autocall library member contains more than one macro, the macro processor compiles all of the macros but executes only the macro with the name you invoked. 4 Any open code statements in the same autocall library member as a macro execute only the first time you invoke the macro. When you invoke the macro later in the same

104 Saving Macros Using the Stored Compiled Macro Facility 4 Chapter 9 session, the compiled macro is executed, which contains only the compiled macro definition and not the other code the autocall macro source file may have contained. It is not advisable to change SASAUTOS during a SAS session. If you change the SASAUTOS= specification in an ongoing SAS session, the SAS System will store the new specification only until you invoke an uncompiled autocall macro and then will close all opened libraries and open all the newly specified libraries that it can open. For information about debugging autocall macros, see Chapter 10, Macro Facility Error Messages and Debugging. Saving Macros Using the Stored Compiled Macro Facility The stored compiled macro facility compiles and saves compiled macros in a permanent catalog in a library that you specify. This compilation occurs only once. If the stored compiled macro is called in the current or later sessions, the macro processor executes the compiled code. Compiling and Storing a Macro Definition To compile a macro definition in a permanent catalog, create and save the source for each stored compiled macro. To store the compiled macro, follow these steps: 1 Use the STORE option in the %MACRO statement. Optionally, you can assign a descriptive title for the macro entry in the SAS catalog, by specifying the DES= option. For example, the %MACRO statement in the following definition shows the STORE and DES= options: %macro myfiles / store des= Define filenames ; filename file1 external-file-1 ; filename file2 external-file-2 ; %mend; CAUTION: Save your macro source code. You cannot re-create the source statements from a compiled macro. Therefore, you must save the original macro source statements if you want to change the macro. For all stored compiled macros, you should document your macro source code well and save it. It is recommended that you save the source code in the same catalog as the compiled macro. In this example, save it to mylib.sasmacro.myfiles.source 4 2 Set the MSTORED system option to enable the stored compiled macro facility. For more information, see MSTORED in Chapter 13. 3 Assign the SASMSTORE option to specify the SAS data library that contains or will contain the catalog of stored compiled SAS macros. For example, to store or call compiled macros in a SAS catalog named MYLIB.SASMACR, submit these statements. libname mylib SAS-data-library ; options mstored sasmstore=mylib; For more information, see SASMSTORE in Chapter 13.

Storing and Reusing Macros 4 Calling a Stored Compiled Macro 105 4 Submit the source for each macro that you want to compile and permanently store. You cannot move a stored compiled macro to another operating system. You can, however, move the macro source code to another operating system where you can then compile and store it. You may need to recompile these macros if you use them in a different release of the SAS System. For more information, see your host companion. Storing Autocall Macros Supplied by SAS Institute If you use the macros in the autocall library supplied by SAS Institute, you can save macro compile time by compiling and storing those macros in addition to ones you create yourself. Many of the macros related to base SAS software that are in the autocall library supplied by the Institute can be compiled and stored in a SAS catalog named SASMACR by using the autocall macro COMPSTOR that is supplied by SAS Institute. For more information, see COMPSTOR in Chapter 13. Calling a Stored Compiled Macro Once you have set the required system options, calling a stored compiled macro is just like calling session compiled macros. However, it is important that you understand how the macro processor locates a macro. When you call a macro, the macro processor searches for the macro name using this sequence: 1 the macros compiled during the current session 2 the stored compiled macros in the SASMACR catalog in the specified library (if options MSTORED and SASMSTORE= are in effect) 3 each autocall library specified in the SASAUTOS option (if options SASAUTOS= and MAUTOSOURCE are in effect). You can display the entries in a catalog containing compiled macros. For more information, see Chapter 10.

106 Calling a Stored Compiled Macro 4 Chapter 9

The correct bibliographic citation for this manual is as follows: SAS Institute Inc., SAS Macro Language: Reference, Version 8, Cary, NC: SAS Institute Inc., 1999. 310 pages. SAS Macro Language: Reference, Version 8 Copyright 1999 by SAS Institute Inc., Cary, NC, USA. 1-58025-522-1 All rights reserved. Printed in the United States of America. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, by any form or by any means, electronic, mechanical, photocopying, or otherwise, without the prior written permission of the publisher, SAS Institute, Inc. U.S. Government Restricted Rights Notice. Use, duplication, or disclosure of the software by the government is subject to restrictions as set forth in FAR 52.227 19 Commercial Computer Software-Restricted Rights (June 1987). SAS Institute Inc., SAS Campus Drive, Cary, North Carolina 27513. 1st printing, October 1999 SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. indicates USA registration. OS/2 is a registered trademark or trademark of International Business Machines Corporation. Other brand and product names are registered trademarks or trademarks of their respective companies. The Institute is a private company devoted to the support and further development of its software and related services.