Hi all,
There is a question that maybe someone could help us with.
Is it possible to convert a string to a SAS variable name at datastep execution time? We mean, for example, during execution of datastep being able of doing something like this:
VAR2=str_to_varname("VAR1"); /* using the value of existing VAR1 variable to define other variable*/
or
str_to_varname("VAR1")=4; /*definition of VAR1 variable*/
or
if str_to_varname("VAR1")=5 then VAR3=7; /* using values of existing VAR1 variable in logical expressions*/
It is to say, to dynamically at execution time and without using macro language or arrays, being able to treat a string as a datastep variable name.
Thank you,
Did you try using VVALUEX() function?
http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a002233818.htm
I don't think you can do it directly as part of the datastep execution, as that part is not dynamic. Macro, call execute, are your best bet, these generate the code before going onto the datastep compilation. What are you trying to do, there may be other ways of getting what you want.
Did you try using VVALUEX() function?
http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a002233818.htm
SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition
VVALUEX()
Returns the formatted value that is associated with the variable that you specify.
Thanks a lot all of you. We had not considered the EXECUTE way. Nice to hear from VVALUEX() function. It is what we were looking for. The only thing to mention would be that it would not allow left side assigments like: VVALUEX("VAR1")=VAR2;
Thanks again for your responses, VVALUEX() will be very useful for us.
Regards,
Left side assignments are what arrays are for.
array out var1 var2 var3 ;
target='VAR2';
do i=1 to dim(out);
if target = vname(out(i)) then out(i)=1 ;
end;
If you explain the problem there is probably another way to do what you want that does not need dynamically referenced variables.
For example if you have source data in NAME, VALUE pairs then you can use PROC TRANSPOSE to convert the data into variable using the value of NAME as the name of the variable and VALUE as the value for the variable.
Although we were wondering if there was a function approach to left side assigment, if not, arrays (or PROC TRANSPOSE) is a good approach for us.
Thanks,
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.