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

Renaming variables


Lesson Description
-
  • Sometimes, we want to work with the concept of "Renaming variables" 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.
data CLASS;
infile datalines dlm='|' dsd missover;
input Name : $8. Sex : $1. Age : best32. Height : best32. Weight : best32.;
label ;
format ;
datalines4;
Alfred|M|14|69|112.5
Alice|F|13|56.5|84
;;;;
run;

data renamed;
    set class;
    rename age=age_years height=height_in;
run;
  • The provided SAS code creates a new dataset named "renamed" by copying the observations from the existing dataset "class" and renaming two variables.
  • The set statement is used to read data from the "class" dataset into the "renamed" dataset.
  • The rename statement is used to specify the renaming of variables. In this case, the variable "age" is renamed to "age_years" and the variable "height" is renamed to "height_in."
  • The run; statement marks the end of the data step and executes the creation of the "renamed" dataset.
  • This SAS code snippet demonstrates how to create a new dataset named "renamed" that is identical to the "class" dataset, but with renamed variables. By using the rename statement, the variables "age" and "height" are given new names "age_years" and "height_in," respectively. This allows for better clarity and consistency in variable naming conventions.
class<-tribble(
~Name,~Sex,~Age,~Height,~Weight,
"Alfred","M",14,69,112.5,
"Alice","F",13,56.5,84,
)

 

renamed<-rename(class,Age_years=Age, Height_in=Height)
  • The provided R Tidyverse code snippet creates a new data frame named "renamed" by renaming variables in the existing data frame "class."
  • The rename function is used to specify the renaming of variables. In this case, the variable "Age" is renamed to "Age_years" and the variable "Height" is renamed to "Height_in."
  • The rename function takes two arguments: the first argument is the data frame, and the subsequent arguments are the old variable names followed by the new variable names.
  • By using the rename function, the variables in the "class" data frame are renamed, and the resulting data frame is assigned to the "renamed" object.
  • This R Tidyverse code snippet demonstrates how to create a new data frame named "renamed" that is identical to the "class" data frame, but with renamed variables. By specifying the old variable names and their corresponding new names, you can modify the variable names to improve clarity and consistency in your data analysis.
class <- data.frame(
  Name = c("Alfred", "Alice"),
  Sex = c("M", "F"),
  Age = c(14, 13),
  Height = c(69, 56.5),
  Weight = c(112.5, 84)
  , stringsAsFactors = FALSE
)

renamed <- class

names(renamed)[names(renamed) == 'Age'] <- 'Age_years'

names(renamed)[names(renamed) == 'Height'] <- 'Height_in'
  • names(df) returns column names; indexing lets you replace a specific name.
  • Assigning to names() updates column names in place.