0

I have a data set with three columns: Names, ColA and ColB. There's a number of rows for each name, and for each name I need to subtract B from A, and make a new column get the remaining value of the differece between A and B:

Names  ColA    ColB   NewColA
  x      100       5      95
  x      100      20      75
  x      100      10      65 

Is this possible? I've tried using IF-Then statements, Do-While and considered a macro but my head is still stuck in Excel/VBA mode so I'm not sure how to do it?

4

1 回答 1

2

您的问题与通过分组进行汇总非常相似。在这里可以找到一个例子:http: //support.sas.com/kb/24/649.html

秘诀是使用“第一个”隐式变量。

data out;
    set [replace with your input dataset]; 
    by names;
    retain newColA;
    if first.names then newcola=cola;
    newcola = newcola - colb;
run;

[编辑] 我忘记了保留语句。这是一个使用来自 sashelp 的 fish 数据集的示例。(虽然在那里这样做没有意义。)

首先,必须对数据集进行排序。如果您的已经是,您可以直接进入数据步骤。

proc sort data = sashelp.fish(where=(Weight ne .) drop=Length1-Length3) out = fish nodupkey force;
    by species weight height;
run;

data out;
    set fish;
    by species;
    retain newColA;
    if first.species then newColA  = weight;
    newColA = newColA - height;
run;
于 2013-09-11T12:42:18.703 回答