Note the two formats below are identical except in the handling of assigning values to the array dimensions. In Format 1, all dimension values are listed out. In Format 2, the sequence "..." (three dots/periods) can be written at the end of the array declaration when providing start values for the array dimensions. The "..." signifies that all subsequent array dimensions will be assigned the values listed up to that point. See Example 4 below for further clarification.
Array ʃarray_typeʅ array_name(dim1ʃ, ..., dimNʅ) ʃsaveʅ ʃ= dim1_valueʃ, ..., dimN_valueʅʅ;
Array ʃarray_typeʅ array_name(dim1ʃ, ..., dimNʅ) ʃsaveʅ ʃ= dim1_valueʃ, dim2_valueʃ, ...ʅʅ;
The
Array statement creates an
Array with the name
array_name. Only one array at a time can be declared with the Array statement. The array name must be unique and must contain only letters, numbers, or the underscore character. The name must begin with a letter. You can declare arrays in
PROC GLOBAL or locally in functions or procedures.
Array objects can be numeric, alphanumeric or string. By default an array is numeric, but the type can be modified by specifying the array_type. If creating an alphanumeric array, the length of each array element can be specified by definining the array_type as follows:
If array_length is not specified, each element will be 16 characters.
Array Size: The size of each dimension is specified by supplying a constant positive numeric value,
dimension1 to
dimensionN. An array must have at least one dimension. The starting position of each dimension is 1, not 0. CSPro supports arrays of an unlimited number of dimensions. A previously-defined numeric value can also be used to specify the dimension size. The
Array.length function can be used to query the size of a dimension (see Example 3 below for both these conditions).
Initializing Arrays: The initial values of the array elements can be assigned when declaring the array by listing each value you wish to use. If some values are defined followed by ..., the values that have been defined to that point will be used over and over until the entire array has been initialized (see Example 4 below).
Array Start Values: With a numeric array, each element starts with the value 0. For alphanumeric and string arrays, each element starts as a blank string. If using a numeric
saved array, the initial array contents are
default.
The optional keyword
save indicates that the array values should be saved to a file and loaded from that file when the program is run again. This allows you to maintain the values of the arrays across multiple runs of the same program. When one or more arrays in the program are marked with
save, the first time the application is run, a
saved array file is created and the values of the arrays are written to the file at the end of program execution. On consecutive runs of the program, the initial values of the arrays are read in from the file. This is particularly useful for setting the initial values of hot decks. In this scenario, the program is run twice. The first run fills the hot deck and saves the hot deck array to the file. The second run loads the values saved from the first run and uses them as the initial values for the hot deck for imputation. See
Initialize Hot Decks in Program Logic for more information.
All arrays marked with save in the application are written to the same file. By default this file has the same name as the application but with a .sva file extension appended to it (for example, MyApplication.bch.sva). You can modify the name of the saved array file by using the PFF SaveArray attribute.
By including the
save option, the contents of the MinAgeMarriage array will be saved to an external
.sva file.
PROC GLOBAL
Array string Months(12) = "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec";
function string FormatDate()
FormatDate = maketext("%d %s %d", sysdate("DD"), Months(sysdate("MM")), sysdate("YYYY"));
end;
PROC GLOBAL
numeric NumberProvinces = 12;
Array numeric SexCountsByProvince(NumberProvinces, 2); // province by sex
PROC SEX
inc(SexCountsByProvince(PROVINCE, SEX));
PROC DISPLAY_COUNTS
do numeric counter = 1 while counter <= SexCountsByProvince.length(1)
errmsg("Province %d has %d males and %d females", counter,
SexCountsByProvince(counter, 1), SexCountsByProvince(counter, 2));
enddo;
PROC GLOBAL
// Relationship codes correspond to:
// 1=head
// 2=spouse
// 3=child
// 4=grandchild
// 5=parent of head
// 6=sibling of head
// 7=other relative
// 8=unrelated
Array HD_Sex_Rel (8) = 1, 2, 1, 1, 2, 2, 1, 2; // Format 1, all values are explicitly stated
Array HD_Sex_Rel (8) = 0, ...; // Format 2, zero will be assigned to all array positions
Array HD_Sex_Rel (8) = 1, 2, ...; // Format 2, values 1 & 2 will be used (respectively) for array positions 3-4, 5-6, and 7-8