• <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
      • 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
        • Assignment Statement
        • Recode Statement
        • Recode Statement (Deprecated)
        • Impute Function
        • SetValue Function
        • GetValue Function
      • 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>

Recode Statement

(Prior to CSPro 7.4, a different version of the recode/box command function existed. That version can no longer be used as it was removed in CSPro 8.0.)
Format
recode expression1 ʃ:: expressionNʅ -> destination_variable1 ʃ:: destination_variableNʅ;
       
value1      ʃ:: valueNʅ      -> result1               ʃ:: resultNʅ;
                                   
ʃ-> else_result1          ʃ:: else_resultNʅ;ʅ
endrecode;
Description
The recode statement assigns a value to one or more output variables based on the value of one or more other input variables. It can be used to rescale variables, to assign values to variables, or to create a composite variable from existing variables. In many instances it is easier to use than writing multiple if statements.
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 recode and endrecode, trying to match a line's values, value1 to valueN, with the evaluated expressions. Once a line matches, one or more results, result1 to resultN, which are given after the arrow ->, are assigned to destination_variable1 to destination_variableN, which can be dictionary items, working variables, array values, list values, or the return values of a user-defined function. A destination variable can also be included among the expressions. It is possible to declare a new numeric, alpha, or string destination variable as part of the recode statement (see Example 2 below).
Each value provided must evaluate to the same type (number or string) as its respective expression, and each result must be the same type as the destination variable. A value omitted is considered a match, and if no values are provided, then the optional else_result1 to else_resultN are used in the assignment (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
recode AGE   -> AGE_GROUP;
       
0:19 -> 1;
       
20:29 -> 2;
       
30:39 -> 3;
       
40:49 -> 4;
       >= 
50 -> 5;
             -> 
9;
endrecode;
Example 2
PROC AGE

   
recode RELATIONSHIP :: AGE(1) - AGE -> numeric valid_age_difference_with_head;
           
3, 4         :: <  12        -> false; // biological and step children must be 12+ years younger
           6            :: > -12        -> false; // parents must be 12+ years older
           7            :: <  24        -> false; // grandchildren must be 24+ years younger
                                        -> true;
   
endrecode;
Example 3
function string GetDisplayName()

   
recode NAME -> GetDisplayName;
           
""   -> "(Undefined)";
                -> 
strip(toupper(NAME));
   
endrecode;

end;
Example 4
recode INDUSTRY_SECTION -> min_division :: max_division;
                   
"A" ->            1 ::  3;
                   
"B" ->            5 ::  9;
                   
"C" ->           10 :: 33;
                   
// ...
endrecode;
See also: If Statement, When Statement