How to: |
The SELECTS function decodes a value from a stack.
target SELECTS (code, result, code, result, ... [ELSE default])
where:
Is a valid expression. It can be either a field name or a variable that resolves to a single stack cell.
Is the value for which SELECTS searches. Once the value is found, the input expression is assigned the corresponding result. The comma between the code and result is optional.
Is the value assigned when the input expression has the corresponding code.
Is the value to be assigned if the code is not found among the list of codes. If the default is omitted, a space or zero is assigned to non-matching codes.
The following computes a user-defined field based on the values in a stack:
COMPUTE Square = Stk(Cnt).Number SELECTS (1 1, 2 4, 3 9);
Because SELECTS is a binary operator, it can also be used in an expression:
COMPUTE Square_Plus = Stk(Cnt).Number SELECTS (1 1, 2 4, 3 9) +1;
The following example uses MASK to extract the first character of the field CURR_JOBCODE in the EMPLOYEE file. Then SELECTS creates a value for the field JOB_CATEGORY:
MAINTAIN FILE Employee Case Top FOR ALL NEXT EMPINFO.EMP_ID INTO EmpStack; COMPUTE DEPX_CODE/A1 = MASK(EmpStack().CURR_JOBCODE,'9$$'); JOB_CATEGORY/A15 = DEPX_CODE SELECTS (A 'ADMINISTRATIVE' B 'DATA PROCESSING') ; EndCase END
The following table shows sample values for CURR_JOBCODE and the corresponding values for JOB_CATEGORY:
CURR_JOBCODE JOB_CATEGORY ------------ ------------ A01 ADMINISTRATIVE A07 ADMINISTRATIVE A15 ADMINISTRATIVE A17 ADMINISTRATIVE B02 DATA PROCESSING B03 DATA PROCESSING B04 DATA PROCESSING B14 DATA PROCESSING