finput: Create Invalue Format (Reverse Formatting like 'SAS' INVALUE)

View source: R/format_invalue.R

finputR Documentation

Create Invalue Format (Reverse Formatting like 'SAS' INVALUE)

Description

Creates an invalue format that converts formatted labels back to values. This is similar to 'SAS' PROC FORMAT with INVALUE statement. The invalue is automatically stored in the global format library if name is provided.

Usage

finput(..., name = NULL, target_type = "numeric", missing_value = NA)

Arguments

...

Named arguments defining label-value mappings (reverse of fnew), or one or more named vectors/lists using c(Label = value). Example: "Male" = 1, "Female" = 2 or c(Male = 1, Female = 2).

name

Character. Optional name for the invalue format. If provided, the invalue is automatically registered in the global format library.

target_type

Character. Type to convert to: "numeric" (default), "integer", "character", or "logical". INVALUE formats produce numeric output by default; character-to-character conversion should use a regular VALUE format (fnew) instead.

missing_value

Value to use for missing inputs (default: NA)

Value

An object of class "ks_invalue" containing the invalue definition. The object is also stored in the format library if name is given.

Examples

# Convert text labels to numeric codes
finput(
  "Male" = 1,
  "Female" = 2,
  name = "sex_inv"
)

# Apply using finputn (numeric invalue by name)
finputn(c("Male", "Female", "Unknown"), "sex_inv")
# [1]  1  2 NA
fclear()

# From a named vector
finput(c(Male = 1, Female = 2), name = "sex_inv2")
finputn(c("Male", "Female"), "sex_inv2")
# [1] 1 2
fclear()

ksformat documentation built on May 21, 2026, 9:07 a.m.