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

Create Sample Data


Lesson Description
-

  • Lets assume that we want to create some dummy data inline within a program.
  • We will see how one way how we can create such data in both SAS and R
data CLASS;
  infile datalines dlm='|' dsd missover;
  input Name : $8. Sex : $1. Age : best32. Height : best32. Weight : best32.;
  datalines;
  Alfred|M|14|69|112.5
  Alice|F|13|56.5|84
  Barbara|F|13|65.3|98
;
run;
  • Base SAS datastep reading inline pipe-delimited data with explicit informats.
  • Useful reference for downstream translations.
library(tidyverse)

class <- tribble(
  ~Name, ~Sex, ~Age, ~Height, ~Weight,
  "Alfred", "M", 14, 69, 112.5,
  "Alice",  "F", 13, 56.5, 84,
  "Barbara", "F", 13, 65.3, 98
)
  • Existing tidyverse implementation using tribble for readability.
class <- data.frame(
  Name = c("Alfred", "Alice", "Barbara"),
  Sex = c("M", "F", "F"),
  Age = c(14, 13, 13),
  Height = c(69, 56.5, 65.3),
  Weight = c(112.5, 84, 98)
  , stringsAsFactors = FALSE
)
  • data.frame() builds a table from equal-length vectors, one per column.
  • stringsAsFactors = FALSE keeps character columns as character.
  • The result is assigned to class for later use.