Minitab Macros 10.1 Introduction to Minitab Macros 10.2 Global Minitab Macros 10.3 Local Minitab Macros 10.4 Advanced Minitab Macro Programming 10.5 Hints on Debugging Macros Introduction to Minitab Macros Objectives Identify the three different types of Minitab macros. Describe the characteristics of the three types of macros. Types of Minitab Macros Global Macro simplest form of a Minitab Macro Local Macro advanced form of Minitab Macro Execs older form of Minitab Macro Global Macros Apply directly to the columns of the current worksheet, matrices and constants. Must be stored in a file. Can include any Minitab session commands. Can include some control statements. Does not accept any arguments or subcommands. Relatively easy to define, but the lack of structure often results in unintended consequences. Local Macros Executes locally, requires local variables to be defined. Must be stored in a file. Can include any Minitab session commands. Can include a variety of control statements. Calls to the Macro can include arguments and/or subcommands. Very flexible, but requires more precise programming.
Execs Oldest form of Minitab Macros. Must be stored in a file. Can include any Minitab session commands. Control statements are not available. Calls to the Exec can imply iterative looping. Executes similar to a Global Macro. Generally considered obsolete, but still in use. Not recommended by Minitab, will not be covered any more in this course. Global Minitab Macros Objectives Define the structure of a Minitab global macro. Illustrate the use of a Minitab global macro with an example. The Structure of a Global Macro GMACRO template body of the macro ENDMACRO GMACRO marks the beginning of the macro. ENDMACRO marks the end of the macro. template the name of the macro. body of the macro Minitab commands, macro statements, and calls to other macros. Example of a Global Macro As stored in the file gmacroex1.mac: GMACRO beginning of macro gmacroex1 template name of macro Random k1 c1; Normal 500 100. body of macro Minitab commands Histogram c1. ENDMACRO end of macro Note 1: This macro requires that k1 be already defined as an integer greater than 1; otherwise the macro will produce an error. Note 2: This macro will overwrite any data that currently resides in column one. Creating and Storing a Global Macro Create a file with a simple editor, such as Notepad. Copy/paste commands from Minitab s Project Manager s History. Add GMACRO, a macro name, and ENDMACRO. Other Hints: Use the same name for the macro as the filename. Use the default extension.mac Be sure to switch to all files when using the SAVE AS TYPE in Notepad; otherwise your file may be named myfile.mac.txt rather than myfile.mac Store the macro in the /MACROS subdirectory of the Minitab Program if you plan to use it often.
Invoking a Global Macro To execute the macro, type: % fullmacroname where fullmacroname is the complete path and name of the macro to be executed. Example: %'C:\My Documents\cqas742\Class 10\gmacroex1.mac Invoking a Global Macro Example: %'C:\My Documents\cqas742\Class 10\gmacroex1.mac Notes: If the file extension is.mac, then it may be omitted. If the file is in the current directory, or in the /MACROS subdirectory of the Minitab Program, then the path may be omitted. If there are no spaces in the filename, of the path, then the single quotes may be omitted. Control Statements in a Global Macro Adding some control statements can make the macro more robust: IF, ELSEIF, ELSE, and ENDIF conditional statements. DO-ENDO statement blocks. CALL and RETURN access to other macros. WHILE-ENDWHILE loops. Adding messages to the user also help with execution: NOTE command to write to the session window. ECHO, NOECHO to control the display of commands. BRIEF 0 and BRIEF 2 to control the display of output from commands. GMACRO beginning of macro gmacroex2 template # checks the data type - 2 means integer # Brief 0 silences the output from the command Brief 0 Dtype k1 k98 # Turn output back on Brief 2 # If command makes sure that k1 is an integer # greater than zero, prints an error otherwise IF k1 < 1 Or K98 ~= 2 body Note K1 must be set to an integer greater than zero # Else is executed if everything is okay... ELSE Random k1 c1; Normal 500 100. Histogram c1. ENDIF As stored in the file: ENDMACRO end of macro lmacroex2.mac A Special Macro STARTUP.MAC STARTUP.MAC is executed whenever Minitab is started. Minitab will search the current directory, and the /MACROS subdirectory of the Minitab program. STARTUP.MAC can be a Global Macro, or a Local Macro. Commands that are might be put in STARTUP.MAC: OW Controls Output Width (Spaces/Line) OH Controls Output Height (Lines/Scrolling) NOTE Write messages to the session window RETRIEVE opens a saved worksheet BRIEF Controls Output from Commands Could be used to execute any predefined set of commands. Local Minitab Macros
Objectives Define the structure of a Minitab local macro. Illustrate the use of a Minitab local macro with an example. Introduce some of the more advance programming capabilities of the local macro. The Structure of a Local Macro MACRO template declaration statements body of the macro ENDMACRO MACRO marks the beginning of the macro. ENDMACRO marks the end of the macro. template the name of the macro. declaration statements defines all variables to be used. body of the macro Minitab commands, macro statements, and calls to other macros. Example of a Local Macro As stored in the file lmacroex1.mac: MACRO beginning of macro lmacroex1 ssize xbar sd template name of macro, arguments, and Mconstant ssize xbar sd subcommands declarations constants and columns Mcolumn sample Random ssize sample; Normal xbar sd. body of macro Minitab commands Histogram sample. ENDMACRO end of macro Note 1: This macro requires that three arguments are supplied when the local macro is called; otherwise it will produce an error. Note 2: This macro does not affect any data in the worksheet, but otherwise it produces the same type of plot as the global macro. Creating, Storing, and Invoking Global Macros The same rules apply to local macros: Created with a simple editor such as Notepad. Same conventions with regard to naming, and storing the macros. Macros are invoked, or called, the same way as local macros except that local macros can have arguments, and subcommands. Example: %'C:\My Documents\cqas742\Class 10\lmacroex1.mac 10 500 100 Arguments ssize xbar sd Variable Declarations All variables (constants, columns, or matrices) used in the local macro must be declared: MCONSTANT defines all the constants MCOLUMN defines all the columns Naming Variables A variable name Can be a maximum of eight characters. May include letters, numbers, and an underscore, but must begin with a letter. Can be in capitals, lower case, or mixed. Cannot be the same name as a subcommand. MMATRIX defines all the matrices The names of these variables do not have to follow the usual Minitab conventions (e.g. K for constants, C for columns, and M for matrices).
Adding Subcommands In order to add subcommands to a local macro, you must: Add a Subcommand Change the Template Template with a subcommand: Invoked by: 1) Add the subcommand to the template 2) Declare any additional variables needed lmacroex2 ssize xbar sd; Normaltest. %lmacroex2 10 500 100; Normaltest. 3) Add statements that conditionally execute dependent on the value of the subcommands. We ve added a subcommand named Normaltest, that requires no arguments Note: It is often easier to write the Minitab statements that make use of the subcommand, then write the declaration statements required Add a Subcommand Write the Statements MACRO beginning of macro lmacroex2 ssize xbar sd; Normaltest. Mconstant ssize xbar sd template name of macro, arguments, and subcommands declarations constants and columns Mcolumn sample Random ssize sample; Normal xbar sd. Histogram sample. body of macro Minitab commands If Normaltest ~= 0 %NormPlot sample Endif ENDMACRO end of macro Tests to see whether the subcommand was used Note: In this case no other variables were required, or declared Invoking a Local Macro with a Subcommand To execute the macro, type: % fullmacroname arguments; subcommands arguments. Example: %'C:\My Documents\cqas742\Class 10\lmacroex2.mac 10 500 100; Normaltest. Advanced Minitab Macro Programming Objectives Illustrate advanced macro programming by developing a local macro that takes samples from a column of data, calculates means, and outputs the results to a column of the same worksheet. Show how the macro is developed from the session commands.
Session Commands: Create the Data and Other Would-Be Arguments to the Macro # suppose c1 contains the data Random 100 c1; Normal 500 100. # k1 is the sample size let k1 = 25 # k2 is the number of samples let k2 = 5 Session Commands: Taking Random Samples with Replacement and Storing # take a random sample with replacement # store in columns c11-c15 (five samples) Sample K1 C1 c11; Sample K1 C1 c12; Sample K1 C1 c13; Sample K1 C1 c14; Sample K1 C1 c15; Session Commands: Calculating Sample Means and Storing Them # calculate the sample means and store in k11-k15 Mean C11 k11. Mean C12 k12. Mean C13 k13. Mean C14 k14. Mean C15 k15. # copy constants into a column c2 copy k11-k15 c2 Strategy Developed from the Session Commands The raw data column, the sample size, and the number of samples can be considered inputs to the macro. The output data column of sample means is the only required output from the macro. The samples can be temporary columns, and their means can be temporary constants (before they are copied to the output column). The number of temporary columns and constants is a function of the number of samples, and is not know until the macro is invoked, making it difficult to know how many variables to set aside in the declaration. The columns and constants would be more easily addressed if they behaved like SAS Arrays and sequenced variables (i.e. Sample1-Sample5) Developing the Macro - Template MACRO # template lmacroex3 ssize nsamples indata outmeans Developing the Macro Declarations # declarations Mconstant ssize nsamples Mcolumn indata outmeans # this macro does 5 samples, regardless # of the value of nsamples Mcolumn col11 col12 col13 col14 col15 Mconstant kon11 kon12 kon13 kon14 kon15
Developing the Macro - Sampling # take the random samples # would like to make this a loop Sample ssize indata col11; Sample ssize indata col12; Sample ssize indata col13; Sample ssize indata col14; Sample ssize indata col15; Developing the Macro - Calculations # calculate the sample means and store in kon11-kon15 # would like to make this a loop also Brief 0 Mean col11 kon11. Mean col12 kon12. Mean col13 kon13. Mean col14 kon14. Mean col15 kon15. Brief 3 Developing the Macro Return and End # copy constants into a column outmeans # variable number of constants... Copy kon11-kon15 outmeans ENDMACRO Invoking the Macro To execute the macro, type: %'C:\My Documents\cqas742\Class 10\lmacroex3.mac 10 5 C1 C2 Sample Size 10 Number of Samples 5 Input Data C1 Output Means C2 Note: This version of the macro produces five samples regardless of the value of the second argument. Improvements Still Needed We have to get the macro to execute the number of samples supplied in the call to the macro. In order to do this we must be able to: Declare a variable number of columns and constants depending on the value of nsamples. Loop through the samples as many times as specified by the value of nsamples. Reference a specific column or constant in a list as we go through the loop. Suffixed Variables Suffixed Variables (similar to SAS arrays), may be referred as: Suffixed Variable Variable Name Period Suffix sample.1 sample. 1 ` sample.index sample. index where index is a constant.
Do Loops DO Index =start : stop Minitab Statements ENDDO Refining the Macro - Template MACRO # template lmacroex4 ssize nsamples indata outmeans Do Loops with Suffixed Variables DO Index =start : stop Minitab Statements assignment = sample.index ENDDO Note: Only the macro name changed. Refining the Macro Declarations # declarations Mconstant ssize nsamples Mcolumn indata outmeans Mcolumn col.1-col.nsamples Mconstant kon.1-kon.nsamples index Refining the Macro - Sampling # take the random samples Do index = 1:nsamples Sample ssize indata col.index; Enddo Note 1: Now the number of columns and constants declared depends on the value of nsamples. Note: Several statements replaced with a loop Note 2: The variable index is added to the list of constants. Refining the Macro - Calculations # calculate the sample means and store in kon.index Brief 0 Do index = 1:nsamples Mean col.index kon.index Enddo Brief 3 Note: Several statements replaced with a loop Refining the Macro Return and End # copy constants into a column outmeans # variable number of constants... Copy kon.1-kon.nsamples outmeans ENDMACRO Note: Takes into account the variable number of samples
Objectives Introduce some Minitab commands that are useful in writing and debugging Minitab macros. Hints on Debugging Macros Interpreting Error Messages Errors are generally labeled with one of two designations: ** ERROR ** (two asterisks) means an error was found by the macro processor * ERROR * (one asterisk) means an error was found by regular Minitab Minitab Response to Macro Errors Minitab can stop when a macro error is found, or it can continue on through the program depending on the value of: PLUG NOPLUG - Minitab continues as best it can - Minitab stops when it encounters a macro error The default setting is NOPLUG, but the setting can be changed in the session window, or anywhere in the macro. Getting INFO During Macro Execution Syntax: INFO shows information about the local worksheet, including columns, constants and matrices. INFO can be typed in the session window to get information about the current active worksheet, or in the body of the macro to get information about the Local macro worksheet and it s temporary variables. Debugging Tools Minitab can stop when a macro error is found, or it can continue on through the program depending on the value of: ECHO NOECHO - Minitab commands are printed in the session log as executed by the macro processor - Minitab displays only the errors associated with the commands executed The default setting is NOECHO, but the setting can be changed in the session window, or anywhere in the macro.
Debugging Tools Minitab can printed additional diagnostic information during the macro processing depending on the value of: DEBUG - extra information is displayed Debugging Tools Minitab can pause execution of a macro at any point: PAUSE - execution is paused, and control is returned to the session window NODEBUG - no extra information is displayed RESUME - returns control to the macro The default setting is NODEBUG, but the setting can be changed in the session window, or anywhere in the macro. PAUSE can be place anywhere in the macro. When control is passed back, the user can type any command such as the PRINT command, or the INFO command. Note: The commands act only on the local macro worksheet. SUMMARY Minitab Global Macros can be defined to help automate repetitive tasks that act directly on the active worksheets. Minitab Local Macros can execute without modifying the active worksheet unless specified to do so. Minitab Local Macros can be written to accept arguments, and subcommands, making them much more flexible than Minitab Global Macros. Much of the capability of SAS Macros and SAS Arrays can be implemented within the context of Minitab Local Macros.