SAS 中的动态变量名 [英] Dynamic variable names in SAS

查看:31
本文介绍了SAS 中的动态变量名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我想添加一个包含 Var*Index* 值的列.即我想要的输出是:

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

我无法使用 call symput 来让它工作.另外我应该提一下,我的真正问题稍微复杂一些,我已经有一个凌乱的蛮力方法,但我正在寻找一些干净的方法.

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.

推荐答案

如果你创建一个 Var1 - VarN 的数组,那么你可以很容易地引用存储在 Index 中的值.

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;

这篇关于SAS 中的动态变量名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆