Package 'stringSAS'

Title: SAS-Inspired Character Manipulation Functions
Description: Provides SAS-inspired character manipulation functions for R users who regularly work with both SAS and R. Includes functions for character compression, word extraction, substring operations, concatenation, searching, and replacement. Supports vectorized operations and integration with data manipulation workflows.
Authors: Geonhyeok Jeong [aut, cre]
Maintainer: Geonhyeok Jeong <[email protected]>
License: MIT + file LICENSE
Version: 0.1.0
Built: 2026-12-06 13:43:03 UTC
Source: https://github.com/statsergeant/stringsas

Help Index


Concatenate Strings with a Delimiter Using SAS CATX Syntax

Description

Concatenates character strings using a delimiter, removing leading and trailing spaces and skipping blank or missing values.

Usage

sas_catx(delimiter, ...)

Arguments

delimiter

A single non-missing character string.

...

Character vectors to concatenate. Each argument must have length 1 or the common output length.

Value

A character vector containing concatenated strings. Blank and missing elements are skipped.

Examples

sas_catx("-", "STUDY01", "001", "0001")
sas_catx("-", " ABC ", " DEF ")
sas_catx("-", "ABC", "", "DEF")
sas_catx("-", c("A", "B"), c("1", "2"))

Compress Character Strings Using SAS-Inspired Syntax

Description

Removes or retains characters based on literal character selections and SAS-inspired modifiers.

Usage

sas_compress(x, chars = NULL, modifier = "")

Arguments

x

A character vector.

chars

A single string of characters to select. If NULL, ordinary spaces are removed when no character class modifier is supplied.

modifier

A single string containing any combination of a (letters), d (ASCII digits), s (whitespace), and k (keep selected characters).

Value

A character vector of the same length as x. Missing values remain missing.

Examples

sas_compress("A B C")
sas_compress("AB123", modifier = "d")
sas_compress("AB123", modifier = "kd")
sas_compress("A-B", chars = "-")
sas_compress(c("A B", NA_character_))

Find a Substring Using SAS-Inspired FIND Syntax

Description

Searches for a substring and returns its 1-based position, or zero when no match is found.

Usage

sas_find(x, substring, modifier = "", startpos = 1)

Arguments

x

A character vector to search.

substring

A single non-missing character string.

modifier

Either "" for case-sensitive matching or "i" for case-insensitive matching.

startpos

A single positive integer indicating where to begin searching.

Value

An integer vector of the same length as x. Returns 0 for no match and NA for missing inputs.

Examples

sas_find("Hello World", "World")
sas_find("Hello World", "world", "i")
sas_find("ABCABC", "ABC", startpos = 4)
sas_find(c("ABC", "DEF"), "B")

Extract Words Using SAS-Inspired SCAN Syntax

Description

Extracts a word from a character string using a positive or negative word position.

Usage

sas_scan(x, n, delimiters = NULL)

Arguments

x

A character vector.

n

A single nonzero integer indicating word position.

delimiters

A single string containing delimiter characters. If NULL, whitespace is used.

Details

Positive positions count from the beginning. Negative positions count from the end.

Consecutive delimiters are treated as one separator. Unlike SAS, nonexistent words return NA_character_.

Value

A character vector with the same length as x. Missing inputs and nonexistent words return NA.

Examples

sas_scan("John Smith", 1)
sas_scan("John Smith", -1)
sas_scan("A-B-C", 2, "-")
sas_scan(c("A-B", "C-D"), 2, "-")

Extract Substrings Using SAS-Inspired Syntax

Description

Extracts a substring using a starting position and a character count, following SAS SUBSTR conventions.

Usage

sas_substr(x, start, length = NULL)

Arguments

x

A character vector.

start

A single numeric starting position. Positions are 1-based.

length

A single non-negative integer indicating the number of characters to extract. If NULL, extracts to the end of the string.

Value

A character vector with the same length as x. Missing values remain missing.

Examples

sas_substr("STUDY001", 1, 5)
sas_substr("STUDY001", 6, 3)
sas_substr("STUDY001", 6)
sas_substr(c("ABC123", "DEF456"), 1, 3)

Replace Substrings Using SAS TRANWRD Syntax

Description

Replaces every occurrence of a target substring with a replacement string. Matching is literal and case-sensitive, following SAS TRANWRD conventions.

Usage

sas_tranwrd(x, target, replacement)

Arguments

x

A character vector.

target

A single non-missing string to find.

replacement

A single non-missing replacement string.

Value

A character vector with the same length as x. Missing input values remain missing.

Examples

sas_tranwrd("A-B-C", "-", "_")
sas_tranwrd("Hello SAS", "SAS", "R")
sas_tranwrd(c("ABC", "ABCABC"), "ABC", "X")