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

Convert all the variable names to uppercase


Lesson Description
-
  • Sometimes, we want to work with the concept of "Convert all the variable names to uppercase" 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.
options validvarname=upcase; 

data class; 
    
input name $ sex $ age height weight; 
    
datalines
Alfred M 14 69 112.5 
Alice F 13 56.5 84 

run;
  • `options validvarname=upcase;` ensures that all variable names are converted to uppercase automatically.
  • This is useful during data import or manual entry to maintain consistency with naming conventions.
  • In this example, variables `name`, `sex`, `age`, `height`, and `weight` are all automatically stored in uppercase in the dataset.
library(tidyverse)

class <- tribble(
~name, ~sex, ~age, ~height, ~weight,
"Alfred", "M", 14, 69, 112.5,
"Alice", "F", 13, 56.5, 84
)

 
#Convert column names to uppercase

class_upper <- class %>%
rename_with(toupper)
  • This example shows how to rename all column names in R using `rename_with()` and `toupper()`.
  • It aligns with the behavior of `validvarname=upcase` in SAS where variable names are automatically capitalized.
  • The resulting dataset has all variable names in uppercase format.
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
)

class_upper <- class
names(class_upper) <- toupper(names(class_upper))
  • toupper() converts names to uppercase.
  • Assign back to names() to rename all columns.