• <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 (Deprecated)

Feature Upgrade: Starting with CSPro 7.4, this version of recode has been superseded by a new version, recode. This version was removed in CSPro 8.0. To update to the new version, you must change three things:
  1. Replace => with ->
  2. When using multiple variables, replace : with ::
  3. Modify ranges from using - to using : (as with in lists)
For example, this shows the differences between using recode prior to CSPro 7.4 and using it starting with CSPro 7.4:
// prior to CSPro 7.4
recode SEX :   AGE => ASK_FERTILITY;
         
2 : 12-49 => 1;
           :       => 
0;
endrecode;

// CSPro 7.4+
recode SEX ::   AGE -> ASK_FERTILITY;
         
2 :: 12:49 -> 1;
                    -> 
0;
endrecode;
 
The following documentation is for the old recode statement.

Format:

recode var-1 [:var-2 [:var-n]]   => var-out;

[range-1] [:range-2 [:range-n]]  => exp;

[range-1] [:range-2 [:range-n]]  => exp;

: : :

[: [:]]   => other-exp;

endrecode;

 

[ ] indicates that this part is optional.

 

Description:

The recode statement assigns a value to a variable based on the value of one or more other variables. It is used to rescale variables, to assign values to variables, and to create new variables from existing ones. It works like a multiple if statement but is easier to use. The recode statement evaluates each line within it sequentially, beginning with the first line.

 

If the values of variables "var-1" to "var-n" lie within the ranges "range-1" to "range-n", respectively, then "var-out" is assigned the value given by the expression on the first line and the recode statement is ended. If the values of the variables "var-1" to "var-n" do not all lie within their specified ranges, then the next line of the recode statement is evaluated. This process continues until either a value is assigned to "var-out" or the end of the recode statement is reached.

 

A variable in a multiple record or group cannot be used in the recode statement except in data entry applications (where it may be specified without an index and the current occurrence of a variable is assumed). Use working variables to refer to or to assign values to variables in multiple sections or groups.

 

Variables "var-1" through "var-n" are referred to as independent variables and must be separated by colons [:]. "Var-out", the variable whose value is assigned by the recode statement, is referred to as the dependent variable. A recode statement can have any number of independent variables, but only one dependent variable. The dependent variable can also be included among the independent variables. The dependent variable is separated from the independent variables by =>.

 

The ranges specified in the recode statement (i.e., "range-1" through "range-n") can take the following formats:

 

• A range between two values, e.g., 12-15

• An individual value, e.g., 9

• A comparison with another value, using >, <, >=, <=, or <>, e.g., < 5

• A special value , e.g., NOTAPPL

• Some combination of these formats separated by a comma, e.g., < 5, 9, 12-15, missing

 

A blank range for an independent variable includes all values. A blank range for all independent variables on the last line of a recode statement acts as a catch-all condition. It ensures that a value is always assigned to "var-out" by the recode statement. If a value is not assigned by the recode statement, the value of "var-out" will not change. The number of ranges on each line must equal the number of independent variables.

 

The expression for the dependent variable must result in a numeric value (if "var-out" is a numeric variable) or a string (if "var-out" is an alphanumeric variable).

 

(Note to ISSA users: The Recode and Box statements are identical.)

 

Example 1:

recode AGE => AGE_GROUP;

      0-19 => 1;

     20-29 => 2;

     30-39 => 3;

     40-49 => 4;

     >= 50 => 5;

           => 9;

endrecode;

 

is equivalent to the following if statements:

 

if AGE in 0:19 then

  AGE_GROUP = 1;

elseif AGE in 20:29 then

  AGE_GROUP = 2;

elseif AGE in 30:39 then

  AGE_GROUP = 3;

elseif AGE in 40:49 then

  AGE_GROUP = 4;

elseif AGE >= 50 then

  AGE_GROUP = 5;

else

  AGE_GROUP = 9;

endif;

 

Example 2:

recode ATTEND : ED_LEVEL => EDUC;

    2,notappl :          => 1;

            1 : 1        => 2;

            1 : 2,3      => 3;

              :          => 9;

endrecode;

 

is equivalent to the following if statements:

 

if (ATTEND = 2 or ATTEND = notappl) then

  EDUC = 1;

elseif ATTEND = 1 then

  if ED_LEVEL = 1 then

    EDUC = 2;

  elseif ED_LEVEL in 2:3 then

    EDUC = 3;

  endif;

else

  EDUC = 9;

endif;

 

Example 3:

recode UNITS : NUMBER  => DAYS;

             : notappl => notappl;

             : missing => missing;

           1 :         => NUMBER;

           2 :         => NUMBER*7;

           3 :         => NUMBER*30;

           4 :         => NUMBER*365;

             :         => missing;

endrecode;

 

is equivalent to the following if statements:

 

if NUMBER = notappl then DAYS = notappl;

elseif NUMBER = missing then DAYS = missing;

elseif UNITS = 1 then DAYS = NUMBER;

elseif UNITS = 2 then DAYS = NUMBER*7;

elseif UNITS = 3 then DAYS = NUMBER*30;

elseif UNITS = 4 then DAYS = NUMBER*365;

else DAYS = missing;

endif;

 

See also: If Statement