DATA Step, Macro, Functions and more

Table cell indicating which column's value to use in equation

Reply
Occasional Contributor PHK
Occasional Contributor
Posts: 11

Table cell indicating which column's value to use in equation

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

Occasional Contributor PHK
Occasional Contributor
Posts: 11

Re: Table cell indicating which column's value to use in equation

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?

Ask a Question
Discussion stats
  • 1 reply
  • 130 views
  • 1 like
  • 1 in conversation