Announcement Icon Online training class for Clinical SAS programming starting soon. Click here for details.

Renaming variables


SAS code

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;

SAS code description

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.

R code


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)

R code description

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.