如何在SAS中的分组数据中找到第一次观察的滞后

时间:2016-08-18 20:56:00

标签: mysql sql sas lag

我有一个数据集,它看起来像这样:

id name    score

1  daniel  30
1  jane    20
1  keisha  70
2  kelly   30
2  jerry   60
2  jay     40

我想找到分数之间的差异,将每组的FIRST分数与该组中的所有其他分数进行比较。例如,

我想将简的分数与丹尼尔(30-20 = 10)进行比较,并将凯莎的分数与丹尼尔的分数进行比较(绝对(30-70)= 40)

重新开始,将杰瑞的得分与凯莉的分数(绝对数(30-60 = 30))和杰伊的分数比作凯利的分数(40-30 = 10) )。

id name score compare

1  daniel  30    .
1  jane    20    10
1  keisha  70    40
2  kelly   30    .
2  jerry   60    30
2  jay     40    10

有人知道在SAS中写这个的方法吗?还是任何SQL命令?

我已尝试过以下

data scoring_prep;
  set scoring_prep;
  by id;
  if not missing(score) then do;
    scorediff = abs(dif(score));
    if id ne lag(id) then scorediff = .;
  end;
run;

但是这只发现了之前记录的延迟,因此,例如,keisha将与jane而不是daniel进行比较。

1 个答案:

答案 0 :(得分:4)

由于您未与之前的值进行比较,因此您不想使用LAG()DIF()功能。而是使用保留变量来传递比较值。

data want;
  set scoring_prep;
  by id;
  retain baseline;
  if first.id then baseline=score;
  else scorediff=abs(baseline - score);
run;