Learn R Programming

ksformat

SAS-style PROC FORMAT for R: create and apply value formats, range-based formatting, reverse formatting (invalue), and consistent handling of missing values (NA, NULL, NaN).

Installation

From GitHub (after cloning or from your repo URL):

# install.packages("remotes")
remotes::install_github("crow16384/ksformat")

From local source:

install.packages(".", repos = NULL, type = "source")
# or
devtools::install()

Features

  • Format creation — Value-to-label mappings like SAS PROC FORMAT
  • Format application — Apply formats to vectors and data frames
  • Reverse formatting — Convert labels back to values (INVALUE)
  • Missing value handling — NA, NULL, NaN, and empty values
  • Range support — Numeric ranges with inclusive/exclusive bounds
  • Format library — Register and retrieve formats globally

Quick start

Discrete formatting

library(ksformat)

fnew(
  "M" = "Male",
  "F" = "Female",
  .missing = "Unknown",
  name = "sex"
)

fput(c("M", "F", NA, "X"), "sex")
# [1] "Male"    "Female"  "Unknown" "X"

Numeric ranges

fparse(text = '
VALUE age (numeric)
  [0, 18)   = "Child"
  [18, 65)  = "Adult"
  [65, HIGH] = "Senior"
  .missing   = "Age Unknown"
;
')

fputn(c(5, 25, 70, NA), "age")
# [1] "Child"       "Adult"       "Senior"      "Age Unknown"

Reverse formatting (invalue)

finput("Male" = 1, "Female" = 2, name = "sex_inv")

finputn(c("Male", "Female", "Unknown"), "sex_inv")
# [1]  1  2 NA

Format library

fprint()              # list all registered formats
fmt <- format_get("sex")
fclear("sex")         # remove one format
fclear()              # clear all

Data frames

df <- data.frame(
  sex = c("M", "F", "M", NA),
  age = c(15, 25, 70, 35)
)

fput_df(df, sex = format_get("sex"), age = format_get("age"), suffix = "_label")

Missing value handling

Priority order:

  1. NA, NULL, NaN.missing label if defined, otherwise NA
  2. Exact match → value–label mapping
  3. Range match → range label (numeric formats)
  4. No match.other label or original value

Options: keep_na = TRUE, na_if, include_empty = TRUE.

Cheat sheet

  • In R: run ksformat_cheatsheet() to open the cheat sheet in your browser (HTML), or ksformat_cheatsheet("pdf") for the PDF.
  • In this repo: HTML | PDF

Function reference

AreaFunctions
Creationfnew(), finput(), fnew_bid(), fnew_date(), fparse()
Applicationfput(), fputn(), fputc(), fput_all(), fput_df()
Reversefinputn(), finputc()
Libraryformat_get(), fprint(), fclear(), fexport(), fimport()
Utilitiesis_missing(), range_spec()
Documentationksformat_cheatsheet() — open cheat sheet

Development

install.packages(c("roxygen2", "testthat", "devtools"))
devtools::document()
devtools::test()
devtools::check()

When bumping the package version, update DESCRIPTION and then run
Rscript scripts/sync-version.R to refresh version references in cran-comments.md and any other synced files.

License

GPL-3. See https://www.gnu.org/licenses/gpl-3.0.html.

Copy Link

Version

Install

install.packages('ksformat')

Version

0.3.5

License

GPL-3

Maintainer

Vladimir Larchenko

Last Published

March 25th, 2026

Functions in ksformat (0.3.5)

fparse

Parse Format Definitions from 'SAS'-like Text
fexport

Export Formats to 'SAS'-like Text
fclear

Remove Format(s) from Library
fimport

Import Formats from 'SAS' PROC FORMAT CNTLOUT CSV
.to_r_datetime

Convert input to R POSIXct
finputc

Apply Character Invalue by Name (like 'SAS' INPUTC)
fnew_date

Create Date/Time Format
fnew_bid

Create Bidirectional Format
format_get

Retrieve a Format from the Library
finput

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

Create a Format Definition (like 'SAS' PROC FORMAT)
finputn

Apply Numeric Invalue by Name (like 'SAS' INPUTN)
fprint

Print Format(s) from Library
.to_r_date

Convert input to R Date
.unquote

Remove surrounding quotes from a string
.format_to_text

Convert ks_format to SAS-like text
.invalue_to_text

Convert ks_invalue to SAS-like text
.format_time_values

Format time values
.format_range_bound

Format a numeric range bound for text output
.parse_blocks

Parse text lines into block structures
.is_sas_datetime_format

Check if a name corresponds to a built-in SAS datetime format
format_datetime

Date/Time Format Support ('SAS'-style)
fput_df

Apply Format to Data Frame Columns
fputc

Apply Character Format by Name (like 'SAS' PUTC)
ksformat_cheatsheet

Open the ksformat cheat sheet
print.ks_format

Print Format Object
fput

Apply Format to Data (like 'SAS' PUT function)
is_missing

Check if Value is Missing
print.ks_invalue

Print Invalue Object
fputn

Apply Numeric Format by Name (like 'SAS' PUTN)
ksformat-package

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

Create Range Specification
utilities

Utilities for ksformat Package
fput_all

Apply Format and Return All Matches (Multilabel)
.block_to_format

Convert a parsed block to a ks_format or ks_invalue object
.apply_datetime_format

Apply a date/time format to a vector
detect_format_type

Detect Format Type
.block_to_ks_datetime_format

Convert VALUE block with date/time/datetime type to ks_format
.block_to_ks_invalue

Convert INVALUE block to ks_invalue
.format_date_values

Format date values
.block_to_ks_format

Convert VALUE block to ks_format
.datetime_format_to_text

Convert datetime ks_format to SAS-like text
.normalize_sas_format_name

Normalize a SAS format name (strip trailing period, uppercase)
.cntlout_to_ks_format

Convert CNTLOUT rows to ks_format
.cntlout_to_ks_invalue

Convert CNTLOUT rows to ks_invalue
.format_library

Format Library (Global Environment for Storing Formats)
.format_datetime_values

Format datetime values
.parse_mapping_line

Parse a single mapping line
.invalue_apply

Apply Invalue Format (Reverse Formatting)
.parse_range_bound

Parse a range bound value
.resolve_sas_format_def

Resolve a SAS format definition from the built-in table
.sas_format_defaults

Default widths for SAS format names without explicit width