DATA Step, Macro, Functions and more

String To Variable Name

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

String To Variable Name

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,


Accepted Solutions
Solution
‎07-21-2015 11:38 AM
Super User
Super User
Posts: 7,039

Re: String To Variable Name


All Replies
Super User
Super User
Posts: 7,942

Re: String To Variable Name

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.

Solution
‎07-21-2015 11:38 AM
Super User
Super User
Posts: 7,039

Re: String To Variable Name

Super User
Posts: 19,772

Re: String To Variable Name

SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition

VVALUEX()

Returns the formatted value that is associated with the variable that you specify.

Occasional Contributor
Posts: 5

Re: String To Variable Name

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,

Super User
Super User
Posts: 7,039

Re: String To Variable Name

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;

Super User
Super User
Posts: 7,039

Re: String To Variable Name

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.

Occasional Contributor
Posts: 5

Re: String To Variable Name

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,

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 1834 views
  • 8 likes
  • 4 in conversation