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*¤t_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
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?
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.
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.