Conditional Statement & Loops in SAS

A Conditional statements checks for a condition or a set of condition & decide further flow of a program.  SAS, like any other programming language has some predefined functions which helps a coder to put a conditional check on the variables & then execute the code written inside conditional parentheses accordingly.

SAS has following condtional statements, which we are going to explain in detail & then compare-contrast them.

IF-Else-Then Statement: An If-else statement checks for the one or more than one conditions & 'IF' conditions are true 'THEN' SAS executes all the statements written within conditional parentheses. If condition is false then SAS will not execute Then statements & go straight to 'ELSE' statement.
Syntax of If-else-then statement is 

IF CONDITION THEN <EXECUTE THIS>;
ELSE <EXECUTE THIS>;

Writing an 'ELSE' statement is not necessary, if we don't need to test any other conditions.

Sometimes, it is required to test more than 1 conditions in a single statement & we have to check them simultaneously. In such cases we can use operators like 'AND', 'OR', 'IN' etc. This is the right time to introduce all operators of SAS & their applications. 

Operators: These are used to check more than one conditions at a time. 

Operator  Application Conditional Statements
OR (Boolean) Returns a TRUE, When checking 2 or more conditions, & one or more can be true IF-ELSE
AND (Boolean) Returns a TRUE, When checking 2 or more conditions, & all of then must be true IF-ELSE
NOT (Boolean) Returns a TRUE, if condition mentioned after NOT is false. IF-ELSE
EQ, = To check arithmetic equality IF-ELSE
NE , != Arithmetic operator to check inequality IF-ELSE
LE, <= Less than & Equal to IF-ELSE
GE, >= Greater than & Equal to IF-ELSE
IN Multiple 'OR', To check more than one condition on same variable. IF-ELSE
IS MISSING To check for missing value. WHERE
IS NULL To check for null value. WHERE
BETWEEN …. AND  ….. To check between. WHERE
CONTAINS Checks for more than one strings in a variable. WHERE
LIKE "%...." To check for strings, alphabets & wildcards WHERE
 =* For phonetic mathes WHERE


Precedence order of Boolean operators is decided in SAS, This order is as follows

                                                                     NOT > AND > OR

Nested IF statement: A nested if statement is used to check for conditions within conditions. This makes program a bit  less complicated to understand, if written properly in alignment & indents. Below is a simple syntax for Nested IF statement.

IF <CONDITION1> AND <CONDITION2>  THEN OUTPUT;
     ELSE 
     IF <CONDITION3> OR <CONDITION4> THEN DELETE;
         ELSE
         IF <CONDITION5> THEN CONTINUE;

Here OUTPUT, DELETE, CONTINUE are options defined in SAS, to change program flow. 

Select-When Statement: This conditional statement is used to test conditions on a variable. This variable is declared in SELECT statement as an arguement. This statement is more efficient when conditions to be checked in are mutually exclusive. Syntax is as below.

SELECT ( VAR1);
WHEN (CONDITION1)  
              EXECUTE THIS;
WHEN (CONDITION2)  
              EXECUTE THIS;
.
.
.
.
OTHERWISE EXECUTE THIS
END;
RUN;


Where Statement: A Where statement is also used to test conditions & its used mostly in subsetting dataset. Syntax of a where statement subsetting data is given below.

DATA XYZ;
SET ABC;
WHERE CONDITION1;
RUN




DO Loops: Sometimes we need a certain part of code to be run iteratively. One way is to write these codes again & again or simply use a 'DO' loop statement. A Do loop can run a particlular lines of code for a defined no. of times.
This Do loop can perform a code after a condition is checked as mentioned below. Suppose we have if-conditional statements which can perform a single action after checking given conditions. This will increase length of code & hence in-turn will decrease program inefficiency.

IF <CONDITION1> THEN <ACTION1>;
IF <CONDITION1> THEN <ACTION2>;
IF <CONDITION1> THEN <ACTION3>;
IF <CONDITION1> THEN <ACTION4>;

DO Group statement:  Using a Do group, we can perform multiple actions in a single conditional statement. For above IF statements we can write,

IF <CONDITION1> THEN  DO;
<ACTION1>;
<ACTION2>;
<ACTION3>;
<ACTION4>;
END;


Iterative DO loops: When we have to run a group of statement multiple times, without checking any condition on variable. An index variable is used to program no. of iteration. This index variable is incremented every time by a pre-defined incremental value & following statements will be executed till end statement. This loop will keep on iterating till the index variable value is less than or equal to 'TO' value. Its syntax is defined as

DO  N=  <initial value>  TO <terminal value>  BY <incremental value>;
STATEMENT1;
STATEMENT2;
.
.
.
END;
RUN;


DO WHILE/ DO UNTIL Loops: These loops are used when in place of index variable we have to test a condition on variable present in data or calculated variable. Choosing between DO-WHILE or DO-UNTIL depends upon when conditions applied to the loop will be checked. In a DO-UNTIL loop, condition is checked at the bottom of the loop, everytime after loop is iterated. While, a DO-WHILE loop is used when condition needs to be checked at the beginning of loop. So here first condition is tested & if true then loop is run one time & then again condition is checked. Syntax of loops are

DO UNTIL (<VARIABLE CONDITION>);
STATEMENT1
STATEMENT2
.
.
.
END;


DO WHILE (<VARIABLE CONDITION>);
STATEMENT1
STATEMENT2
.
.
.
END;

No comments:

Post a Comment

My First SAS Program