【问题标题】:SAS return first value in the group for the rest of the groupSAS为组的其余部分返回组中的第一个值
【发布时间】:2012-03-25 05:08:31
【问题描述】:

假设我有以下数据,但我基本上想将 a 和 b 的第一个值复制到组中的其余值(底部的表格)。

例如,在第 1 组中,a 中的第一个值 = 3。我想将组中的 2、4、1 替换为 3 -- 变量 b 也是如此。

原始数据:

grp a   b
----------
1   3   2
1   2   1
1   4   2
1   1   3
2   2   4
2   1   1
2   2   2
2   3   1

更新数据:

grp a   b
----------
1   3   2
1   3   2
1   3   2
1   3   2
2   2   4
2   2   4
2   2   4
2   2   4

提前致谢。

【问题讨论】:

  • a 和 b 的第一个值?详细说明

标签: sas data-manipulation


【解决方案1】:

您可以使用按组处理和保留语句来执行此操作。请注意,输入数据集需要先按组排序才能正常工作。

data output(keep=grp a b);
  retain firsta firstb;
  set input;
  by grp;
  if first.grp then do;
    firsta = a;
    firstb = b;
  end;
  else do;
    a = firsta;
    b = firstb;
  end;
run;

【讨论】:

  • 谢谢!你是救生员。
猜你喜欢
  • 1970-01-01
  • 2022-01-05
  • 1970-01-01
  • 1970-01-01
  • 2021-06-24
  • 2020-10-04
  • 1970-01-01
  • 2020-05-04
  • 1970-01-01
相关资源
最近更新 更多