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

Create a copy of an existing dataset


Lesson Description
-
  • Sometimes, we want to create a copy of an existing dataset.
  • We will see one approach on how we can do it in 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;

data new_class;
    set class;
run;
  • The SAS code snippet creates a new dataset named "new_class" by copying the data from an existing dataset called "class."
  • The data statement initializes the creation of the "new_class" dataset.
  • The set statement is used to read and copy the data from the "class" dataset into the "new_class" dataset.
  • It essentially replicates the structure and content of the original dataset.
  • The run; statement marks the end of the data step and executes the creation of the "new_class" dataset.
  • This SAS code snippet demonstrates how to create a new dataset by duplicating an existing dataset.
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,
)


new_class<-class
  • This code snippet demonstrates how to create a new data frame by duplicating an existing one in R.
  • The R Tidyverse code snippet assigns the data frame "class" to a new data frame named "new_class" using the assignment operator (
  • The existing data frame "class" is duplicated, and the resulting duplicate is stored in the new data frame "new_class."
  • This operation creates a separate copy of the data frame, so any changes made to "new_class" will not affect the original "class" data frame.
  • By duplicating the data frame, you can perform different operations on the duplicated version while preserving the original dataset for future use or comparison.
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
)

new_class <- class
  • new_class <- class assigns a new object; R uses copy-on-modify semantics.
  • After a change, new_class and class become independent.