Get column value from column name

Sometimes we need to use the attributes like the column value from the column name or the format of a variable within a data set. 

Normally, this can be achieved with a simple CONTENTS procedure, but SAS functions make programming more efficient.

Some of the functions are the variable information functions. These functions allow the programmer to use the information about the variable within the data step without having to hard code the information or writing a secondary step to fetch data from PROC CONTENTS.

The Vvalue and Vvaluex functions can be used to return the format and/or value of a variable or a string within the data step.

VValue function

The VValue returns the formatted value associated with the variable you specify.

data new;
set one;
Get column value from column name

VVALUE and an assignment statement return the current value of the variable you specify. However, With VVALUE, the value is formatted using the current format associated with the variable. With an assignment statement, however, the value returned is unformatted.

column value from column name

VValuex Function

VVALUEX evaluates the argument to determine the variable name and then returns the value that is associated with that variable name.

VVALUEX returns a character string containing the current value of the argument you specify. The value is formatted using the format currently associated with the argument.

Get column value from the column name.

The VValuex function can get the column value from the column name. Consider the below example where we want to get the value of the respective column specified in the Marks Column for each row.

data have;
input (Marks Marks1 Marks2 Marks3) (:$8.);
Marks2 1 2 3 
Marks1 4 5 6 
Marks1 7 8 9 
Marks3 10 11 12 

data want;
set have;
column value from column name

Every week we'll send you SAS tips and in-depth tutorials


Subhro Kar is an Analyst with over five years of experience. As a programmer specializing in SAS (Statistical Analysis System), Subhro also offers tutorials and guides on how to approach the coding language. His website, 9to5sas, offers students and new programmers useful easy-to-grasp resources to help them understand the fundamentals of SAS. Through this website, he shares his passion for programming while giving back to up-and-coming programmers in the field. Subhro’s mission is to offer quality tips, tricks, and lessons that give SAS beginners the skills they need to succeed.