Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic variable names in SAS

Is there a way in SAS to specify dynamic variable names in code? i.e. use different variables on different observations based on the value of another variable?

For example, my input data set could be:

Index  Var1   Var2  Var3
1      78.3   54.7  79.8
3      67.2   56.2  12.3
2      65.3   45.2  98.1
1      56.2   49.7  11.3
1      67.2   98.2  98.6

And I want to add a column that holds the value of Var*Index*. i.e. the output I'd want would be:

    Index  Var1   Var2  Var3  Var_Index
    1      78.3   54.7  79.8  78.3
    3      67.2   56.2  12.3  12.3
    2      65.3   45.2  98.1  45.2
    1      56.2   49.7  11.3  56.2
    1      67.2   98.2  98.6  67.2

I'm unable to use call symput to get this to work. Also I should mention that my real problem is slightly more complicated, and I already have a messy brute force method but I'm looking for something clean.

like image 602
user997943 Avatar asked Dec 19 '25 05:12

user997943


1 Answers

If you create an array of Var1 - VarN then you can easily reference the value stored in Index.

data have;
input Index  Var1   Var2  Var3;
cards;
1      78.3   54.7  79.8
3      67.2   56.2  12.3
2      65.3   45.2  98.1
1      56.2   49.7  11.3
1      67.2   98.2  98.6
;
run;

data want;
set have;
array vars(*) var: ;
var_index=vars(index);
run;
like image 146
Longfish Avatar answered Dec 22 '25 00:12

Longfish



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!