stringSAS

stringSAS logo

SAS-Inspired Character Functions for R

stringSAS provides familiar SAS character manipulation functions for R users, particularly SAS programmers transitioning to R and tidyverse workflows.

The package focuses on SAS-inspired syntax, vectorized operations, and straightforward integration with dplyr.

Motivation

In pharmaceutical statistical programming, SAS and R are frequently used side by side. Switching between these languages can be inconvenient because similar character manipulation tasks require different function names and argument conventions.

stringSAS was developed to reduce this cognitive overhead by providing familiar SAS-inspired character functions in R.

The goal is not to replace existing R string manipulation packages, but to provide a convenient interface for programmers who regularly work with both languages.

Installation

Install the development version from GitHub:

# install.packages("remotes")
remotes::install_github("Statsergeant/stringSAS")

After the package is published on CRAN:

install.packages("stringSAS")

Available Functions

Function Description
sas_compress() Remove or retain selected characters
sas_scan() Extract words by position
sas_substr() Extract substrings by position and length
sas_catx() Concatenate strings with a delimiter
sas_find() Find a substring’s position
sas_tranwrd() Replace substrings

Quick Examples

library(stringSAS)

sas_compress("AB-123", "-")
## [1] "AB123"
sas_scan("STUDY-001-002", 2, "-")
## [1] "001"
sas_substr("STUDY001", 1, 5)
## [1] "STUDY"
sas_catx("-", "STUDY", "001", "002")
## [1] "STUDY-001-002"
sas_find("Hello World", "World")
## [1] 7
sas_tranwrd("A-B-C", "-", "_")
## [1] "A_B_C"

Using stringSAS with dplyr

stringSAS functions are vectorized and can be used directly inside mutate().

library(dplyr)
dm <- tibble(
  usubjid = c(
    "ABC-001-0001",
    "ABC-002-0002",
    "XYZ-003-0003"
  )
)

dm %>%
  mutate(
    study = sas_scan(usubjid, 1, "-"),
    site = sas_scan(usubjid, 2, "-"),
    subject = sas_scan(usubjid, 3, "-"),
    clean_id = sas_compress(usubjid, "-")
  )
## # A tibble: 3 × 5
##   usubjid      study site  subject clean_id  
##   <chr>        <chr> <chr> <chr>   <chr>     
## 1 ABC-001-0001 ABC   001   0001    ABC0010001
## 2 ABC-002-0002 ABC   002   0002    ABC0020002
## 3 XYZ-003-0003 XYZ   003   0003    XYZ0030003

Design Philosophy

stringSAS is designed to make SAS character manipulation familiar in R.

The package:

Unlike SAS program translation tools, stringSAS focuses specifically on character manipulation.

Contributing

Contributions, bug reports, and feature requests are welcome through GitHub Issues.

License

MIT License

Author

Geonhyeok Jeong