Language Reference

CHOOSE Function

conditionally chooses and changes elements

CHOOSE( condition, result-for-true, result-for-false)

The inputs to the CHOOSE function are as follows:
condition
is checked for being true or false for each element.

result-for-true
is returned when condition is true.

result-for-false
is returned when condition is false.

The CHOOSE function examines each element of the first argument for being true (nonzero and not missing) or false (zero or missing). For each true element, it returns the corresponding element in the second argument. For each false element, it returns the corresponding element in the third argument. Each argument must be conformable with the others or be a single element to be propagated.

For example, suppose that you want to choose between x and y according to whether x\char93 y is odd or even, respectively. You can use the following statements to execute this task:

  
       x={1, 2, 3, 4, 5}; 
       y={101, 205, 133, 806, 500}; 
       r=choose(mod(x#y,2)=1,x,y); 
       print x y r;
 
Here is the result:
  
                       X         Y         R 
                       1       101         1 
                       2       205       205 
                       3       133         3 
                       4       806       806 
                       5       500       500
 
Suppose you want all missing values in x to be changed to zeros. Submit the following statements to create a matrix with missing values:
  
     x={1 2 ., 100 . -90, . 5 8}; 
     print x; 
  
                  X             3 rows      3 cols    (numeric) 
  
                                  1         2         . 
                                100         .       -90 
                                  .         5         8
 
The following statement replaces the missing values in x with zeros:
  
     x=choose(x=.,0,x); 
     print x; 
  
                  X             3 rows      3 cols    (numeric) 
  
                                  1         2         0 
                                100         0       -90 
                                  0         5         8
 

Previous Page | Next Page | Top of Page