Conditional Execution of SAS code
Conditional execution of code is a fundamental concept in computer programming and is commonly done using the IF…THEN…ELSE syntax in SAS. However, there are various other techniques available for SAS programmers to implement conditional logic. In this article, we will examine alternative methods to IF…THEN…ELSE such as the SELECT statement, the IFC and IFN functions, the CHOOSE and WHICH families of functions, and more esoteric methods.
One alternative to IF…THEN…ELSE is the SELECT statement, which operates by specifying an expression as part of the SELECT statement and comparing it to the expression(s) associated with each WHEN statement. When the SELECT expression is equal to one of the WHEN expressions, an associated statement is executed.
The IFC and IFN functions, introduced in SAS version 9, offer a more compact and elegant alternative to IF…THEN…ELSE logic. Both functions accept the same four arguments: a logical expression to be tested, a value to be returned if the expression is true, a value to be returned if the expression is false, and an optional value to be returned if the expression is missing. IFC returns a character value, while IFN returns a numeric value.
The CHOOSE and WHICH families of functions are useful for performing conditional operations on numeric data, where the result of the operation depends on the value of an index. They can be used to return values, perform calculations, or control the flow of execution.
While the IFC and IFN functions offer a high level of versatility in SAS programming, they can also present potential problems if not used carefully. One important aspect to consider is that each of the arguments passed to IFC or IFN are evaluated before the logical test is performed, which can result in errors or unexpected results if one of the arguments is invalid under certain conditions. To avoid these issues, it may be necessary to use alternative techniques, such as the DIVIDE function or the PROC FORMAT procedure, which can offer a more streamlined and maintainable approach. The PROC FORMAT procedure, in particular, allows for the creation of custom informats that can be used repeatedly in different programs, reducing the need for multiple copies of the same conditional logic.
The WHICHC and WHICHN functions, along with the CHOOSEC and CHOOSEN functions, are powerful tools in SAS programming that can help simplify complex conditional logic. They allow developers to search lists and perform in-line table lookups, enabling a more concise and efficient representation of conditional statements. It is important to keep in mind that these functions return missing values if the first argument is missing or zero if it fails to match any subsequent argument, so it is crucial to design conditional logic accordingly. By incorporating these functions into their programming, developers can make their code more streamlined and easier to maintain.
The COALESCE and COALESCEC functions are used to find the first non-missing value among a list of expressions. COALESCE operates on numeric values while COALESCEC operates on character data. The functions can be used to simplify conditional logic that selects the first non-missing value from a set of expressions. For example, the logic to determine the last date of contact for a subject in a clinical trial can be simplified using the COALESCE function.
The comparison operator allows you to perform conditional logic by comparing two values. If the comparison evaluates to true, then a certain value is assigned, and if it evaluates to false, then a different value is assigned. For example, in the given scenario, the comparison operator is used to determine the study day (AESTDY) corresponding to an adverse event record (AESTDT) by comparing it with the date of the first dose (EXSTDT). The comparison operator is used within an expression on the right-hand side of an assignment statement, and the result of the comparison (1 if true, 0 if false) is used to add 1 to the study day if AESTDT is equal to or later than EXSTDT. The use of the comparison operator simplifies the conditional logic and makes the code more concise.
The subsetting IF statement is used in SAS to exclude or subset records based on a certain condition. The syntax of the subsetting IF statement is an IF statement followed by an expression to be evaluated. If the expression evaluates to true, the current record is written to the output data set, otherwise the current iteration of the DATA step terminates without writing any output and control returns to the top of the data step. The subsetting IF statement is similar to an IF…THEN statement, but it only writes the output if the condition is met, and it does not include an ELSE clause.
The %IF macro statement is used within SAS macros to conditionally generate SAS code based on the value of one or more macro variables. It is part of the SAS/Macro facility, which is a separate language from the DATA step and has its own syntax. Unlike the IF-THEN-ELSE statement, which can only be used within the DATA step, the %IF macro statement can generate a variety of SAS code, including DATA step code, SAS procedures, or combinations thereof.