ksformat-package: ksformat: 'SAS'-Style 'PROC FORMAT' for R

ksformat-packageR Documentation

ksformat: 'SAS'-Style 'PROC FORMAT' for R

Description

Provides 'SAS' 'PROC FORMAT'-like functionality for creating and applying value formats in R. The package supports mapping values to labels, range-based formatting, reverse formatting (invalue), date/time/datetime formatting, and proper handling of missing values (NA, NULL, NaN).

Details

Format creation:

  • fnew — create value-to-label mappings (formats)

  • finput — create reverse mappings (label-to-value invalues)

  • fnew_bid — create both format and invalue simultaneously

  • fnew_date — create date/time/datetime formats ('SAS'-style or custom strftime patterns)

  • fparse — parse 'SAS'-like format definitions from text or file

  • fimport — import formats from a 'SAS' CNTLOUT CSV file

  • e — mark a label for expression evaluation at apply-time

Format application:

  • fput — apply a format to a vector (value to label)

  • fputn — apply a numeric format by name (like 'SAS' PUTN)

  • fputc — apply a character format by name (like 'SAS' PUTC)

  • fput_all — apply a multilabel format returning all matching labels

  • fput_df — apply formats to data frame columns

Reverse formatting:

  • finputn — apply a numeric invalue by name (like 'SAS' INPUTN)

  • finputc — apply a character invalue by name (like 'SAS' INPUTC)

Format library:

  • format_get — retrieve a format from the global library

  • fprint — list or display registered formats

  • fclear — remove one or all formats from the library

  • format_library_app — open interactive library browser (Shiny)

  • fexport — export formats to 'SAS'-like text

Utilities:

  • is_missing — check for NA, NaN, and empty strings

  • range_spec — create a range specification object

Key features:

  • Discrete and range-based numeric formatting with configurable inclusive/exclusive bounds

  • Multilabel formats — a value can match multiple labels (multilabel = TRUE in fnew, retrieved with fput_all)

  • Case-insensitive matching (ignore_case = TRUE in fnew)

  • Expression labels — labels containing .x1, .x2, etc. are evaluated at apply-time; see also e

  • Date/time/datetime formatting with built-in 'SAS' format names (auto-resolved) or custom strftime patterns

  • Global format library with auto-registration and case-insensitive name lookup

  • CNTLOUT import — read format catalogues exported from 'SAS'

Cheat sheet: run ksformat_cheatsheet() to open the HTML version in your browser, or see the files in system.file("doc", package = "ksformat").

Author(s)

Maintainer: Vladimir Larchenko vladimir.larchenko@keystatsolutions.com

Authors:

See Also

Source repository and issue tracker: https://github.com/crow16384/ksformat

Examples

# Discrete format
fnew("M" = "Male", "F" = "Female", .missing = "Unknown", name = "sex")
fput(c("M", "F", NA), "sex")

# Numeric range format (parsed from text)
fparse(text = '
VALUE age (numeric)
  [0, 18)  = "Child"
  [18, 65) = "Adult"
;
')
fputn(c(5, 25), "age")

# Bidirectional format + invalue
fnew_bid("A" = "Active", "I" = "Inactive", name = "status")
fputc("A", "status")
finputc("Active", "status_inv")

# Multilabel format
ml <- fnew(
  "0,17,TRUE,TRUE" = "Pediatric",
  "18,Inf,TRUE,TRUE" = "Adult",
  "0,Inf,TRUE,TRUE" = "Any Age",
  name = "agegrp", type = "numeric", multilabel = TRUE
)
fput_all(c(10, 30), ml)

# Date format (SAS-style, auto-resolved)
fputn(Sys.Date(), "DATE9.")

# Export and library management
cat(fexport(sex = format_get("sex")))
flist()           # character vector of registered names
fprint()
fclear()


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