Local macros with subcommands, submacros, and control statements

This recipe will create a complicated macro using control statements and subcommands on the main macro command.

The following macro will be able to accept any number of columns on the main command line and generate a layout for each column. Specifications can be optional; they can be entered as a column or a single value. We will use a column for the specifications to allow different specifications for each column of data, or a constant to use the same specification for each column. The macro will be able to choose between the layout with or without specifications and perform an error check to see if the right number of specifications have been entered. This will cover several nested IF statements and DO loops.

Finally, we will copy the Llayout macro from the previous recipe into Notepad twice and edit this macro to create two submacros. One will be used to generate a layout for specifications and we will edit the other to create a layout when no specifications are used.

Getting ready

We will need the Llayout.mac file from the Creating a Local macro recipe or the catch-up file, Llayout.mac.

How to do it…

The following steps will convert the Llayout macro to use any number of columns as an input. We will also add a subcommand to use specifications as either a constant or a column:

  1. Open a new Notepad and save the file as LSub.mac in the macro folder.
  2. Enter the values shown in the following screenshot into Notepad for the macro name, subcommand, and variables to be defined.
    How to do it…
  3. Enter the first part of the IF statements as a way to check if the specification subcommand has been used and to identify if it is a constant.
    How to do it…
  4. Enter the next section for the specifications that have been used as a column and to check if the number of specifications and the number of columns are the same.
    How to do it…
  5. Finish the macro by entering the next section for no specifications used and ending the macro.
    How to do it…
  6. Copy the entire Llayout macro and paste this after ENDMACRO.
  7. Paste the Llayout macro a second time.
  8. For the second Llayout commands, change the macro name to NOSPEC.
  9. Delete all references to SPEC and GROUP from the NOSPEC macro.
  10. Change the header section and declarations to the commands shown in the following screenshot:
    How to do it…
  11. Change the TSPLOT commands by removing the GROUP and Reference subcommands. Ensure that the TSPLOT command appears as follows:
    How to do it…
  12. Save the macro.
  13. Return to Minitab and test the macro by first generating random data. Go to the Calc menu, then Random Data, and then select Normal….
  14. In Number of rows of data to generate:, enter 30.
  15. In the Store in column(s): section, enter c1-c5.
  16. For the Mean: section, enter 10.
  17. Click on OK.
  18. In the worksheet, create a new column called Specifications.
  19. Enter 11 11.5 * 12.5 13 in the Specifications column.
  20. Press Ctrl + L to open the command-line editor.
  21. Enter the %LSub c1-c5; command and press Enter to go to the next line.
  22. Enter Specs c6. and click on Submit Commands.

How it works…

Subcommands that are entered on the macro name line allow optional commands to be entered. When running the macro, we can decide whether we want to use specifications or not.

The use of the Mfree command here allows a single specification to be entered as a constant; if separate specifications are desired, we can enter a column from the worksheet because Mfree will set its variable type based on the variable that is being passed to the macro. The Mtype command is then used to ask what variable type we have in SPC.

The first IF statement checks if the SPECS subcommand was used. If it was, then we check if the specifications were entered as a constant or column. If they are entered as a column, then we check if the number of specifications match the number of columns with the count of SPC compared to N.

To avoid rewriting the graph code several times, we store this in two separate macros: Llayout and Nospecs. If we want to remove excess code, we could further tidy this by placing the Pplot and the Histogram command in a third macro. As these commands haven't used specifications, using a third submacro could reduce the number of lines used.

The Miss() function is used to identify the missing values in the worksheet. It has been used here to identify when missing values have been entered for specifications. This allows us to not include a specification for some columns.

There's more…

The Default command can be used to fix the values of constants in subcommands. This allows us to set a value for these constants if they have not been specified when calling the macro. The Default command, though, must come directly after the declaration statements.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.252.204