BookmarkSubscribeRSS Feed
PHK
Calcite | Level 5 PHK
Calcite | Level 5

Hi,

I am looking for a straightforward / easy way to do the following:

Say I have the table:

data INPUT_DATASET;

  input a $1. b c;

  datalines;

b 10 20

c 30 40

b 50 60

c 70 80

  ;

run;

The first variable (i.e. a) is the name of the column/variables of which I want to use the value in an equation.  Say my equation is 10*X, where X is either the value of b or c, depending on what column a says it should be.  (I don't want to use IF statements, for I work from the assumption that I don't know the variables that column a can refer to.  I need it more dynamic than that.)

What I have tried, which obviously did not work but would illustrate what I want, is the following:

data TEST_OUTPUT;

    set INPUT_DATASET;

    call execute('%let current_var = ' || a );

    TEST=10*&current_var;

run;

The warning is:

WARNING: Apparent symbolic reference CURRENT_VAR not resolved

So, my question is: is there an easy straightforward way to do what I want to do, or should I work around it (such as first determining the unknown variables (b and c in this case) and then using a macro with a loop over the determined list of variables)?

Thanks,

Pieter

1 REPLY 1
PHK
Calcite | Level 5 PHK
Calcite | Level 5

A colleague of mine suggested using the VNAME function, so I do have a solution now:

data TEST;

  set INPUT_DATASET;

  array numeric_vars{*} _NUMERIC_;

  do i = 1 to dim(numeric_vars);

    if vname(numeric_vars{i}) = a then do;

      TEMP_VALUE = numeric_vars{i};

   leave;

end;

  end;

  TEST = TEMP_VALUE*10;

  drop i TEMP_VALUE;

run;

Is there perhaps an even more straight forward way than looping through the variables?

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 969 views
  • 1 like
  • 1 in conversation