如何在SAS中添加具有指定条件的两列?

时间:2016-04-13 19:23:01

标签: sas

如何在SAS中添加具有指定条件的两列?

这里我想添加前2列中指定的列数据。

    data data1;
      set data;
      column1 = x;
      column2 = y;
      total = column1 + column2;
    run;

    input
    x   y   a   b   c   d   e
    --  --  --  --  --  --  --
    a   c   1   45  32  7   45
    b   a   22  45  55  33  55
    d   e   56  78  66  44  12
    c   d   33  45  44  56  77

    Output
    x   y   a   b   c   d   e   Output
    --  --  --  --  --  --  --  ------
    a   c   1   45  32  7   45  33
    b   a   22  45  55  33  55  67
    d   e   56  78  66  44  12  56
    c   d   33  45  44  56  77  100

2 个答案:

答案 0 :(得分:1)

这是一个更简单的解决方案。创建一个可添加的数字数组,循环显示它们,并使用x函数检查变量名称是否与yvname中的值相同。

data have;
input x $ y $  a   b   c   d   e;
datalines;
a   c   1   45  32  7   45
b   a   22  45  55  33  55
d   e   56  78  66  44  12
c   d   33  45  44  56  77
;
run;

data want;
set have;
array vars{*} a--e;
output=0;
do i = 1 to dim(vars);
    if x=vname(vars{i}) then output+vars{i};
    if y=vname(vars{i}) then output+vars{i};
end;
drop i;
run;

答案 1 :(得分:-1)

我确信有更简洁的方法可以做到这一点,但这有效:

{{1}}