Announcement Icon Online training class for Clinical R programming batch starts on Monday, 02Feb2026. Click here for details.

Reading Files Using Path References


Lesson Description
-
  • Sometimes, we want to work with the concept of "Reading Files Using Path References" in a clear, repeatable way.
  • This lesson walks through a simple example and shows the key steps.
  • We will see one approach on how we can do it in SAS and R.

 


libname mydata "path"

data class; 
    
set mydata.class; 
run;

 

  • In SAS, `libname` assigns a shortcut (library) to a folder of datasets.
  • You can then refer to datasets in that folder using the library name, e.g., `mydata.class`.
  • This enables reading multiple datasets from a folder without repeating the full path.
# Direct path reading library(haven)

class <- read_sas("path/class.sas7bdat") 

# Using file.path for portability 

folder_path <- "D:/SAS/Home/dev/" 

class <- read_sas(file.path(folder_path, "class.sas7bdat")) 

# Using glue for portability

library(glue) 

folder_path <- "D:/SAS/Home/dev/" 

class <- read_sas(glue("{folder_path}/class.sas7bdat"))
  • R does not use `libname` or folder shortcuts like SAS.
  • Datasets are accessed directly by their full or relative file paths using `read_sas()` from the haven package.
  • `file.path()` is used to construct paths in a platform-independent way.
  • Alternatively, `glue()` from the `glue` package allows flexible string interpolation using variables inside paths.
library(haven)

# Direct path reading
class <- read_sas("path/class.sas7bdat")

# Using file.path for portability
folder_path <- "D:/SAS/Home/dev/"
class <- read_sas(file.path(folder_path, "class.sas7bdat"))

# Using glue for portability
library(glue)
folder_path <- "D:/SAS/Home/dev/"
class <- read_sas(glue("{folder_path}/class.sas7bdat"))
  • read_sas() reads a SAS dataset into R.
  • file.path() or glue() builds portable file paths.