pros and cons of interactive sas® mode vs. batch mode ... · 8/31/2010  · 1 pros and cons of...

9
1 Pros and Cons of Interactive SAS® Mode vs. Batch Mode Irina Walsh, ClinOps, LLC, San Francisco, CA ABSTRACT It is my opinion that SAS programs can be developed in either interactive or batch mode and produce the same end results. To achieve identical results in both modes a few rules have to be followed. This paper explores the differences and limitations of developing a program in interactive SAS display manager mode versus batch mode and shows how to work around some of these limitations. The pros and cons of both approaches are discussed. Examples of techniques to achieve successful program execution in both modes are offered. INTRODUCTION The interactive SAS display manager is a powerful tool that allows you the use of explorer, log, output and results windows where you can examine your input or output SAS data, it also lets you use the enhanced editor window and view the log as you work on your code. This set of tools is very accommodating in the process of program development. The SAS Batch mode allows you to run programs in the background while you continue working in Interactive SAS, it also automatically creates a log file and output file, which you can review after the program runs. While Interactive SAS mode and Batch mode each have their advantages, there are considerable differences to pay attention to. This paper highlights the differences and limitations in the use of Interactive SAS Mode vs. Batch Mode and also provides some examples and workarounds to eliminate these differences. It is primarily directed to the novice user of SAS Software. The examples in this paper are processed by using the MS Windows® XP operating system and SAS version 9.1. AUTOEXEC One factor to consider is that, in order to run programs in Batch mode, an AUTOEXEC.SAS file is required, unlike in Interactive mode. However setting up paths, libraries, options and operating environment for programs in one place is a good practice and it is recommended when using interactive mode as well. The following code provides a simple example of AUTOEXEC.SAS. /*define programs directory*/ %let ProjectPath=C:\WUSS2010; %let ProgFld=&ProjectPath.\Programs; %let Reportfld=&ProjectPath.\Reports; %let Logsfld=&ProjectPath.\Programs\Logs_Outputs; /*define macro programs directory*/ filename stdmacro "&ProjectPath.\Macros"; options sasautos = (stdmacro) mautosource; /*define libraries*/ libname rawdata "&ProjectPath\Rawdata"; To run programs in Batch mode, the AUTOEXEC.SAS file should be placed in the same folder where the programs are located. The current directory is the first place where SAS will try to locate the AUTOEXEC file. If you use the same directory structure for all of your projects you can create a dynamic AUTOEXEC file and use it for all of your projects with needed modifications. For example the directory used for this project is:

Upload: others

Post on 11-Aug-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Pros and Cons of Interactive SAS® Mode vs. Batch Mode ... · 8/31/2010  · 1 Pros and Cons of Interactive SAS® Mode vs. Batch Mode Irina Walsh, ClinOps, LLC, San Francisco, CA

1

Pros and Cons of

Interactive SAS® Mode vs. Batch Mode

Irina Walsh, ClinOps, LLC, San Francisco, CA

ABSTRACT

It is my opinion that SAS programs can be developed in either interactive or batch mode and produce the same end results. To achieve identical results in both modes a few rules have to be followed. This paper explores the differences and limitations of developing a program in interactive SAS display manager mode versus batch mode and shows how to work around some of these limitations. The pros and cons of both approaches are discussed. Examples of techniques to achieve successful program execution in both modes are offered.

INTRODUCTION

The interactive SAS display manager is a powerful tool that allows you the use of explorer, log, output and results windows where you can examine your input or output SAS data, it also lets you use the enhanced editor window and view the log as you work on your code. This set of tools is very accommodating in the process of program development. The SAS Batch mode allows you to run programs in the background while you continue working in Interactive SAS, it also automatically creates a log file and output file, which you can review after the program runs. While Interactive SAS mode and Batch mode each have their advantages, there are considerable differences to pay attention to. This paper highlights the differences and limitations in the use of Interactive SAS Mode vs. Batch Mode and also provides some examples and workarounds to eliminate these differences. It is primarily directed to the novice user of SAS Software. The examples in this paper are processed by using the MS Windows® XP operating system and SAS version 9.1.

AUTOEXEC

One factor to consider is that, in order to run programs in Batch mode, an AUTOEXEC.SAS file is required, unlike in Interactive mode. However setting up paths, libraries, options and operating environment for programs in one place is a good practice and it is recommended when using interactive mode as well. The following code provides a simple example of AUTOEXEC.SAS.

/*define programs directory*/ %let ProjectPath=C:\WUSS2010; %let ProgFld=&ProjectPath.\Programs; %let Reportfld=&ProjectPath.\Reports; %let Logsfld=&ProjectPath.\Programs\Logs_Outputs; /*define macro programs directory*/ filename stdmacro "&ProjectPath.\Macros"; options sasautos = (stdmacro) mautosource; /*define libraries*/ libname rawdata "&ProjectPath\Rawdata";

To run programs in Batch mode, the AUTOEXEC.SAS file should be placed in the same folder where the programs are located. The current directory is the first place where SAS will try to locate the AUTOEXEC file. If you use the same directory structure for all of your projects you can create a dynamic AUTOEXEC file and use it for all of your projects with needed modifications. For example the directory used for this project is:

Page 2: Pros and Cons of Interactive SAS® Mode vs. Batch Mode ... · 8/31/2010  · 1 Pros and Cons of Interactive SAS® Mode vs. Batch Mode Irina Walsh, ClinOps, LLC, San Francisco, CA

2

Figure 1 Project Directory Example

Thus a standard approach can be used to create the AUTOEXEC file. The example shown accommodates both the Interactive mode as well as the Batch mode. The environmental variable SAS_EXECFILEPATH takes the values of the file path and filename from the Enhanced Editor during the Interactive mode whereas the SYSIN option specifies a Batch mode file source and its path.

/*Works in batch mode*/

%let seefld=%sysfunc(getoption(SysIn)); /*Works in Interactive Mode*/

%MACRO prgmpath(); %global fldnamep; %let fldnamep= ; %if %length(&seefld)=0 %then %let fldnamep=%sysget(SAS_EXECFILEPATH); %MEND prgmpath; %prgmpath;

%let ProjectPath=%sysfunc(ifc(&seefld= , %sysfunc(tranwrd(&fldnamep.,\Programs\%scan(&fldnamep.,-1,\/),)), %sysfunc(tranwrd(&seefld.,\Programs\%scan(&seefld.,-1,\/),))));

%let ProgFld=&ProjectPath.\Programs; %let Reportfld=&ProjectPath.\Reports; %let Logsfld=&ProjectPath.\Programs\Logs_Outputs; /*define macro programs directory*/ filename stdmacro "&ProjectPath.\Macros"; options sasautos = (stdmacro)mautosource; /*define libraries*/ libname rawdata "&ProjectPath\Rawdata";

Please note- If you run an AUTOEXEC program in Batch mode while Interactive SAS is open or interactively in consecutive SAS sessions, your profile will not be available and you will get the following message in your log:

NOTE: Unable to open SASUSER.REGSTRY. WORK.REGSTRY will be opened instead.

NOTE: All registry changes will be lost at the end of the session.

WARNING: Unable to copy SASUSER registry to WORK registry. Because of this, you

will not see registry customizations during this

session.

NOTE: Unable to open SASUSER.PROFILE. WORK.PROFILE will be opened instead.

NOTE: All profile changes will be lost at the end of the session.

This message is displayed if more than one SAS session is open, -RSASUSER option in the configuration file set to NORSASUSER , or regstry.sas7bitm file is corrupted. To check your option settings, run the following code:

PROC OPTIONS option=rsasuser; run;

Page 3: Pros and Cons of Interactive SAS® Mode vs. Batch Mode ... · 8/31/2010  · 1 Pros and Cons of Interactive SAS® Mode vs. Batch Mode Irina Walsh, ClinOps, LLC, San Francisco, CA

3

The RSASUSER option makes the SASUSER library read-only and makes it available in many SAS sessions. It also allows access to automatic LIBNAME definitions stored in the regstry.sas7bitm file. More information on setting the RSASUSER option is available on the SAS support web site. This message can be ignored if printing preferences and options saved in SASUSER catalog are defined in the AUTOEXEC.SAS file.

/*define options*/ options nodate nonumber linesize=154 pagesize=56; options sysprint="HP Photosmart C4380 series"; options formchar="|----|+|---+=|-/\<>*";

AUTOEXEC.SAS is a default name for the file that executes right after SAS is initialized and before any user input is accepted. However, if you need to use a file, for example named SETUP.SAS, instead of the AUTOEXEC.SAS file, you can create a new action associated with running SAS programs. To create new actions choose Folder Option from the Tools menu in Windows Explorer.

Figure 2 Folder Option Window

Select file type SAS from the File Type tab and press “Advance” button to open the Edit File Type window.

Figure 3 Edit File Type Window

Page 4: Pros and Cons of Interactive SAS® Mode vs. Batch Mode ... · 8/31/2010  · 1 Pros and Cons of Interactive SAS® Mode vs. Batch Mode Irina Walsh, ClinOps, LLC, San Francisco, CA

4

Press “New” button and open the New Action window.

Figure 4 New Action Window

Enter a new action name for example “Batch submit with Setup” under the Action textbox and enter the following code under ‘Application used to perform action:’

C:\PROGRA~1\SAS\SAS9~1.1\SAS.EXE "%1" -nologo -config C:\PROGRA~1\SAS\SAS9~1.1\SASV9.CFG -autoexec Setup.sas

Now when you right click on any of the SAS programs you can choose to run you programs in batch with SETUP.SAS instead of AUTOEXEC file.

Figure 5 Context Menu for SAS Program

These changes can be made directly in your configuration file as well. However it is easier and more straightforward to call it AUTOEXEC.SAS. When you run a program in Batch mode the information from AUTOEXEC file will be included in the log automatically. In Interactive SAS mode you will have to run AUTOEXEC.SAS before you run a program or you can use a %include statement with OPTIONS SOURCE2 at the top of your program in order to include source code from the AUTOEXEC file in the program log.

Page 5: Pros and Cons of Interactive SAS® Mode vs. Batch Mode ... · 8/31/2010  · 1 Pros and Cons of Interactive SAS® Mode vs. Batch Mode Irina Walsh, ClinOps, LLC, San Francisco, CA

5

LOG AND OUTPUT

The main difference to consider is the speed of execution, especially when a program creates a large log file or a large output file. When you run such a program in Interactive mode the log display window and output display windows use bold fonts and colors to make it easy to read and it will take more processing time for writing these outputs whereas the log file and the output file produced in Batch mode do not use colors or bold fonts and take less processing time. One way to produce a log file and an output file when running programs in Interactive SAS display manager is to use PROC PRINTTO. In this case the log will not be printed in the log window, which will save processing time since it is redirected to a log file specified in filename statement.

/*define output files*/

filename output "&Logsfld.\Program1.lst" ; filename logfile "&Logsfld.\Program1.log" ; PROC PRINTTO print=output new; RUN; PROC PRINTTO log=logfile new; RUN;

-------Code Here-----

PROC PRINTTO; RUN;

Another way is to use dm (display manager) commands to save log and output files after processing. In this case the log and the output will be printed in the corresponding windows and processing time will not be decreased. The dm commands work in Interactive mode and will be ignored in Batch mode

%sysfunc(ifc(%length(&seefld)=0 , filename outputl "&Logsfld.\Program1.lst"; filename logfile "&Logsfld.\Program1.log"; dm 'log; file LOGFILE replace' log; dm 'output; file OUTPUTL replace' output; , ));

One of the advantages of Batch mode is that the log and output files are generated automatically. These files are an important part of the program documentation. When we run a programs in Interactive mode we can programmatically redirect and output these files for documentation purposes.

ODS RTF- THINGS TO KNOW

The challenge for a programmer is to try to create a program so it can run in either mode and produce exactly the same output results. Some steps will require applying special programming code in order to produce the same output from Interactive mode and Batch mode. For example, if you use a DATA _NULL_ statement with ODS RTF you might encounter that blank lines are removed from output if you run the program in the Batch mode. For example, if we run the following code in Interactive mode:

DATA class; set sashelp.class; RUN; PROC FORMAT; value $gender 'F'='Female' 'M'='Male'; RUN; PROC TEMPLATE; define style styles.Land7; parent = styles.rtf; replace fonts / 'TitleFont2' = ("Courier New, Courier",7pt)

Page 6: Pros and Cons of Interactive SAS® Mode vs. Batch Mode ... · 8/31/2010  · 1 Pros and Cons of Interactive SAS® Mode vs. Batch Mode Irina Walsh, ClinOps, LLC, San Francisco, CA

6

'TitleFont' = ("Courier New, Courier",7pt) 'StrongFont' = ("Courier New, Courier",7pt) 'EmphasisFont' = ("Courier New, Courier",7pt) 'FixedEmphasisFont' = ("Courier New, Courier",7pt) 'FixedStrongFont' = ("Courier New, Courier",7pt) 'FixedHeadingFont' = ("Courier New, Courier",7pt) 'BatchFixedFont' = ("Courier New, Courier",7pt) 'FixedFont' = ("Courier New, Courier",7pt) 'headingEmphasisFont' =("Courier New, Courier",7pt) 'headingFont' = ("Courier New, Courier",7pt) 'docFont' = ("Courier New, Courier",7pt); replace body from document/ leftmargin = 1 in rightmargin = 1 in topmargin = 1 in bottommargin = 1 in; end; RUN; ods output OneWayFreqs=OneWayFreqs; PROC FREQ data=class; tables sex; format sex $gender.; RUN; ods output close; %let title1=Class 2010; %let linesize=154; %let pagesize=56; options nodate nonumber orientation=landscape ps=&PAGESIZE ls=&LINESIZE; ods rtf file="&Reportfld.\IMode_Output.rtf" style=styles.Land7; ods noresults; ods listing close; DATA _null_; set OneWayFreqs end=lastobs; retain pageno 0; file print linesleft=remain notitles n=1; if _n_=1 then link header; put /@4 F_Sex @20 Frequency @40 Percent; if (lastobs) then link footer; return; header: put _page_; pageno+1; put @1 "&title1." ; put @1 62 *'_' /@4 'Gender' @20 ' Number in Class' @40 'Percent of total (%)' /@1 62 *'_'; return; footer: put @1 62 *'_'

/@1 "&ProjectPath.\Programs\Program1.sas"; return; RUN; ods listing; ods rtf close;

The output produced by the code above:

Page 7: Pros and Cons of Interactive SAS® Mode vs. Batch Mode ... · 8/31/2010  · 1 Pros and Cons of Interactive SAS® Mode vs. Batch Mode Irina Walsh, ClinOps, LLC, San Francisco, CA

7

Figure 6 ODS RTF Output run in Interactive SAS mode

However, when we submit the same code in Batch mode, the blank lines will be eliminated from the output.

Figure 7 ODS RTF run in Batch mode

The ODS ESCAPECHAR-'^' with PUT ‘^\~’ at the place of blank line provides a solution to keep all blank spaces while running program in Batch mode.

ods escapechar='^'; options nodate nonumber orientation=landscape ps=&PAGESIZE ls=&LINESIZE; ods rtf file="&Reportfld.\IMode_Output.rtf" style=styles.Land7; ods noresults; ods listing close; DATA _null_; set OneWayFreqs end=lastobs; retain pageno 0; file print linesleft=remain notitles n=1; if _n_=1 then link header; put '^\~'; put @4 F_Sex @20 Frequency @40 Percent; if (lastobs) then link footer; return; header: put _page_; pageno+1; put @1 "&title1." ; put @1 62 *'_' ; put '^\~' ; put @4 'Gender' @20 ' Number in Class' @40 'Percent of total (%)'; put @1 62 *'_'; return; footer: put @1 62 *'_'; put @1 "&ProjectPath.\Programs\Program1.sas"; return; RUN;

Page 8: Pros and Cons of Interactive SAS® Mode vs. Batch Mode ... · 8/31/2010  · 1 Pros and Cons of Interactive SAS® Mode vs. Batch Mode Irina Walsh, ClinOps, LLC, San Francisco, CA

8

ods listing; ods rtf close;

CONCLUSION

It is my opinion that SAS programs can be developed in either Interactive mode or Batch mode and can produce the same end results. To achieve identical results in both modes you should remember to set all your options, templates, paths, libraries in one place and make them accessible in Interactive mode and in Batch mode. The AUTOEXEC.SAS file should contains all of your settings. If you use special options in your program, make sure to turn them off at the end of the program. You also should know that not all options and environmental variables are available in both modes and use them appropriately. One of the advantages of Interactive SAS mode is a user-friendly environment for development of a program with many tools for customizing your session. The great advantage of the Batch Mode is a considerable decrease in processing time. It is a good practice when you develop your program to make sure it is run in both Interactive mode and Batch mode and produce the same end result. It is important to save log and output files for audit documentation regardless of how you generate and execute your program. By using system options and environmental variables, you can easily control your output and generate the same output result.

Page 9: Pros and Cons of Interactive SAS® Mode vs. Batch Mode ... · 8/31/2010  · 1 Pros and Cons of Interactive SAS® Mode vs. Batch Mode Irina Walsh, ClinOps, LLC, San Francisco, CA

9

REFERENCES

Wei Cheng (2003),”Build a SAS® Development Environment under Windows®” SUGI, Paper 109-28 SAS Community website http://www.sascommunity.org/wiki/Batch_processing_under_Windows Modified on 31 August 2010, at 10:23 SAS Support Tech Notes http://support.sas.com/kb/8/681.html http://support.sas.com/kb/13/838.html Copyright © 2007 SAS Institute Inc. SAS Help documentation: “Using the SAS Editors under Windows”

CONTACT INFORMATION

If you have any comments and questions regarding this paper, please contact: Irina Walsh ClinOps, LLC (Pacific Data Designs, LLC and ClinPro, Inc. are now ClinOps, LLC) 353 Sacramento Street, Suite 800 San Francisco, California 94111 (415) 776-0660 ext. 199 FAX (415) 776-0746 E-mail: [email protected] 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 trademarks of their respective companies.