30-Day Course
Clinical SAS → R
Day 1 of 30·R Basics & SAS Equivalents

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

TaskSAS®R
Numeric variableNumeric (8 bytes, always double)numeric / double, or integer (1L)
Character variableCharacter (fixed length, $20.)character (variable length, no padding)
Boolean flagNumeric 0/1 or '\Y'/'\N'logical (TRUE / FALSE)
Categorical / codedNumeric + format, or characterfactor (levels + labels)
Missing value. (numeric) / ' ' (character)NA (typed: NA_real_, NA_character_)
A table of dataSAS dataset (.sas7bdat)data.frame / tibble
Run a procedurePROC + licensed modulefunction from an installed package

Code examples

Assigning values and checking type
SAS®
data _null_;
  age = 47;
  name = "ADAE";
  flag = "Y";
  put age= name= flag=;
run;
R
age  <- 47           # numeric (double)
name <- "ADAE"       # character
flag <- TRUE         # logical

class(age)           # "numeric"
class(name)          # "character"
str(flag)            #  logi TRUE

Note: In R the assignment arrow <- is idiomatic. Use class() or str() the way you'd glance at a variable in PROC CONTENTS.

Factors — R's answer to formatted categorical variables
SAS®
proc format;
  value sexf 1='Male' 2='Female';
run;
data dm;
  sex = 1; format sex sexf.;
run;
R
sex <- factor(
  c(1, 2, 1),
  levels = c(1, 2),
  labels = c("Male", "Female")
)
levels(sex)   # "Male"  "Female"
table(sex)    # frequency counts

Note: A factor stores the underlying code AND the display label together — much like a numeric variable with a PROC FORMAT applied.

Installing and loading a package (one-time vs. per-session)
SAS®
/* Modules are licensed and pre-installed;
   you simply call the PROC */
proc means data=dm; run;
R
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.

Practice exercise

Create three objects: a numeric subject age, a character treatment arm, and a factor for sex with levels 1='Male' and 2='Female'. Use str() and class() to confirm each type. Then install and load the tidyverse package and run sessionInfo() to see what was attached.