• No results found

Introduction to Minitab Macros. Types of Minitab Macros. Objectives. Local Macros. Global Macros

N/A
N/A
Protected

Academic year: 2021

Share "Introduction to Minitab Macros. Types of Minitab Macros. Objectives. Local Macros. Global Macros"

Copied!
10
0
0

Loading.... (view fulltext now)

Full text

(1)

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.

(2)

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.

„As stored in the file gmacroex1.mac: GMACRO gmacroex1 Random k1 c1; Normal 500 100. Histogram c1. ENDMACRO

Example of a Global Macro

beginning of macro

end of macro template – name of macro

body of macro – Minitab commands…

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.

(3)

Invoking a Global Macro

To execute the macro, type: %’fullmacroname’

wherefullmacronameis 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 gmacroex2

# 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

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 ENDMACRO beginning of macro end of macro template body

As stored in the file: 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.

(4)

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.

„As stored in the file lmacroex1.mac:

MACRO

lmacroex1 ssize xbar sd Mconstant ssize xbar sd Mcolumn sample Random ssize sample;

Normal xbar sd. Histogram sample. ENDMACRO

Example of a Local Macro

beginning of macro

end of macro

template – name of macro, arguments, and subcommands

body of macro – Minitab commands…

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.

declarations – constants and columns

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

ssize xbar sd Arguments

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 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).

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.

(5)

Adding Subcommands

In order to add subcommands to a local macro, you must: 1) Add the subcommand to the template

2) Declare any additional variables needed

3) Add statements that conditionally execute dependent on the value of the subcommands.

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 – Change the Template

Template with a subcommand:

lmacroex2 ssize xbar sd; Normaltest.

Invoked by:

%lmacroex2 10 500 100; Normaltest.

We’ve added a subcommand named Normaltest, that requires no arguments…

Add a Subcommand – Write the Statements

MACRO

lmacroex2 ssize xbar sd; Normaltest.

Mconstant ssize xbar sd Mcolumn sample Random ssize sample;

Normal xbar sd. Histogram sample. If Normaltest ~= 0 %NormPlot sample Endif ENDMACRO beginning of macro end of macro

template – name of macro, arguments, and subcommands

body of macro – Minitab commands… declarations – constants and columns

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.

(6)

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; Replace. Sample K1 C1 c12; Replace. Sample K1 C1 c13; Replace. Sample K1 C1 c14; Replace. Sample K1 C1 c15; Replace.

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

(7)

Developing the Macro - Sampling

# take the random samples

# would like to make this a loop Sample ssize indata col11;

Replace.

Sample ssize indata col12; Replace.

Sample ssize indata col13; Replace.

Sample ssize indata col14; Replace.

Sample ssize indata col15; Replace.

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.

(8)

Do Loops

DO Index =start : stop

Minitab Statements ENDDO

Do Loops with Suffixed Variables

DO Index =start : stop

Minitab Statements assignment = sample.index ENDDO

Refining the Macro - Template

MACRO

# template

lmacroex4 ssize nsamples indata outmeans

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

Note 1: Now the number of columns and constants declared depends on the value of nsamples.

Note 2: The variable index is added to the list of constants.

Refining the Macro - Sampling

# take the random samples

Do index = 1:nsamples Sample ssize indata col.index;

Replace. Enddo

Note: Several statements replaced with a loop

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
(9)

Hints on Debugging Macros

Objectives

„Introduce some Minitab commands that are useful in writing and debugging Minitab 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 - Minitab continues as best it can… NOPLUG - 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 - Minitab commands are printed in the

session log as executed by the macro processor…

NOECHO - 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.

(10)

Debugging Tools

Minitab can printed additional diagnostic information during the macro processing depending on the value of:

DEBUG - extra information is displayed… NODEBUG - no extra information is displayed… The default setting is NODEBUG, but the setting can be changed in the session window, or anywhere in the macro.

Debugging Tools

Minitab can pause execution of a macro at any point: PAUSE - execution is paused, and control is

returned to the session window… RESUME - returns control to 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.

References

Related documents

In most of the European countries decentralised hospital incinerators have been, because of high operation costs and pollution problems, widely banned and replaced by

© 2011 KPMG Intern ational Cooperative (“KPMG International”), a Swiss entity. Member firms of the KPMG network of independent firms are affiliated with KPMG International.

If the customer has an account with SECURITRON or a valid credit card, the customer may order an advance replacement product, whereby SECURITRON will charge the customer’s account

Using ion’s platform integrated with call tracking, the Paychex marketing team is able to test and optimize over 250 paid search landing pages based on accurate and

While some of the station located out of urban and located at the palm oil farming area also give some high value in water quality parameters such as NH3N at the station T5..

[r]

Highly motivated to the zumba fitness instructor resume sample sign in working towards the physical needs in fitness instructors are applying for motivating participants at

information retrieval that does not require user hearing shall be provided, or support for Assistive Technology used by people who are deaf or hard of hearing shall be