• <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
      • 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
        • Break Statement
        • Do Statement
        • Exit Statement
        • For Statement
        • ForCase Statement
        • For (Dictionary) Statement
        • If Statement
        • Next Statement
        • Universe Statement
        • When Statement
        • While Statement
      • 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>

When Statement

Format
when expression1 ʃ:: expression2 :: expressionNʅ;
     
ʃvalue1ʅ    ʃ:: value2      :: valueNʅ       -> statement;
                                                 
ʃ-> else_statement;ʅ
endwhen;
Description
The when program control statement executes a statement based on the value of one or more other variables, combining if statements with the power of recode. The statement is similar to statements in other programming languages (like switch in C or when in Kotlin).
One or more expressions, expression1 to expressionN, are given, with each expression separated by two colons ::. These expressions must evaluate to either a number or string. Based on the values of each expression, CSPro evaluates each line between the when and endwhen, trying to match a line's values, value1 to valueN, with the evaluated expressions. Once a line matches, a statement, which is given after the arrow ->, is executed and program control moves to statements following the endwhen. Only a single statement can be provided per line.
Each value provided must evaluate to the same type (number or string) as its respective expression. A value omitted is considered a match, and if no values are provided, then the optional else_statement is executed (assuming no preceding line matched).
Values can be provided in the following ways:
  • An individual value. For example: 50
  • Using the syntax of in lists, meaning that multiple values can be separated with commas and ranges be separated by colons. For example: 1,3,5:9
  • An individual value preceded by one of the following operators: <, <=, >, >=, =, <>. If such an operator is used, the expression's value is compared with the value using the operator. For example: >= 65
Example 1
function SimpleWhenExample(numeric sex, numeric age)

   
when sex :: age;
         
1   ::        -> errmsg("Male");
         
2   ::  0: 11 -> errmsg("Young female");
         
2   :: 12: 49 -> errmsg("Fertile female");
         
2   :: 50:120 -> errmsg("Older female");
                       -> 
errmsg("Invalid sex or age");
   
endwhen;

end;

SimpleWhenExample(
2,  10); // Young female
SimpleWhenExample(2,  38); // Fertile female
SimpleWhenExample(1, 150); // Male
SimpleWhenExample(2, 150); // Invalid sex or age
Example 2
PROC EMPLOYMENT_STATUS

   
when EMPLOYMENT_STATUS;
         
1:2 -> skip to UNEMPLOYMENT;
         <= 
6 -> skip to INFORMAL_EMPLOYMENT
         >= 
7 -> skip to FORMAL_EMPLOYMENT;
   
endwhen;
See also: If Statement, Recode Statement