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

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

问题描述

在SAS中有没有办法在代码中指定动态变量名?
即根据另一个变量的值来使用不同的变量?



例如,我的输入数据集可以是:

 索引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 *的值。
即我想要的输出是:

 索引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
/ pre>

我无法使用调用symput 来使其正常工作。另外我也应该提到我真正的问题稍微复杂一些,我已经有一个凌乱的强力方法,但是我正在寻找一些干净的东西。

解决方案

如果您创建一个Var1 - VarN数组,那么您可以轻松地引用存储在索引中的值。

 数据有
输入索引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
;
运行;

数据需要;
set have;
数组vars(*)var:;
var_index = vars(index);
运行;


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.

解决方案

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天全站免登陆