BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
blert
Calcite | Level 5

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,

1 ACCEPTED SOLUTION
7 REPLIES 7
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Reeza
Super User

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

VVALUEX()

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

blert
Calcite | Level 5

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,

Tom
Super User Tom
Super User

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;

Tom
Super User Tom
Super User

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.

blert
Calcite | Level 5

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,

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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