【问题标题】:sas change last value by group to first valuesas按组将最后一个值更改为第一个值
【发布时间】:2020-02-11 11:42:01
【问题描述】:

我想更改表单的数据

id   value
1     1
1     1 
1     2  
2     7
2     7
2     7 
2     5
.     .
.     .
.     .

id   value
1     1
1     1 
1     1  
2     7
2     7
2     7 
2     7
.     .
.     .
.     .

即分组的最后一个值应该是分组的第一个值。我试过下面的代码

data want;
set have;
by id;
last.value=first.value;
run;

但这没有用。有人可以帮帮我吗?

【问题讨论】:

    标签: sas


    【解决方案1】:

    您应该将 first.id 值保存在变量中,并将其保存在 retain 中。

    data want(drop=tValue);
       set have;
       by id;
       retain tValue;
       if first.id then tValue=value;
       if last.id then value=tValue;
    run;
    

    【讨论】:

      【解决方案2】:

      这里的问题是first.valuelast.value

      • 不要保留实际值,它们只是告诉您观察是 BY 组中的第一个还是最后一个
      • 无法分配 - last.value = 语法无效

      其次,first.valuelast.value 只有在 by 语句中声明了 value 变量时才被设置。您应该改用first.idlast.id

      这里我们需要做的是:

      1. 检查我们是否正在查看基于id 的 BY 组中的第一个观察值
      2. 保持value 变量的值,直到达到最后一个id
      3. 当我们查看最后一个 id 值时,请设置第 1 步中的值。

      Alexey 的回答涵盖了执行此操作所需的实际语法。这是first.id/last.id 值的样子。 (您始终可以通过将put _all_; 添加到您的数据步骤中来查看它们):

      id   value  first.id  last.id  tValue
      1     1     1         0        1
      1     1     0         0        1
      1     2     0         1        1
      2     7     1         0        7
      2     7     0         0        7
      2     7     0         0        7
      2     5     0         1        7
      .     .
      .     .
      .     .
      

      【讨论】:

        猜你喜欢
        • 2020-05-04
        • 1970-01-01
        • 1970-01-01
        • 2023-04-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-04
        相关资源
        最近更新 更多