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?

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 1 reply
  • 634 views
  • 1 like
  • 1 in conversation