• <GetStart>
  • CSPro User's Guide
    • The CSPro System
    • Data Dictionary Module
    • The CSPro Language
    • Data Entry Module
    • Batch Editing Applications
    • Tabulation Applications
    • Data Sources
    • CSPro Statements and Functions
      • Statement Format Symbols
      • Alphabetical List of Functions and Statements
      • List of Reserved Words
      • Deprecated Features
      • Declaration Statements
      • Symbol Functions
      • Item Functions
      • Array Object
        • Array Statement
        • Array.length Function
        • Array.clear Function
      • Audio Object
      • Barcode and QR Codes
      • Case Object
      • Document Object
      • File Object
      • Freq Object
      • Geometry Object
      • HashMap Object
      • Image Object
      • List Object
      • Map Object
      • Path
      • Pff Object
      • SystemApp Object
      • ValueSet Object
      • Program Control Statements
      • Assignment Statements
      • Data Entry Statements and Functions
      • Batch Edit Statements
      • Numeric Functions
      • String Functions
      • Multiple Occurrence Functions
      • General Functions
      • Date and Time Functions
      • External File Functions
      • Synchronization Functions
    • Templated Reporting System
    • HTML and JavaScript Integration
    • Action Invoker
    • Appendix
  • <CSEntry>
  • <CSBatch>
  • <CSTab>
  • <DataViewer>
  • <TextView>
  • <TblView>
  • <CSFreq>
  • <CSDeploy>
  • <CSPack>
  • <CSDiff>
  • <CSConcat>
  • <Excel2CSPro>
  • <CSExport>
  • <CSIndex>
  • <CSReFmt>
  • <CSSort>
  • <ParadataConcat>
  • <ParadataViewer>
  • <CSCode>
  • <CSDocument>
  • <CSView>
  • <CSWeb>

Array Statement

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.
Format 1
Array ʃarray_typeʅ array_name(dim1ʃ, ..., dimNʅ) ʃsaveʅ ʃ= dim1_valueʃ, ..., dimN_valueʅʅ;
Format 2
Array ʃarray_typeʅ array_name(dim1ʃ, ..., dimNʅ) ʃsaveʅ ʃ= dim1_valueʃ, dim2_valueʃ, ...ʅʅ;
Description
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:
alpha ʃ(array_length)ʅ
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.
Saved Arrays
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.
Example 1 (Numeric)
By including the save option, the contents of the MinAgeMarriage array will be saved to an external .sva file.
PROC GLOBAL

Array numeric MinAgeMarriage(2) save = 15, // male
                                       12; // female

PROC MARRIAGE_AGE

   
if MARRIAGE_AGE < MinAgeMarriage(SEX) then
       
errmsg("The minimum age of marriage is %d", MinAgeMarriage(SEX));
       
reenter;
   
endif;
Example 2 (String)
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;
Example 3 (Assignment)
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;
Example 4 (Repeating an Assignment with "...")
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
See also: Array Object, Saved Arrays File (.sva), DeckArrays, DeckArray Leftover Rows, HashMap Object, List Object, Numeric Statement, Alpha Statement, String Statement