Epi Info™ User Guide

Command Reference


Analysis Commands: IF THEN ELSE

Description
This command defines conditions and one or more consequences which occur if the conditions are met. An alternative consequence can be given after the ELSE statement. The ELSE will be executed if the first set of conditions is not true. If the condition is true, the first statement is executed. If the statement is false, the statement is bypassed. If an ELSE statement exists, it is executed instead. THEN is part of the If Condition statement. It starts the section of code executed when the If condition is true.

Syntax
IF <expression> THEN
[command(s)]
END
IF <expression> THEN
[command(s)]
ELSE
[command(s)]
END

  • The <expression> represents a condition that determines whether or not subsequent commands will be run. If the condition evaluates to true, the commands inside of the IF block will run. If the condition evaluates to false, the commands inside of the ELSE block will run instead. If no ELSE exists and the condition is false, then no commands inside of the IF block are run.
  • The [command(s)] represents at least one valid command.
  • The ELSE statement is optional and will run any code contained inside of it when the <expression> evaluates to false.

Comments
An IF statement is executed immediately if it does not refer to a database variable, if characteristic or attribute that can be measured, or if any defined variables have been assigned literal values. If the statement, YEAR = 97 has already occurred, then an IF statement dependent on it, such as IF YEAR = 97 then …., is executed immediately.
IF Age > 15 THEN
ASSIGN Group = “ADULT”
ELSE
ASSIGN Group = “CHILD”
END

It is important to cover all the conditions in IF statements to avoid gaps in the logic and results. Sometimes it is important to have an ELSE condition that covers conditions not included in other IF clauses. This effect is best achieved by setting the variable initially to something other than missing.
DEFINE ILL YN
ASSIGN ILL = (-)

IF Vomiting = (+) THEN
ASSIGN ILL = (+)
END

IF Diarrhea = (+) THEN
ASSIGN ILL = (+)
END

IF Fever = (+) THEN
ASSIGN ILL = (+)
END
Set Ill = (+) only if one or more symptoms are present.

The same result could be achieved with this code:
IF (Diarrhea = (+)) OR (Vomiting = (+)) OR (Fever = (+)) THEN
ASSIGN ILL = (+)
ELSE
ASSIGN ILL = (-)
END

Examples
Example 1: The Group variable for all records in the data set is assigned the value of “Young Adult” if the Age variable has a value between (but not including) 17 and 30. If the value in Age falls outside of this range, no value is assigned to Group.

READ {.\Projects\Sample\Sample.prj}:Oswego 
DEFINE Group TEXTINPUT 
IF Age < 30 AND Age > 17 THEN
    ASSIGN Group = "Young Adult" 
END 
LIST Group

Example 2: Several different values are assigned to the Group variable depending on the value of the Age variable.

READ {.\Projects\Sample\Sample.prj}:Oswego 
DEFINE Group TEXTINPUT 
IF Age < 30 AND Age > 17 THEN 
   ASSIGN Group = "Young Adult" 
END
IF Age <= 17 THEN
   ASSIGN Group = "Minor" 
END
IF Age >= 30 THEN 
  ASSIGN Group = "Adult" 
END
LIST Group

Example 3: If the patient ate chocolate or vanilla ice cream, the variable IceCream is assigned a value of true. Otherwise, it is assigned a value of false.

READ {.\Projects\Sample\Sample.prj}:Oswego 
DEFINE IceCream YN  
IF Vanilla = (+) OR Chocolate = (+) THEN 
 ASSIGN IceCream = (+) 
ELSE 
 ASSIGN IceCream = (-) 
END 
LIST Vanilla Chocolate IceCream