rleid函数的SAS等价

rleid函数的SAS等价,sas,grouping,Sas,Grouping,R包data.table有一个函数rleid,它为向量的每个元素分配一个整数,相邻的相同值元素得到相同的整数。希望R中的这个例子解释了rleid的作用,这里y是rleid(x) 只需将BY语句与NOTSORTED选项一起使用即可 data want ; set have ; by x notsorted ; y + first.x ; run; 您可以在此处使用“按组处理”来添加计数器。组中有一个名为NoReCube的选项,允许您考虑逻辑顺序之外的组。 data want;

R包
data.table
有一个函数
rleid
,它为向量的每个元素分配一个整数,相邻的相同值元素得到相同的整数。希望R中的这个例子解释了
rleid
的作用,这里
y
rleid(x)


只需将
BY
语句与
NOTSORTED
选项一起使用即可

data want ;
  set have ;
  by x notsorted ;
  y + first.x ;
run;

您可以在此处使用“按组处理”来添加计数器。组中有一个名为NoReCube的选项,允许您考虑逻辑顺序之外的组。

data want;
    set ex;
    by x notsorted;
    if first.x then count+1;
run;

还有一种方法可以做到这一点

  data b;
  set ex;
  if lag(x)=x then count+0;
  else count+1;
  run;
data want;
    set ex;
    by x notsorted;
    if first.x then count+1;
run;
  data b;
  set ex;
  if lag(x)=x then count+0;
  else count+1;
  run;