Differences between revisions 1 and 4 (spanning 3 versions)
Revision 1 as of 2023-01-13 20:31:20
Size: 1113
Comment:
Revision 4 as of 2023-06-11 21:05:47
Size: 3130
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 ("NULL"="").
}}}

If any string literal on the left side of an instruction is longer than all source variables, it is implicitly truncated to that length. Spaces are implicitly trimmed from values when checking against left sides.

A string literal on the right side of an instruction cannot be longer than any source variables.

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 ("NULL"="") into bar.
recode foo ("NULL"="") (else=copy) into bar.
}}}

The `CONVERT` instructions causes strings representing numeric literals to be converted into actual numeric values.

{{{
recode foo ("NULL"=sysmis) (convert) into bar.
}}}
Line 15: Line 91:
== String Variables == == Data Model ==
Line 17: Line 93:
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.
The `RECODE` command is queued as a pending transformation.
Line 25: Line 99:
== Type Conversion == == See also ==
Line 27: Line 101:
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.
[[https://www.gnu.org/software/pspp/manual/html_node/RECODE.html|PSPP manual for RECODE]]

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 ("NULL"="").

If any string literal on the left side of an instruction is longer than all source variables, it is implicitly truncated to that length. Spaces are implicitly trimmed from values when checking against left sides.

A string literal on the right side of an instruction cannot be longer than any source variables.

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 ("NULL"="") into bar.
recode foo ("NULL"="") (else=copy) into bar.

The CONVERT instructions causes strings representing numeric literals to be converted into actual numeric values.

recode foo ("NULL"=sysmis) (convert) into bar.


Data Model

The RECODE command is queued as a pending transformation.


See also

PSPP manual for RECODE


CategoryRicottone

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