Language Reference


Index Creation Operator:   :

  • value1 : value2;

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 24.13:

s = 7:10;
print s;

Figure 24.13: Increasing Sequence

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 24.14:

r = 10:6;
print r;

Figure 24.14: Decreasing Sequence

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 $-1$.

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 24.15:

varList = "var1":"var5";
print varList;

Figure 24.15: Sequence of Character Values

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];

Figure 24.16: Sequence of Variable Names

x
X1 X2 X3 X4
1 2 3 4
5 6 7 8
7 6 5 4



Lastly, you can use the index operator to create a sequence of English letters, in either increasing or descending order, as follows:

a = "a":"h";
b = "P":"L";
print a, b;

Figure 24.17: Sequence of Letters

a
a b c d e f g h

b
P O N M L