07-21-2015 11:06 AM
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*/
str_to_varname("VAR1")=4; /*definition of VAR1 variable*/
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.
07-21-2015 11:29 AM
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.
07-21-2015 12:07 PM
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.
07-21-2015 12:37 PM
Left side assignments are what arrays are for.
array out var1 var2 var3 ;
do i=1 to dim(out);
if target = vname(out(i)) then out(i)=1 ;
07-21-2015 12:43 PM
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.
07-21-2015 01:11 PM
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.
Need further help from the community? Please ask a new question.