• <GetStart>
  • CSPro User's Guide
    • The CSPro System
    • Data Dictionary Module
    • The CSPro Language
      • Introduction to CSPro Language
      • Data Requirements
      • CSPro Program Structure
      • Programming Standards
      • Code Folding
      • Debugging CSPro Applications
      • Declaration Section
      • Procedural Sections
      • Logic
      • Language Elements
        • Version
        • Delimiters
        • Comments
        • Preprocessor
        • Variables and Constants
          • Data Items
          • This Item ($)
          • Subscripts
          • Numbers
          • Boolean Values
          • Special Values
          • Refused Value
          • String Literals
          • Newline Handling
        • Expressions
        • Operators
        • Files
        • Miscellaneous
    • Data Entry Module
    • Batch Editing Applications
    • Tabulation Applications
    • Data Sources
    • CSPro Statements and 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>

Subscripts

Items with multiple occurrences or in multiple records have one name (the item name), but can occur multiple times. In order to indicate the specific occurrence of the item, you may need to use an index or subscript. The subscripts are integers and are numbered from 1.
Imagine that the SEX is an item in the multiple record CHILD.
The expressions:
SEX(1)refers to the sex of the first child
SEX(3)refers to the sex of the third child
SEX(i)refers to the sex of the i-th child
Subscripts can be numeric expressions as well as numeric constants. For example, the expression:
SEX(curocc(CHILD));
refers to the current occurrence of CHILD (curocc is a function that returns the current occurrence of a multiple record). When referring to multiply-occurring items within the scope of their repetition, you do not need to use subscripts, as the current occurrence will be assumed. For example, suppose you have a population record that has multiple occurrences. Belonging to that record are the three variables SEX, AGE, and FERTILITY. If your code is located within any of these variables' procedures, you do not need to use subscripts. To illustrate:
Example 1
PROC SEX
   
// this will check the sex and fertility values for each person in the household
    numeric row_num = curocc();

   
if $ = 1 then
       
if fertility <> notappl then
           
errmsg("Row %d: Male found with fertility", row_num);
       
endif;
   
elseif $ = 2 then
       
if age < 10 and fertility <> notappl then
           
errmsg("Row %d: Underage female found with fertility data", row_num);
       
endif;
   
else
       
errmsg("Row %d: Invalid sex code (sex=%d)", row_num, $);
   
endif;
However, if you were to place the exact same logic elsewhere in your program, you would have to programmatically mimic the looping mechanism by using subscripts. For example, if the above code were placed in the QUEST procedure (where QUEST is equivalent to the case), it would need to be adjusted as follows:
Example 2
PROC QUEST
    NumPeople = 
count(POP_RECS);
   
do varying numeric i=1 while i <= NumPeople
       
if sex(i) = 1 then
           
if fertility(i) <> notappl then
               
errmsg("Row %d: Male found with fertility", i);
           
endif;
       
elseif sex(i) = 2 then
           
if age(i) < 10 and fertility(i) <> notappl then
               
errmsg("Row %d: Underage female found with fertility data", i);
           
endif;
       
else
           
errmsg("Row %d: Invalid sex code (sex=%d)", i, sex(i));
       
endif;
   
enddo;
On the other hand, it simplified our logic for the row number, as we don't need to make a call to curocc anymore to find out what row we're on—we know it now, as we're controlling the looping.