| 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-05 20:03:19 UTC |
| Source: | https://github.com/cran/stringSAS |
Concatenates character strings using a delimiter, removing leading and trailing spaces and skipping blank or missing values.
sas_catx(delimiter, ...)sas_catx(delimiter, ...)
delimiter |
A single non-missing character string. |
... |
Character vectors to concatenate. Each argument must have length 1 or the common output length. |
A character vector containing concatenated strings. Blank and missing elements are skipped.
sas_catx("-", "STUDY01", "001", "0001") sas_catx("-", " ABC ", " DEF ") sas_catx("-", "ABC", "", "DEF") sas_catx("-", c("A", "B"), c("1", "2"))sas_catx("-", "STUDY01", "001", "0001") sas_catx("-", " ABC ", " DEF ") sas_catx("-", "ABC", "", "DEF") sas_catx("-", c("A", "B"), c("1", "2"))
Removes or retains characters based on literal character selections and SAS-inspired modifiers.
sas_compress(x, chars = NULL, modifier = "")sas_compress(x, chars = NULL, modifier = "")
x |
A character vector. |
chars |
A single string of characters to select.
If |
modifier |
A single string containing any combination
of |
A character vector of the same length as x.
Missing values remain missing.
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_))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_))
Searches for a substring and returns its 1-based position, or zero when no match is found.
sas_find(x, substring, modifier = "", startpos = 1)sas_find(x, substring, modifier = "", startpos = 1)
x |
A character vector to search. |
substring |
A single non-missing character string. |
modifier |
Either |
startpos |
A single positive integer indicating where to begin searching. |
An integer vector of the same length as x.
Returns 0 for no match and NA for missing inputs.
sas_find("Hello World", "World") sas_find("Hello World", "world", "i") sas_find("ABCABC", "ABC", startpos = 4) sas_find(c("ABC", "DEF"), "B")sas_find("Hello World", "World") sas_find("Hello World", "world", "i") sas_find("ABCABC", "ABC", startpos = 4) sas_find(c("ABC", "DEF"), "B")
Extracts a word from a character string using a positive or negative word position.
sas_scan(x, n, delimiters = NULL)sas_scan(x, n, delimiters = NULL)
x |
A character vector. |
n |
A single nonzero integer indicating word position. |
delimiters |
A single string containing delimiter
characters. If |
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_.
A character vector with the same length as x.
Missing inputs and nonexistent words return NA.
sas_scan("John Smith", 1) sas_scan("John Smith", -1) sas_scan("A-B-C", 2, "-") sas_scan(c("A-B", "C-D"), 2, "-")sas_scan("John Smith", 1) sas_scan("John Smith", -1) sas_scan("A-B-C", 2, "-") sas_scan(c("A-B", "C-D"), 2, "-")
Extracts a substring using a starting position and a character count, following SAS SUBSTR conventions.
sas_substr(x, start, length = NULL)sas_substr(x, start, length = NULL)
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. |
A character vector with the same length as x.
Missing values remain missing.
sas_substr("STUDY001", 1, 5) sas_substr("STUDY001", 6, 3) sas_substr("STUDY001", 6) sas_substr(c("ABC123", "DEF456"), 1, 3)sas_substr("STUDY001", 1, 5) sas_substr("STUDY001", 6, 3) sas_substr("STUDY001", 6) sas_substr(c("ABC123", "DEF456"), 1, 3)
Replaces every occurrence of a target substring with a replacement string. Matching is literal and case-sensitive, following SAS TRANWRD conventions.
sas_tranwrd(x, target, replacement)sas_tranwrd(x, target, replacement)
x |
A character vector. |
target |
A single non-missing string to find. |
replacement |
A single non-missing replacement string. |
A character vector with the same length as x.
Missing input values remain missing.
sas_tranwrd("A-B-C", "-", "_") sas_tranwrd("Hello SAS", "SAS", "R") sas_tranwrd(c("ABC", "ABCABC"), "ABC", "X")sas_tranwrd("A-B-C", "-", "_") sas_tranwrd("Hello SAS", "SAS", "R") sas_tranwrd(c("ABC", "ABCABC"), "ABC", "X")