Your First Steps in R: Data Types & the SAS Mindset Shift
How R thinks about data compared to SAS, and the vocabulary you need before writing a single transformation.
Learning objectives
- 1Understand the core R data types (numeric, integer, character, logical, factor) and how they map to SAS variable types.
- 2Recognize the difference between a SAS dataset and an R data.frame / tibble.
- 3Assign objects, inspect their structure, and read basic R syntax confidently.
- 4Install and load packages — the open-source equivalent of licensed SAS modules.
Key SAS® → R comparison
| Task | SAS® | R |
|---|---|---|
| Numeric variable | Numeric (8 bytes, always double) | numeric / double, or integer (1L) |
| Character variable | Character (fixed length, $20.) | character (variable length, no padding) |
| Boolean flag | Numeric 0/1 or '\Y'/'\N' | logical (TRUE / FALSE) |
| Categorical / coded | Numeric + format, or character | factor (levels + labels) |
| Missing value | . (numeric) / ' ' (character) | NA (typed: NA_real_, NA_character_) |
| A table of data | SAS dataset (.sas7bdat) | data.frame / tibble |
| Run a procedure | PROC + licensed module | function from an installed package |
Code examples
data _null_;
age = 47;
name = "ADAE";
flag = "Y";
put age= name= flag=;
run;age <- 47 # numeric (double)
name <- "ADAE" # character
flag <- TRUE # logical
class(age) # "numeric"
class(name) # "character"
str(flag) # logi TRUENote: In R the assignment arrow <- is idiomatic. Use class() or str() the way you'd glance at a variable in PROC CONTENTS.
proc format;
value sexf 1='Male' 2='Female';
run;
data dm;
sex = 1; format sex sexf.;
run;sex <- factor(
c(1, 2, 1),
levels = c(1, 2),
labels = c("Male", "Female")
)
levels(sex) # "Male" "Female"
table(sex) # frequency countsNote: A factor stores the underlying code AND the display label together — much like a numeric variable with a PROC FORMAT applied.
/* Modules are licensed and pre-installed;
you simply call the PROC */
proc means data=dm; run;install.packages("dplyr") # once, like adding a module
library(dplyr) # every session, like options
# tidyverse bundles many packages at once:
install.packages("tidyverse")
library(tidyverse)Note: install.packages() downloads from CRAN (free, public). library() attaches it for the current session.
Explanation
The single biggest shift moving from SAS to R is that R is object-oriented and interactive. In SAS you describe a dataset and a procedure consumes it; in R you create named objects — vectors, data.frames, lists — that live in memory and that you manipulate directly. There is no implicit PDV (Program Data Vector) looping over rows behind the scenes. Operations are vectorized: when you write df$age + 1, R adds one to every element at once.
R distinguishes types more strictly than SAS. SAS has only two storage types (numeric and character); R separates numeric, integer, logical, character, and the special factor type for categorical data. Getting comfortable with these — and with the typed nature of NA missing values — prevents most beginner surprises.
Finally, R's power comes from packages. Where SAS ships licensed modules (SAS/STAT, SAS/GRAPH), R draws on CRAN, a public repository of over 20,000 free packages. The tidyverse collection (dplyr, tidyr, ggplot2, purrr, readr, haven) is the backbone of modern clinical R programming and will appear throughout this course.