Subset
Declaration(D)
Returns a list containing all the index elements of the index of one-D array for which the value is true.
When to use
Use Subset to create a new index that is a subset of an existing index.
Variations
When there is the possibility of having Null values in your array, you should consider whether you want those elements to be retained. The following two uses respectively retain or remove Null elements:
Subset(Test Or Test=Null) Subset(Test And Test<>Null)
Without the explicit test, Null elements are removed in release 4.2 and later, but retained prior to 4.2.
(new to release 4.2) The position (rather than the index value) of each non-zero element can be obtained using:
Subset(D,position:true)
See Associative vs. Positional Indexing.
Examples
Subset(Years < 1987) → [1985,1986]
Subset(Years < 1987) → [1,2] { requires 4.2 }
Array Considerations
An error will result if the parameter to Subset is not one-dimensional. Subset does not array abstract. A scalar parameter or a parameter with two or more dimensions results in an error.
The one-dimensional array passed to Subset needs to be explicitly indexed, since the index elements corresponding to true elements is returned. Thus, an implicitly indexed array (e.g., a list) also results in an error. Theoretically Subset could return a result in the implicitly indexed case, but it would be misleading since it would just be returning all the true values, for example:
Subset( 1997..2008 > 2005 ) →? [1,1,1] { Hypothetical only -- error reported }
In a case like this, you should use a local index, e.g.:
Index I := 1997..2008 do Subset(I>2005) → [2006,2007,2008]
However, if you use the position:true parameter setting, then you can pass an array with an implicit index, since element positions (and not index values) are returned. Hence, the following does work (in Analytica 4.2 or better):
Subset( 1997..2008 > 2005, position:true ) → [10,11,12]
Enable comment auto-refresher