Controlling Macro Flow

Similar documents
Minitab 18. Minitab Macros Help

Introducing Simple Macros

Essentials for Scientific Computing: Bash Shell Scripting Day 3

A Tutorial for ECE 175

Control Structures. CIS 118 Intro to LINUX

Shell programming. Introduction to Operating Systems

A control expression must evaluate to a value that can be interpreted as true or false.

DATABASE AUTOMATION USING VBA (ADVANCED MICROSOFT ACCESS, X405.6)

Programming Fundamentals

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029

(Refer Slide Time: 01:12)

Attia, John Okyere. Control Statements. Electronics and Circuit Analysis using MATLAB. Ed. John Okyere Attia Boca Raton: CRC Press LLC, 1999

IDL Primer - Week 1 John Rausch

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Macro Programming Reference Guide. Copyright 2005 Scott Martinez

Macro Programming Information

5. Selection: If and Switch Controls

Control Flow. September 2, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 September 2, / 21

ECE 202 LAB 3 ADVANCED MATLAB

COMP 4/6262: Programming UNIX

Indian Institute of Technology Kharagpur. PERL Part II. Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. I.I.T.

GNU ccscript Scripting Guide IV

Individual Plot Overlay

Cisco IOS Shell. Finding Feature Information. Prerequisites for Cisco IOS.sh. Last Updated: December 14, 2012

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Conditional Control Structures. Dr.T.Logeswari

Fortran 90 Two Commonly Used Statements

bash Execution Control COMP2101 Winter 2019

example: name1=jan name2=mike export name1 In this example, name1 is an environmental variable while name2 is a local variable.

The SAS interface is shown in the following screen shot:

CSCI 1061U Programming Workshop 2. C++ Basics

Voice Foundation Classes

Flow Control: Branches and loops

Objectives Reading SAS Data Sets and Creating Variables Reading a SAS Data Set Reading a SAS Data Set onboard ia.dfwlax FirstClass Economy

Java Methods. Lecture 8 COP 3252 Summer May 23, 2017

A Big Step. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

MIXREG for Windows. Overview

A shell can be used in one of two ways:

GETTING STARTED WITH MINITAB INTRODUCTION TO MINITAB STATISTICAL SOFTWARE

SOURCE LANGUAGE DESCRIPTION

Introduction. C provides two styles of flow control:

Conditionals and Recursion. Python Part 4

Linux Shell Scripting. Linux System Administration COMP2018 Summer 2017

Scripting. Shell Scripts, I/O Redirection, Ownership and Permission Concepts, and Binary Numbers

ECE15: Introduction to Computer Programming Using the C Language. Lecture Unit 4: Flow of Control

Module 25 Control Flow statements and Boolean Expressions

Chapter 4 The If Then Statement

An interesting related problem is Buffon s Needle which was first proposed in the mid-1700 s.

Functions. Lecture 6 COP 3014 Spring February 11, 2018

Note on homework for SAS date formats

Unix Scripts and Job Scheduling. Overview. Running a Shell Script

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

Running Minitab for the first time on your PC

UNIX shell scripting

Report Writer Reference Manual

UNIT-IV: MACRO PROCESSOR

VBA Handout. References, tutorials, books. Code basics. Conditional statements. Dim myvar As <Type >

SECTION 2: PROGRAMMING WITH MATLAB. MAE 4020/5020 Numerical Methods with MATLAB

Using nu/tpu on UNIX Last revised: 1/28/00

PRELIMINARY APPLE BASIC USERS MANUAL OCTOBER Apple Computer Company. 770 Welch Rd., Palo Alto, CA (415)

Repetition Through Recursion

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

PROBLEM SOLVING WITH FORTRAN 90

Chapter 4 Introduction to Control Statements

Compiling Techniques

Microsoft Office Excel Use Excel s functions. Tutorial 2 Working With Formulas and Functions

ISETL Graphics Guide. Using the Graphics Facility

Computer Systems and Architecture

Statements and Operators

Erlangen API Documentation

Building Powerful Workflow Automation with Cherwell and PowerShell

Bourne Shell Programming Topics Covered

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Computer Programming I - Unit 5 Lecture page 1 of 14

Language Fundamentals

CS251 Programming Languages Handout # 29 Prof. Lyn Turbak March 7, 2007 Wellesley College

EL2310 Scientific Programming

TABLE OF CONTENTS 2 CHAPTER 1 3 CHAPTER 2 4 CHAPTER 3 5 CHAPTER 4. Algorithm Design & Problem Solving. Data Representation.

Mathews Malnar and Bailey, Inc.

CERTIFICATE IN WEB PROGRAMMING

Chapter 4 - Notes Control Structures I (Selection)

STUDENT LESSON A12 Iterations

Definition: A data structure is a way of organizing data in a computer so that it can be used efficiently.

IDL 5.3. Things you need to know. On Windows, select Start > Programs -> Research Systems IDL 5.3 -> IDL Online Manuals

Matlab Primer. Lecture 02a Optical Sciences 330 Physical Optics II William J. Dallas January 12, 2005

Lecture 1: Hello, MATLAB!

M1-R4: Programing and Problem Solving using C (JAN 2019)

Assembler Language Macro "Boot Camp" Part 1

Principles of Programming Languages

Excel Programming with VBA (Macro Programming) 24 hours Getting Started

Bourne Shell Reference

Real SQL Programming Persistent Stored Modules (PSM)

COMP 2718: Shell Scripts: Part 2. By: Dr. Andrew Vardy

Lab copy. Do not remove! Mathematics 152 Spring 1999 Notes on the course calculator. 1. The calculator VC. The web page

Control Statements. Objectives. ELEC 206 Prof. Siripong Potisuk

MP 3 A Lexer for MiniJava

Chapter 3: Programming with MATLAB

Fundamentals of Computer Programming Using C

Transcription:

30 Controlling Macro Flow Control Statement Overview, 30-2 IF, ELSEIF, ELSE,, 30-2 DO,, 30-3 WHILE, ENDWHILE, 30-4 NEXT, 30-5 BREAK, 30-5 GOTO, MLABEL, 30-6 Invoking Macros from Within Macros, 30-7 CALL, RETURN, 30-8 EXIT, 30-10 PAUSE, RESUME, 30-10 Using DOS Commands, 30-11 MINITAB User s Guide 1 30-1

Chapter 30 Control Statement Overview Control Statement Overview Control statements can make your macro more flexible and powerful because they allow you to control the sequence in which commands in the macro are executed. They can perform some action given a condition using an IF statement. They can perform some action repeatedly using a DO- LOOP statement. They can start other macros from within a macro using a CALL and RETURN statement. The following pages document these control statements, and more. You can also nest control statements. For example, one control statement, such as an IF statement, can contain several other control statements, such as additional IF statements or a DO statement. IF, ELSEIF, ELSE, IF logical expression (a block of MINITAB and macro commands) ELSEIF logical expression (a block of MINITAB commands and macro statements) ELSE (a block of MINITAB commands and macro statements) You can make your macro conditionally execute commands with the IF control statements IF, ELSEIF, ELSE, and. The IF control statements can evaluate a logical expression using comparison operators (such as > for greater than and = for equals ) and Boolean operators (such as AND, OR, and NOT). A logical expression is any expression from the LET command. The comparison and Boolean operators listed below are the features of LET that are most often used in IF statement. = or EQ equal to & or AND ~= or NE not equal to or OR < or LT less than ~ or NOT > or GT greater than <= or LE less than or equal to >= or GE greater than or equal to In most cases the logical expression evaluates to a single number. If the number is 0 (false), the block of statements is skipped; if it is not 0 (true), the block is executed. If 30-2 MINITAB User s Guide 1

DO, Controlling Macro Flow the logical expression evaluates to a column, then if all entries in the column are 0, the expression is considered false, otherwise it is considered true. You can use up to 50 ELSEIF statements within the IF- block. e Example of using IF in a macro In the example below, the macro only executes the REGRESS command if there are more than three observations in the column Yield. If there are less than three or exactly three observations, the macro prints a message in the Session window using the NOTE command (described on page 28-8). LET K1 = COUNT( Yield ) IF K1 < 3 NOTE Not enough observations ELSEIF K1 = 3 NOTE Add one observation ELSE REGRESS C5 2 C1 C2 DO, DO K = list of numbers (a block of MINITAB commands and macro statements) Allows you to loop through a block of commands. K is set equal to the first number in the list, then the block of commands is executed. When MINITAB reaches the, K is set equal to the next number in the list and the block is executed again. This continues until all numbers in the list are used, or until you branch out of the DO-loop with a BREAK, GOTO, RETURN, or EXIT command. The list of numbers can be an explicit list of any numbers or stored constants. A patterned list can be abbreviated using a colon and slash as in SET. For example, 1:10 is the list 1, 2, 3,, 10, and 1:1.8 /.2 is the list 1, 1.2, 1.4, 1.6, 1.8. Numbers can be increasing or decreasing order. The following DO-loop changes the values in rows 1 through 10 and row 50 of columns C1 and C2 to the missing value code: DO K1 = 1:10 50 LET C1(K1) = * LET C2(K1) = * Here is a local macro that calculates a moving average of length three. It shows how to loop through the values in a column. MACRO MOVAVE X Y MINITAB User s Guide 1 30-3

Chapter 30 WHILE, ENDWHILE Calculates the simple moving average of the data in X and stores the answer in Y. MCONSTANT N I MCOLUMN X Y LET N = COUNT(X) LET Y(1) = * LET Y(2) = * DO I = 3 : N LET Y(I) = (X(I) + X(I-1) + X(I-2))/3 WHILE, ENDWHILE WHILE logical expression (a block of MINITAB commands and macro statements) ENDWHILE Repeats a block of commands as long as the logical expression is true. The logical expression follows the same rules as in the IF statement. Suppose we want to find the root of the equation, y = 1 + x + x 3. We know this equation has just one real root and that it is between 0 and 1. The following global macro allows us to find, approximately, what the root is. ROOT Finds the root of a specific polynomial. The result is within.01 of the exact answer. K90-K93 are used for scratch work NAME K90 = X K91 = Y K92 = Xlow K93 = Ylow LET X = 0 LET Y = -1 WHILE Y < 0 LET X = X +.01 LET Y = -1 + X + X **3 ENDWHILE LET Xlow = X -.01 LET Ylow = -1 + Xlow + Xlow **3 PRINT Xlow Ylow X Y We first initialized the two variables, X and Y, to 0 and 1. Each time through the WHILE-loop, MINITAB first checks to see that Y is still less than zero. If it is, we increase X by.01 and calculate Y at this new value. Once the condition fails, that is, once Y is no 30-4 MINITAB User s Guide 1

NEXT Controlling Macro Flow longer less than zero, we exit the loop and go to the first statement after ENDWHILE. Then we print out the answer. NEXT NEXT Transfers control from within a DO- or WHILE-loop back to the beginning of the block. For DO, the loop variable is then set to the next value in the list and the loop is executed again. Here is a simple example, using a global macro. FIVES Takes the column named X and changes all entries that are greater than 5 to 5. Constants K90 and K91 are used for scratch work. NAME K90 = N K91 = I LET N = COUNT( X ) DO I = 1 : N IF X ( I ) <= 5 NEXT ELSE LET X ( I ) = 5 The DO-loop goes through all the values in X. If a value is less than or equal to 5, NEXT passes control to the top of the DO-loop and the value is left unchanged. If a value is greater than 5, the ELSEIF block is executed and that value is set to 5. BREAK BREAK Transfers control from within a DO- or WHILE-loop to the command immediately following the end of the loop. Thus BREAK breaks out of the loop. Here is a simple example using a global macro. MINITAB User s Guide 1 30-5

Chapter 30 GOTO, MLABEL NOMISS Takes data from the column named X. Finds the first missing observation. Then deletes all observations starting with the first missing to the end of the column. Constants K90 and K91 are used for scratch work LET K90 = COUNT( X ) DO K91 = 1:K90 IF X (K91) = * BREAK DELETE K91:K90 X The program goes through the values of X until it finds a missing value. It then leaves the loop and goes to the statement following in this example, DELETE. Note This program does not handle the case when X has no missing values correctly. We will fix this when we discuss the command EXIT. GOTO, MLABEL GOTO number (other MINITAB commands and macro statements) MLABEL number Allows you to branch to any line in your macro. There can be several GOTO s in one program. A GOTO is matched to the MLABEL that has the same number. The number can be any integer from 1 to 8 digits long. It cannot be a variable. 30-6 MINITAB User s Guide 1

Invoking Macros from Within Macros Controlling Macro Flow Here is the program we used to illustrate BREAK above, but now coded with a GOTO. LET K90 = COUNT( X ) DO K91 = 1 : K90 IF X (K91) = * GOTO 5 MLABEL 5 DELETE K91:K90 X Invoking Macros from Within Macros You may have two or more macros in one file. Each macro in the file follows the usual structure (beginning with or MACRO, ending with, etc.), and each must have a unique template name. When you invoke a macro, MINITAB executes the first macro in the file. Subsequent macros in the file are subroutines that you can invoke using a CALL statement (see CALL, RETURN on page 30-8). There are some restrictions on which type of macro another macro can call: From within this type of macro You can invoke Global Global Exec Local Local Exec Global Local Exec You invoke a macro from within a macro in the same way you invoke a macro from the MINITAB prompt. On a line, put the symbol % followed by the name of the macro file, as in %TRIM. You can also include a path statement, as in %C:\MYWORK\TRIM. If it is a local macro, include all appropriate arguments and subcommands. Because the macros you execute are stored in your worksheet area, the only limitation to the number of macros you can nest is the amount of space available in your worksheet. If you run out of room, see the macro statement SWAP (page 32-6) for a way to work around that problem. The following example improves the global macro ANALYZE, described in Chapter 28, to handle the case when a data set is too small to analyze. The main file, stored as ANALYZE2.MAC, determines how many observations are in the data set. If there are fewer than 5, it invokes the macro file TOOSMALL.MAC. TOOSMALL prints out a message then prints the data set. If the data set has at least 5 observations, ANALYZE2 invokes the macro file OK.MAC. OK is the same as the original version, ANALYZE. MINITAB User s Guide 1 30-7

Chapter 30 CALL, RETURN ANALYZE2.MAC ANALYZE2 LET K90 = COUNT(C1) IF K90 < 5 %TOOSMALL ELSE %OK TOOSMALL.MAC TOOSMALL NOTE Data set has fewer than 5 observations. NOTE No analysis will be done. Here are the data. PRINT C1 - C3 OK.MAC OK NAME C1 = Yield C2 = Chem1 C3 = Chem2 C5 = Ln.Yield PRINT C1-C3 DESCRIBE C1-C3 LET C5 = LOGE( Yield ) REGRESS C5 2 C2 C3 CALL, RETURN CALL template RETURN You can include several macros in one file, just as a program often includes several subroutines. CALL and RETURN let you specify when to pass control to another macro and when to return to the main macro. You can include several global macros in one file, or several local macros in one file, but you cannot mix global and local macros together in one file. When you invoke a macro, from interactive MINITAB or from another macro, the first macro in the file is executed first. Use the macro statements CALL and RETURN to invoke a different macro within the macro file. 30-8 MINITAB User s Guide 1

CALL, RETURN Controlling Macro Flow Recall that the second line of a macro is the template, or the macro name. When one macro in a macro file calls another macro in that file, use the command CALL, followed by the name on that macro s template. If it is a local macro, include appropriate arguments and subcommands. Any macro in a macro file can CALL any other macro in the file, any number of times. RETURN says to leave the current macro and go back to the calling macro, to the statement just after the CALL. RETURN is optional. If RETURN is not present in the macro that was called (the subroutine), then after it has executed, control is transferred back to the calling macro. The following example is a variation on ANALYZE2.MAC (page 30-7) named ANALYZE3.MAC. This global macro file contains three macros: ANALYZE3 NOTE Would you like all data printed? YESNO K80 If user types yes K80 = 1, if no K80 = 0 LET K90 = COUNT(C1) IF K90 < 5 CALL TOOSMALL ELSE CALL OK IF K80 = 1 NOTE Here are the data. PRINT C1-C3 TOOSMALL NOTE Data set has fewer than 5 observations. NOTE No analysis will be done. OK NAME C1 = Yield C2 = 'Chem1' C3 = 'Chem2 C5 = 'Ln.Yield' DESCRIBE C1-C3 LET C5 = LOGE('Yield') REGRESS C5 2 C2 C3 IF K80 = 1 RETURN NOTE Analysis done, but no data printed by request MINITAB User s Guide 1 30-9

Chapter 30 EXIT In this example, ANALYZE3, we use the YESNO command (see page 31-5) to see if the user wants to print all the data. If the response is Yes, YESNO sets K80 to 1; if the answer is No, K80 is set to 0. The OK subroutine checks the value of K80 with an IF statement. If K80 equals 1, the RETURN statement sends control back to the main macro. If K80 is anything else, the macro prints one more note. When the statement is encountered in either the TOOSMALL or OK subroutine, control is transferred back to the calling macro. EXIT EXIT Stops the macro and transfers control back to interactive MINITAB. Here is a modification of the macro NOMISS, that correctly handles the case when X contains no missing values. LET K90 = COUNT( X ) DO K91 = 1:K90 IF X (K91) = * BREAK IF K91 = K90 NOTE Note: There are no missing observations in X. EXIT DELETE K91:K90 X PAUSE, RESUME PAUSE RESUME When MINITAB encounters a PAUSE in a macro, control is shifted from the macro to the keyboard. You can then type any MINITAB command. When you want to return control to the macro, type RESUME (or just R). PAUSE can help you debug a macro you are developing. It can also allow you to get input from the macro user. 30-10 MINITAB User s Guide 1

Using DOS Commands Controlling Macro Flow If you are in PAUSE mode from within a local macro, you have access to the local worksheet and only the local worksheet. You can also declare new local variables and use them. They will be stored at the end of the local worksheet. When you are in PAUSE mode, you can type any MINITAB command. You cannot CALL other macros in the same file, invoke a macro from another macro file, or use control statements. Using DOS Commands Change and show directories CD [path] DIR [path] CD without a path displays your current directory. CD with a path changes from your current directory to the specified directory. DIR lists the names of all the files in your current or the specified directory. Here are some examples: CD CD \SUE\SALES91 DIR displays your current directory changes to the \SUE\SALES91 directory lists the names of files in your current directory Show a text file TYPE [path]filename.ext Lists the specified text (ASCII) file in the Session window. The file must be a standard text file in order for this to work. Include the full file name and file extension within single quotation marks. If the file is not in your default directory, include the path within single quotation marks as well. For example, to list the contents of the macro file SALES.MAC on your screen, enter: TYPE SALES.MAC MINITAB User s Guide 1 30-11