Get column value from column name

There are times when we need to use the attributes like the column value from column name or format of a variable within a data set.  Normally, this can be achieved with a simple CONTENTS procedure but there are functions in SAS that makes 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 without having to write 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 that is associated with the variable that you specify.

data new;
set one;
vvalue=vvalue(val);
a1=val;
run;

Get column value from column name

VVALUE and an assignment statement both return the current value of the variable that you specify. However, With VVALUE, the value is formatted using the current format that is 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 that contains the current value of the argument that you specify. The value is formatted by using the format that is currently associated with the argument.

Get column value from column name

The VValuex function can be used to get the column value from 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.);
cards;
Marks2 1 2 3 
Marks1 4 5 6 
Marks1 7 8 9 
Marks3 10 11 12 
;
run;
data want;
set have;
value=vvaluex(marks);
run;

column value from column name

 

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

JOIN OUR COMMUNITY OF SAS Programmers!

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.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap