ABSTRACT INTRODUCTION THE GENERAL FORM AND SIMPLE CODE

Similar documents
Preserving your SAS Environment in a Non-Persistent World. A Detailed Guide to PROC PRESENV. Steven Gross, Wells Fargo, Irving, TX

SAS Macro Dynamics - From Simple Basics to Powerful Invocations Rick Andrews, Office of the Actuary, CMS, Baltimore, MD

Migration to SAS Grid: Steps, Successes, and Obstacles for Performance Qualification Script Testing

A Mass Symphony: Directing the Program Logs, Lists, and Outputs

Developing Data-Driven SAS Programs Using Proc Contents

Paper CC16. William E Benjamin Jr, Owl Computer Consultancy LLC, Phoenix, AZ

SAS Macro Dynamics: from Simple Basics to Powerful Invocations Rick Andrews, Office of Research, Development, and Information, Baltimore, MD

SAS Drug Development Program Portability

Program Validation: Logging the Log

How to Create Data-Driven Lists

Foundations and Fundamentals. SAS System Options: The True Heroes of Macro Debugging Kevin Russell and Russ Tyndall, SAS Institute Inc.

Excel at Cutting Costs: Combining SAS and Microsoft Excel to Reduce Headcount

Base and Advance SAS

Paper HOW-06. Tricia Aanderud, And Data Inc, Raleigh, NC

BI-09 Using Enterprise Guide Effectively Tom Miron, Systems Seminar Consultants, Madison, WI

A Practical Introduction to SAS Data Integration Studio

Making do with less: Emulating Dev/Test/Prod and Creating User Playpens in SAS Data Integration Studio and SAS Enterprise Guide

DATA Step Debugger APPENDIX 3

Debugging. Where to start? John Ladds, SAS Technology Center, Statistics Canada.

Extending the Scope of Custom Transformations

A Long-Time SAS Programmer Learns New Tricks

SAS Data Libraries. Definition CHAPTER 26

Using the SQL Editor. Overview CHAPTER 11

Patricia Guldin, Merck & Co., Inc., Kenilworth, NJ USA

A Guided Tour Through the SAS Windowing Environment Casey Cantrell, Clarion Consulting, Los Angeles, CA

The %let is a Macro command, which sets a macro variable to the value specified.

A Time Saver for All: A SAS Toolbox Philip Jou, Baylor University, Waco, TX

Going Under the Hood: How Does the Macro Processor Really Work?

A Macro that can Search and Replace String in your SAS Programs

One Project, Two Teams: The Unblind Leading the Blind

Pros and Cons of Interactive SAS Mode vs. Batch Mode Irina Walsh, ClinOps, LLC, San Francisco, CA

SAS/Warehouse Administrator Usage and Enhancements Terry Lewis, SAS Institute Inc., Cary, NC

Validation Summary using SYSINFO

Posters. Workarounds for SASWare Ballot Items Jack Hamilton, First Health, West Sacramento, California USA. Paper

Performance Considerations

Storing and Reusing Macros

Using a Control Dataset to Manage Production Compiled Macro Library Curtis E. Reid, Bureau of Labor Statistics, Washington, DC

Locking SAS Data Objects

New Macro Features Added in SAS 9.3 and SAS 9.4

While You Were Sleeping, SAS Was Hard At Work Andrea Wainwright-Zimmerman, Capital One Financial, Inc., Richmond, VA

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

The DATA Statement: Efficiency Techniques

ABSTRACT: INTRODUCTION: WEB CRAWLER OVERVIEW: METHOD 1: WEB CRAWLER IN SAS DATA STEP CODE. Paper CC-17

SAS File Management. Improving Performance CHAPTER 37

What Is SAS? CHAPTER 1 Essential Concepts of Base SAS Software

Routing Output. Producing Output with SAS Software CHAPTER 6

A Better Perspective of SASHELP Views

ODS DOCUMENT, a practical example. Ruurd Bennink, OCS Consulting B.V., s-hertogenbosch, the Netherlands

SAS CURRICULUM. BASE SAS Introduction

Copy That! Using SAS to Create Directories and Duplicate Files

SAS Institute Exam A SAS Advanced Programming Version: 6.0 [ Total Questions: 184 ]

An Introduction to SAS/FSP Software Terry Fain, RAND, Santa Monica, California Cyndie Gareleck, RAND, Santa Monica, California

QUEST Procedure Reference

Contents of SAS Programming Techniques

Give m Their Way They ll Love it! Sarita Prasad Bedge, Family Care Inc., Portland, Oregon

Useful Tips When Deploying SAS Code in a Production Environment

Technical Paper. Using SAS Studio to Open SAS Enterprise Guide Project Files. (Experimental in SAS Studio 3.6)

Unlock SAS Code Automation with the Power of Macros

While You Were Sleeping - Scheduling SAS Jobs to Run Automatically Faron Kincheloe, Baylor University, Waco, TX

Provisioning Systems and Other Ways to Share the Wealth of SAS Across a Network Jason Losh and Michael King, SAS Institute Inc.

SAS Display Manager Windows. For Windows

Beginning Tutorials. Introduction to SAS/FSP in Version 8 Terry Fain, RAND, Santa Monica, California Cyndie Gareleck, RAND, Santa Monica, California

10 The First Steps 4 Chapter 2

Best Practice for Creation and Maintenance of a SAS Infrastructure

What to Expect When You Need to Make a Data Delivery... Helpful Tips and Techniques

2. Referencing Files and Sepng Op+ons. GIORGIO RUSSOLILLO - Cours de prépara+on à la cer+fica+on SAS «Base Programming» 47

SAS/ACCESS Interface to R/3

General Improvements for Version 7.5

ABSTRACT MORE THAN SYNTAX ORGANIZE YOUR WORK THE SAS ENTERPRISE GUIDE PROJECT. Paper 50-30

Internet, Intranets, and The Web

DSCI 325: Handout 15 Introduction to SAS Macro Programming Spring 2017

Building Sequential Programs for a Routine Task with Five SAS Techniques

A Generalized Macro-Based Data Reporting System to Produce Both HTML and Text Files

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

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

Optimizing System Performance

SAS Macro. SAS Training Courses. Amadeus Software Ltd

Interleaving a Dataset with Itself: How and Why

SAS Visual Analytics Environment Stood Up? Check! Data Automatically Loaded and Refreshed? Not Quite

%Addval: A SAS Macro Which Completes the Cartesian Product of Dataset Observations for All Values of a Selected Set of Variables

CC13 An Automatic Process to Compare Files. Simon Lin, Merck & Co., Inc., Rahway, NJ Huei-Ling Chen, Merck & Co., Inc., Rahway, NJ

Dictionary.coumns is your friend while appending or moving data

From Clicking to Coding: Using ODS Graphics Designer as a Tool to Learn Graph Template Language

Using UNIX Shell Scripting to Enhance Your SAS Programming Experience

So Much Data, So Little Time: Splitting Datasets For More Efficient Run Times and Meeting FDA Submission Guidelines

Paper SAS Programming Conventions Lois Levin, Independent Consultant, Bethesda, Maryland

Simplifying Your %DO Loop with CALL EXECUTE Arthur Li, City of Hope National Medical Center, Duarte, CA

Introduction. Getting Started with the Macro Facility CHAPTER 1

Improving Your Relationship with SAS Enterprise Guide Jennifer Bjurstrom, SAS Institute Inc.

Hidden in plain sight: my top ten underpublicized enhancements in SAS Versions 9.2 and 9.3

CHAPTER 7 Using Other SAS Software Products

Fall 2012 OASUS Questions and Answers

SAS Programming Techniques for Manipulating Metadata on the Database Level Chris Speck, PAREXEL International, Durham, NC

SAS Windowing environment Tips and Tricks

SAS Programming Conventions Lois Levin, Independent Consultant

Why SAS Programmers Should Learn Python Too

Data Quality Review for Missing Values and Outliers

Purchase this book at

A Dynamic Imagemap Generator Carol Martell, Highway Safety Research Center, Chapel Hill, NC

WHAT ARE SASHELP VIEWS?

Transcription:

Paper SA06 Painless Extraction: Options and Macros with PROC PRESENV Keith Fredlund, MS (candidate) Grand Valley State University, Allendale, Michigan; Thinzar Wai, MS (candidate) Grand Valley State University, Allendale, Michigan ABSTRACT The PRESENV procedure was introduced as part of SAS TM version 9.4, but little has been written about the capabilities of this procedure. The PRESENV procedure works in tandem with the PRESENV system option allowing the user to preserve certain global options, macros, macro variables, formats, and temporary data across SAS sessions. The PRESENV option can be turned on and off anytime within a program and a collection of global statements will be collected accordingly. This paper details procedure syntax, provides example code and output, and describes potential uses and limitations. Emphasis within the paper is given to use of the procedure to debug code written by others, macro value evaluation and the creation of a template for other programs. This procedure would be beneficial for users with an intermediate level of SAS data step programming. INTRODUCTION The PRESENV procedure was introduced as part of SAS version 9.4 and gives the user the ability to save most global options (including titles and footnotes), macros, macro variables, formats, and data between SAS sessions. While global statements and macro variables are written to a user-specified file, macros and temporary data sets are saved to a separate auxiliary directory that is stipulated within PROC PRESENV statement. This paper will focus on (a) the use of PROC PRESENV when trying to understand or debug code written by someone else, (b) the use of PROC PRESENV to extract macro values at specific points within a program, and (c) the use of PROC PRESENV to find and group global options used within a program to create a template for other programs. The saved code can then be reintroduced as part of a separate session using a %INC statement. As a shorthand, throughout this paper PROC PRESENV will be referred to as collecting global statements, but bear in mind that it actually collects a wide set of information including macros, macro variables, formats, and data rather than only collecting global statements. THE GENERAL FORM AND SIMPLE CODE In its general form, PROC PRESENV requires an OPTIONS statement and a PROC step surrounding code containing the information to be extracted. The following is the general form of the code: <sas-code-containing-a-macro,-macro-variable,-data-set,-format-or-option> proc presenv permdir= <library-in-which-to-save-temporary-data-set-code-and-macrofile> sascode= <path-on-which-to-save-generated-code> While PROC PRESENV functions best when both PERMDIR= and SASCODE= are both stipulated, the code can be run without either the PERMDIR= statement (if there is no need to preserve temporary data sets created by the code) or without the SASCODE= (if there is no need to extract SAS code that could be used to recover options, macro variables and macros). If the SASCODE= option is used, the extracted output code could be read back into a future program with the use of the code %INC <path-onwhich-to-save-generated-code>. 1

PROC PRESENV has only a single optional argument: SHOW_COMMENTS. This optional argument prompts the PRESENV procedure to display all global statements in the generated code. It is of somewhat modest utility but one possible use is described in further detail later in this paper in the section describing limitations. (See Limitations: Running PROC PRESENV more than once in the same session.) A simple sample code employing PROC PRESENV: %let mv_day= Thursday; proc presenv sascode= 'C:\PROC PRESENV\Simple_Sample_Output.SAS'; Based on this code, SAS would create the SAS file named as Simple_Sample_Output. The created SAS file will contain the following code as the Simple Generated Code: %let saved_options=options %sysfunc(getoption(source)) %sysfunc(getoption(notes)) %sysfunc(getoption(obs,keyword)) %sysfunc(getoption(firstobs,keyword)); options obs=max firstobs=1; options nosource nonotes; %macro dummy_macro_for_presenv; %mend; data _null_; rc=filename('presenvf',' ','temp'); proc printto log=presenvf new; proc sql; create table _data_ as select distinct memname from dictionary.catalogs where libname='permdir' and memtype='catalog'; quit; data _null_; set; call execute( cats('proc catalog force c=permdir.',memname, '; copy out=work.',memname,'; quit;') ); proc delete; proc copy in=permdir out=work mt=data; quit; proc printto; data _null_; rc=filename('presenvf'); data _null_; call symput('mv_day',' Thursday'); call symput('afdsid','0' ); call symput('afdsname',trimn(' ')); call symput('aflib',trimn(' ')); call symput('afstr1',trimn(' ')); call symput('afstr2',trimn(' ')); call symput('fspbdv',trimn(' ')); options _last_=_null_; &saved_options; Please note that the newly created output code includes the macro variable created by the Simple Sample Code (MV_DAY) within the DATA _NULL_ step towards the end of the file. In addition to the 2

macro variable created by the Simple Sample Code, six other automatic variables are included in the Simple Generated Code: AFDSIS, AFDSNAME, AFLIB, AFSTR1, AFSTR2, and FSPBDV. MORE COMPLEX CODE The following example is more complicated, containing an options statement, a macro, macro variables and a temporary data set (hereafter called as the More Complex Code): option mlogic; %let mv_day= Tuesday; data test_data_set; day = "The Day is Tuesday"; %macro m_printday; %if &mv_day = Tuesday %then %do; proc print data = test_data_set; %end; %mend; %m_printday; libname libra 'C:\PROC PRESENV'; filename presfile 'C:\PROC PRESENV\Generated_Code.SAS'; proc presenv permdir=libra sascode=presfile; The code above generates a folder to permanently hold macro code in the folder specified in the LIBNAME statement, creates permanent SAS data sets of all the temporary data sets in the same folder and generates the following code (the More Complex Generated Code): option mlogic; libname libra 'C:\PROC PRESENV'; filename presfile 'C:\PROC PRESENV\Generated_Code.SAS'; %let saved_options=options %sysfunc(getoption(source)) %sysfunc(getoption(notes)) %sysfunc(getoption(obs,keyword)) %sysfunc(getoption(firstobs,keyword)); options obs=max firstobs=1; options nosource nonotes; %macro dummy_macro_for_presenv; %mend; data _null_; rc=filename('presenvf',' ','temp'); proc printto log=presenvf new; proc sql; create table _data_ as select distinct memname from dictionary.catalogs where libname='libra' and memtype='catalog'; quit; data _null_; set; 3

call execute( cats('proc catalog force c=libra.',memname, '; copy out=work.',memname,'; quit;') ); proc delete; proc copy in=libra out=work mt=data; quit; proc printto; data _null_; rc=filename('presenvf'); data _null_; call symput('mv_day','tuesday' ); call symput('afdsid','0' ); call symput('afdsname',trimn(' ')); call symput('aflib',trimn(' ')); call symput('afstr1',trimn(' ')); call symput('afstr2',trimn(' ')); call symput('fspbdv',trimn(' ')); options _last_=work.test_data_set; &saved_options; As with the simple example described earlier, this code can be read into a future program with the use of the %INC <path-to-generated-code> statement. The execution of the More Complex Code also generates a permanent SAS data set with the filename specified in the SASCODE= statement. This permanent SAS data set can be referenced by other SAS programs. A file (sasmacr.sas7bcat) containing all of the macros captured by PROC PRESENV is also created in the folder specified by the PERMDIR= statement. Note that while options, with one exception, are only added to the generated code when OPTION PRESENV is turned on, macros and macro variables will be saved in the macro catalogue (macros) or the generated code (macro variables) even if the macros or macro variables were created before OPTION PRESENV was first turned on. The exception to the way options are stored is that each time the presenv option is turned on or off throughout the program, this is recorded in the generated code as commented code, except for the last time they appear. Options, file references and library references included in the More Complex Code appear at the top of the More Complex Generated Code and macro variables appear near the bottom. LIMITATIONS RUNNING PROC PRESENV MORE THAN ONCE IN THE SAME SESSION Among the limitations to be aware of when running PROC PRESENV is the way the contents of the generated code behave if the PROC PRESENV code is run multiple times during the same session. Rather than over-writing options used within the code, this procedure duplicates options each time it is run. Consequently if the More Complex Code above was run only once, the options section would contain the line of code: option mprint; However, if PROC PRESENV was run a second time, the generated code would include OPTION MPRINT twice: Once at the top of the code and then again a few lines later. Subsequent executions of the More Complex Code would result in more and more repetitions of any options that were a part of the program. 4

The optional argument SHOW_COMMENTS calls attention to each of the duplicated global options by adding three lines of code below each duplicated option: libname libra <library-in-which-to-save-temporary-data-set-code-and-macrofile> ; filename presfile <path-on-which-to-save-generated-code> ; CAPTURING OPTIONS, MACROS, AND MACRO VARIABLES FROM OTHER PROGRAMS RUN DURING THE SAME SESSION Depending on the use to which PROC PRESENV is being put, the user should be aware that after the PRESENV option has been turned on, code being generated will capture only the last iteration of all options, macros and macro variables being employed in other SAS programs within the same session. With options, for instance, if in a program executed earlier in a session, LINESIZE were set to 30, but then in a subsequent program LINESIZE were set to 40, only LINESIZE 40 would be recorded in the code generated by PROC PRESENV. Some of the hurdles of this limitation can be managed by turning on the PRESENV option (OPTION PRESENV) at the beginning of the target program and then turning it off (OPTION NO PRESENV) at the end of the target program. Similarly macros and macro variables from other programs run during the same session will be recorded into the code generated by PROC PRESENV unless they are overwritten by a new macro or macro variable. Care should be taken in regard to the order that programs are run within a session to make sure that the desired macros and macro variables are included in the macro library: PROC CATALOGUE can be used to delete macros from the macro catalogue and macro variables can be deleted using %SYMDEL. The user should also consider that sequential executions of PROC PRESENV within a single session will overwrite previously generated code. Consequently, attention should be given to the order and types of programs that are being run while the PRESENV option is active. GLOBAL OPTIONS NOT SAVED BY PROC PRESENV As might be expected, certain global options are not appropriate for inclusion in the code generated by PROC PRESENV. Table 1 is a comprehensive list. Table 1: Global Statements Not Included in Code Generated by PROC PRESENV Global Statement Category Null Action Comment Page Resetline Skip X Statement Operating Environment 5

CHECKPOINT DM END SAS %INCLUDE EXEC-UTE_ALWAYS %LIST RUN %RUN While the SAS support documentation indicates that PROC PRESENV saves all global statements between one session and another, there are many global statements that are not saved. The omission of specific global statements is understandable: most of the omitted global options come from the Log Control category (COMMENT, PAGE, RESET LINE, SKIP), the Operating Environment category (X Statement) and the category (CHECKPOINT, DM, END SAS, %INCLUDE, EXECUTE_ALWAYS, %LIST, RUN, %RUN. The only omitted global statement that does not fall into a particular category is the Null statement. While most of the options are understandably not preserved between sessions (i.e. there would be little use in including the Null statement), there is, however, an argument for the inclusion of the %INCLUDE option, perhaps only as a comment, to allow the user to have a list of the external programs included in a session. USES OF PROC PRESENV: OVERVIEW While one of the primarily uses of PROC PRESENV is in the context of a SAS Enterprise Guide grid environment, this paper examines some of the other ways that PROC PRESENV can be used. USES OF PROC PRESENV: UNDERSTANDING OR DEBUGGING CODE WRITTEN BY SOMEONE ELSE When encountering a program for the first time, it can be helpful to understand the structure of the code being examined. While some coders provide ample commentary to allow the program to be easily understood, this is not always the case. PROC PRESENV can be used as part of an investigatory process to extract all global statements from within the source program, creating a high-level outline of the program including titles, footnotes, file names, library references and the like that can help a user understand some of the original program structure. It can be helpful during this process to use the SHOW_COMMENTS option with PROC PRESENV as it will clearly reveal in the output whether the option was changed later on within the session. This is only an aid during the process as global statements can be over-written throughout a program. USES OF PROC PRESENV: EXTRACTING MACROS FROM SPECIFIC POINTS IN A PROGRAM A key benefit of PROC PRESENV is that the PRESENV option can be turned on and off repeatedly throughout the program, this provides a method for accumulating the values of different macros and 6

macro variables at a series of specific points throughout the program. This has a somewhat similar utility to the SYMBOLGEN option except that PROC PRESENV allows a somewhat more targeted approach. USES OF PROC PRESENV: FINDING GLOBAL OPTIONS WITHIN A PROGRAM FOR USE ELSEWHERE Programmers sometimes disperse global options throughout a program rather than grouping all of the options, as much as possible, at the beginning of a program. Using PROC PRESENV, a subsequent editor of the program can gather all of these options together into the generated code. This not only allows the programmer to better understand the program, but also would allow this generated code to be easily copied into another program en masse, rather than having to copy multiple sections of code one at a time. CONCLUSION Though not a simple procedure, PROC PRESENV provides a new approach for the creative SAS programmer interested in extracting specific elements of a SAS program. This paper proposes a few specific uses for the procedure and describes in detail its implementation and outputs. REFERENCES Squillace, Jan. Flexibility by Design: A Look at New and Updated System Options in SAS 9.4 http://www.lexjansen.com/wuss/2013/146_paper.pdf (8/30/2016). CONTACT INFORMATION Questions and comments can be directed to the authors at: Keith Fredlund (616) 309-5857 fredlunk@mail.gvsu.edu Thinzar Wai (616) 643-7060 wait@mail.gvsu.edu 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. Other brand and product names are registered trademarks or trademarks of their respective companies. 7