Differences between revisions 1 and 2
Revision 1 as of 2023-01-13 20:31:20
Size: 1113
Comment:
Revision 2 as of 2023-06-10 19:44:11
Size: 3616
Comment:
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:

The '''`RECODE`''' command replaces values of a variable.
Line 9: Line 11:
== Numeric Variables == == Usage ==



=== Numeric Variables ===

1 or more variables can be specified on a `RECODE` command. They must ''all'' be numeric variables.

{{{
recode foo bar baz (0=sysmis).
}}}

Ranges are valid on the left side of an instruction. They are inclusive and capture non-integers.

{{{
recode foo (0 thru 10=1).
}}}

The `LO`/`LOWEST` and `HI`/`HIGHEST` keywords dynamically capture the lowest and highest non-missing values. These keywords are only valid on ranges.

The `SYSMIS` keyword captures system missing values, while the `MISSING` keyword captures both user missing and system missing values. Both are valid on the left side of an instruction; only `SYSMIS` is a valid right side value.

The `ELSE` keyword captures all cases.

If destination variables are specified...

 * any non-existant variables are initialized with missing values for all cases.
 * the source variables are not modified.
 * the number of source variables must match the number of destination variables.
 * values are only overwritten when a case matches an instruction based on the value of the source variable.

The `COPY` instruction causes matching cases to have be returned their pre-existing value. This is generally only useful with destination variables.

{{{
recode foo (0=sysmis) into bar.
recode foo (0=sysmis) (else=copy) into bar.
}}}



=== String Variables ===

1 or more variables can be specified on a `RECODE` command. They must ''all'' be string variables.

{{{
recode foo bar baz ("."="").
}}}

If any pattern is longer than all source variables, it is implicitly truncated to that length.

Spaces are implicitly trimmed from values when checking against patterns.

The `MISSING` keyword captures user missing values. It is only valid on the left side of an instruction.

The `ELSE` keyword captures all cases.

If destination variables are specified...

 * they must all be existing string variables.
 * the source variables are not modified.
 * the number of source variables must match the number of destination variables.
 * values are only overwritten when a case matches an instruction based on the value of the source variable.

The `COPY` instruction causes matching cases to have be returned their pre-existing value. This is generally only useful with destination variables.

{{{
recode foo (0=sysmis) into bar.
recode foo (0=sysmis) (else=copy) into bar.
}}}
Line 12: Line 82:












SPSS Recode

The RECODE command replaces values of a variable.


Usage

Numeric Variables

1 or more variables can be specified on a RECODE command. They must all be numeric variables.

recode foo bar baz (0=sysmis).

Ranges are valid on the left side of an instruction. They are inclusive and capture non-integers.

recode foo (0 thru 10=1).

The LO/LOWEST and HI/HIGHEST keywords dynamically capture the lowest and highest non-missing values. These keywords are only valid on ranges.

The SYSMIS keyword captures system missing values, while the MISSING keyword captures both user missing and system missing values. Both are valid on the left side of an instruction; only SYSMIS is a valid right side value.

The ELSE keyword captures all cases.

If destination variables are specified...

  • any non-existant variables are initialized with missing values for all cases.
  • the source variables are not modified.
  • the number of source variables must match the number of destination variables.
  • values are only overwritten when a case matches an instruction based on the value of the source variable.

The COPY instruction causes matching cases to have be returned their pre-existing value. This is generally only useful with destination variables.

recode foo (0=sysmis) into bar.
recode foo (0=sysmis) (else=copy) into bar.

String Variables

1 or more variables can be specified on a RECODE command. They must all be string variables.

recode foo bar baz ("."="").

If any pattern is longer than all source variables, it is implicitly truncated to that length.

Spaces are implicitly trimmed from values when checking against patterns.

The MISSING keyword captures user missing values. It is only valid on the left side of an instruction.

The ELSE keyword captures all cases.

If destination variables are specified...

  • they must all be existing string variables.
  • the source variables are not modified.
  • the number of source variables must match the number of destination variables.
  • values are only overwritten when a case matches an instruction based on the value of the source variable.

The COPY instruction causes matching cases to have be returned their pre-existing value. This is generally only useful with destination variables.

recode foo (0=sysmis) into bar.
recode foo (0=sysmis) (else=copy) into bar.


String Variables

Values are compared to recode instructions with an implicit rtrim call (i.e. trailing whitespace is not compared).

SPSS will also emit a warning if a recode instruction includes a value that is too long to possibly be found within the variable.


Type Conversion

To convert a numeric variable into a string, try:

recode numeric_zip_code (0="NULL") (sysmis="") (convert) into string_zip_code.

Note that specific values can be handled differently by including a recode instruction ahead of the convert instruction.

To convert a string variable into a number, try:

recode string_zip_code ("NULL"=0) (""=sysmis) (convert) into numeric_zip_code.

Note that non-numeric values (including blank values) must be handled explicitly by including a recode instruction ahead of the convert instruction, or they will all be replaced with the system missing value and a warning will be emitted.


CategoryRicottone

SPSS/Recode (last edited 2023-06-11 21:05:47 by DominicRicottone)