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;
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.
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;