• <Helps for GetStart>
  • CSPro User's Guide
    • The CSPro System
    • Data Dictionary Module
    • The CSPro Language
    • Data Entry Module
    • Batch Editing Applications
    • Tabulation Applications
    • CSPro Statements and Functions
      • Statement Format Symbols
      • Alphabetical List of Functions and Statements
      • List of Reserved Words
      • Deprecated Features
      • Declaration Statements
      • Array Object
        • Array Statement
        • Array.Length Function
        • Array.Clear Function
      • Audio Object
      • Barcode and QR Codes
      • 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
      • Export Attributes
    • Templated Reporting System
    • HTML and JavaScript Integration
    • Appendix
  • <Helps for CSEntry>
  • <Helps for CSBatch>
  • <Helps for CSTab>
  • <Helps for DataViewer>
  • <Helps for TextView>
  • <Helps for TblView>
  • <Helps for TRSWin>
  • <Helps for CSDeploy>
  • <Helps for CSPack>
  • <Helps for CSFreq>
  • <Helps for CSSort>
  • <Helps for CSExport>
  • <Helps for CSReFmt>
  • <Helps for CSDiff>
  • <Helps for CSConcat>
  • <Helps for TRSSetup>
  • <Helps for ParadataViewer>
  • <Helps for ParadataConcat>
  • <Helps for CSIndex>
  • <Helps for Excel2CSPro>
  • <Helps for CSWeb>

Array Statement

Format
array ʃarray_typeʅ array_name(dimension1ʃ, ..., dimensionNʅ) ʃsaveʅ;
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.
Arrays 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.
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. 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.
The initial values of the elements of an array can be set when declaring the array by listing each value, separated by a comma. If some values are defined and then followed by ..., that set of values will be used over and over until the entire array has been initialized.
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)
PROC GLOBAL

array numeric MinimumAgeMarriage(2) = 15, // male
                                      12; // female

PROC MARRIAGE_AGE

   
if MARRIAGE_AGE < MinimumAgeMarriage(SEX) then
       
errmsg("The minimum age of marriage is %d", MinimumAgeMarriage(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)
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;
See also: Array Object, HashMap Object, List Object, Numeric Statement, Alpha Statement, String Statement, Saved Arrays File (.sva)