Index Creation Operator: : |
The index creation operator (:) creates a row vector with a first element that is value1. The second element is value1+1, and so on, until the last element which is less than or equal to value2.
For example, the following statement creates the vector s which contains consecutive integers, shown in Figure 23.12:
s = 7:10; print s;
s | |||
---|---|---|---|
7 | 8 | 9 | 10 |
If value1 is greater than value2, a reverse-order index is created. For example, the following statement creates the vector r which contains a decreasing sequence of integers, shown in Figure 23.13:
r = 10:6; print r;
r | ||||
---|---|---|---|---|
10 | 9 | 8 | 7 | 6 |
Neither value1 nor value2 is required to be an integer. Use the DO function if you want an increment other than 1 or .
The index creation operator also works on character arguments with a numeric suffix. For example, the following statements create a sequence of values that begin with the prefix "var", shown in Figure 23.14:
varList = "var1":"var5"; print varList;
varList | ||||
---|---|---|---|---|
var1 | var2 | var3 | var4 | var5 |
Sequences of character values are often used to assign names to variables. You can use the string concatenation operator to dynamically determine the length of a sequence, as shown in the following statements:
x = {1 2 3 4, 5 6 7 8, 7 6 5 4}; numVar = ncol(x); /* 4 columns */ varNames = "X1":"X"+strip(char(numVar)); /* "X1":"X4" */ print x[colname=varNames];
x | |||
---|---|---|---|
X1 | X2 | X3 | X4 |
1 | 2 | 3 | 4 |
5 | 6 | 7 | 8 |
7 | 6 | 5 | 4 |