Cyborgx37's answer (VValueX) is definitely the way to go. The reason you weren't able to do this with macro variables is that the macro variable stores the text of the variable name (so in your example, var_names
) not the value of that variable. You couldn't use it during the data step, because a macro variable isn't available during the data step it's created in to be used for compile-time purposes (such as getting the value of a variable named that).
You could go as far as this:
data have;
input var_names $ var_a var_b new_variable ;
datalines;
var_a 7 11 7
var_a 2 9 2
var_b 3 6 6
var_a 6 9 6
;;;;
run;
data want;
set have;
call symput("varname",var_names);
x = symget("varname");
put x=;
run;
But there, x stores "var_a"; you aren't allowed to use "var_a" as a variable name in that context. There isn't really a good macro solution here; you could store all of the values of var_names in a delimited string and parse that, but it would be pretty messy and involves putting data into a macro variable, which violates proper program design principles.