The if-then-else statement provides a secondary path of execution when an "if" clause evaluates to false. You could use an if-then-else statement in the applyBrakes method to take some action if the brakes are applied when the bicycle is not in motion. In this case, the action is to simply print an error message stating that the bicycle has already stopped.
Alternatively, you can check in the if part whether a capturing group has taken part in the match thus far. Place the number of the capturing group inside parentheses, and use that as the if part. Note that although the syntax for a conditional check on a backreference is the same as a number inside a capturing group, no capturing group is created. The number and the parentheses are part of the if-then-else syntax started with (?.
In the HOL object logic, the if-then-else, case, and let constructs have as low a precedence as quantifiers, which requires additional enclosing parentheses in the context of most other operations. Please have a look at , page 10.
There is a built-in predicate construction in Prolog which allows you to express exactly such conditions: the if-then-else construct. In Prolog, if A then B else C is written as ( A -> B ; C). To Prolog this means: try A. If you can prove it, go on to prove B and ignore C. If A fails, however, go on to prove C ignoring B. The max predicate using the if-then-else construct looks as follows:
Again, once you've read your data into a SAS data set, you probably want to do something with it. A common thing to do is to change the original data in some way in an attempt to answer a research question of interest to you. In the last lesson, we learned how to use assignment statements (and functions) to add some information to all of the observations in the data set. In this lesson, we will learn how to use if-then-else statements to add some information to some but not all of the observations in your data set.
As the output from Example 5.1 illustrates, there may be occasions when you want to use an if-then-else statement instead of just an if-then statement. In that example, we told SAS only what to do if the condition following the IF keyword was true. By including an else statement, we can tell SAS what to do if the condition following the IF keyword is false.
This if-then-else stuff seems easy enough! Let's try creating another status variable for our grades data set, but this time let's allow its value to depend on the value of the student's fourth exam (e4) rather than the value of the student's first exam (e1):
The following SAS program illustrates the use of several mutually exclusive conditions within an if-then-else statement. The program uses the AND operator to define the conditions. Again, when comparisons are connected by AND, all of the comparisons must be true in order for the condition to be true.
First, inspect the program to make sure you understand the code. Then, launch and run the SAS program. Review the output from the PRINT procedure to convince yourself that the letter grades have been assigned correctly. Also note how the program in general, and the if-then-else statement in particular, is formatted in order to make the program easy to read. The conditions and assignment statements are aligned nicely in columns and parentheses are used to help offset the conditions. Whenever possible ... okay, make that always ... format (and comment) your programs. After all, you may actually need to use them again in a few years. Trust me ... you'll appreciate it then!
Oh, one more point. You may have noticed, after the condition that takes care of missing values, that the conditions appear in order from A, B, ... down to F. Is the instructor treating the glass as being half-full as opposed to half-empty? Hmmm ... actually, the order has to do with the efficiency of the statements. When SAS encounters the condition that is true for a particular observation, it jumps out of the if-then-else statement to the next statement in the DATA step. SAS thereby avoids having to needlessly evaluate all of the remaining conditions. Hence, we have ourselves another good programming habit ... arrange the order of your conditions (roughly speaking, of course!) in an if-then-else statement so that the most common one appears first, the next most common one appears second, and so on. You'll also need to make sure that your condition concerning missing values appears first in the IF statement, otherwise SAS may bypass it.
All of the if-then-else statement examples we've encountered so far involved only numeric variables. Our comparisons could just as easily involve character variables. The key point to remember when comparing character values is that SAS distinguishes between uppercase and lowercase letters. That is, character values must be specified in the same case in which they appear in the data set. We say that SAS is "case-sensitive." Character values must also be enclosed in quotation marks.
Suppose our now infamous instructor wants to identify those students who either did not complete the course or failed. Because SAS is case-sensitive, any if-then-else statements written to identify the students have to check for those students whose status is 'failed' or 'Failed' or 'FAILED' or ... you get the idea. One rather tedious solution would be to check for all possible "typings" of the word "failed" and "incomp" (for incomplete). Alternatively, we could use the UPCASE function to first produce an uppercase value, and then make our comparisons only between uppercase values. The following SAS program takes such an approach:
Launch and run the SAS program. Review the output from the PRINT procedure to convince yourself that the if-then-else statement that involves the creation of the variable action is inadequate while the one that uses the UPCASE function to create the variable action2 works like a charm.
In this lesson, we learned how to write if-then-else statements in order to change the contents of our SAS data set. The homework for this lesson will give you more practice with this technique so that you become even more familiar with how it works and can use them in your own SAS programming.