通过在SQL中处理SAS,添加计数器

通过在SQL中处理SAS,添加计数器,sql,sas,Sql,Sas,我经常发现自己在SAS中添加了基于分组处理的计数器类型变量。但是,我也需要能够在SQL中实现这一点。有什么想法吗 data one; input col1 col2; datalines; 101 1 101 2 101 2 ; run; data two; retain counter; set one; by col1 col2; if first.col2 then counter = 0; counter + 1; if last.col2 then out

我经常发现自己在SAS中添加了基于分组处理的计数器类型变量。但是,我也需要能够在SQL中实现这一点。有什么想法吗

data one;
  input col1 col2;
datalines;
101 1
101 2
101 2
;
run;

data two;
  retain counter;
  set one;
  by col1 col2;
  if first.col2 then counter = 0;
  counter + 1;
  if last.col2 then output;
run;
因此,表2如下所示:

COL1 COL2 COUNTER
101  1    1
101  2    2

当您只想按组计数时,只需使用计数:

proc sql;
  create table two as
    select Count(*) as counter,col1,col2 from one group by col1,col2;
quit;

注意:计数器是第一个war,因为它也是等效数据步骤中的第一个。另请注意,不需要排序,因为排序是隐式的。

当您只想按组计数时,只需使用计数:

proc sql;
  create table two as
    select Count(*) as counter,col1,col2 from one group by col1,col2;
quit;
注意:计数器是第一个war,因为它也是等效数据步骤中的第一个。另请注意,由于排序是隐式进行的,因此不需要排序。

您用于计算组中记录数的数据步骤可以使用DOW循环重新编码

data two(keep=col1 col2 counter);
  do counter = 1 by 1 until (last.col2);
    set one;
    by col1 col2;
  end;
run;
SQL结果集没有隐式顺序,除非有ORDERBY子句。SQL不像DATA step那样具有串行处理的概念,在DATA step中,可以在组内分配单调递增的序列号

当每个记录都有足够的卫星数据来无条件地将其自身定位在结果集中时,SQL联接可以基于卫星变量的“三角”比较计数来计算序列号。与数据步骤中的+1方法相比,这是一个昂贵的提议

例如:

data have;
  call streaminit(1234);
  do col1 = 'Z', 'X', 'A';
    do col2 = 1 to 4 + rand('uniform', 10);
      do _n_ = 1 to 4 + rand('uniform', 10);
        col3 = ceil(rand('uniform', 1e7));  /* presume col3 is distinct within group and will order the rows within the group (col1,col2) */
        output;
      end;
    end;
  end;
run;

proc sql;
  create table want
  as select one.col1, one.col2, one.col3, count(*) as seq
  from have as one
  join have as two
    on one.col1=two.col1 and one.col2 = two.col2   /* group */
  where two.col3 <= one.col3                       /* col3 used for triangle criteria */
  group by one.col1, one.col2, one.col3
  order by one.col1, one.col2, seq
;
quit;
用于计算组中记录数的数据步骤可以使用DOW循环重新编码

data two(keep=col1 col2 counter);
  do counter = 1 by 1 until (last.col2);
    set one;
    by col1 col2;
  end;
run;
SQL结果集没有隐式顺序,除非有ORDERBY子句。SQL不像DATA step那样具有串行处理的概念,在DATA step中,可以在组内分配单调递增的序列号

当每个记录都有足够的卫星数据来无条件地将其自身定位在结果集中时,SQL联接可以基于卫星变量的“三角”比较计数来计算序列号。与数据步骤中的+1方法相比,这是一个昂贵的提议

例如:

data have;
  call streaminit(1234);
  do col1 = 'Z', 'X', 'A';
    do col2 = 1 to 4 + rand('uniform', 10);
      do _n_ = 1 to 4 + rand('uniform', 10);
        col3 = ceil(rand('uniform', 1e7));  /* presume col3 is distinct within group and will order the rows within the group (col1,col2) */
        output;
      end;
    end;
  end;
run;

proc sql;
  create table want
  as select one.col1, one.col2, one.col3, count(*) as seq
  from have as one
  join have as two
    on one.col1=two.col1 and one.col2 = two.col2   /* group */
  where two.col3 <= one.col3                       /* col3 used for triangle criteria */
  group by one.col1, one.col2, one.col3
  order by one.col1, one.col2, seq
;
quit;

您是否需要它在纯SAS代码中工作,或者您正在连接到某个远程数据库,在那里您可以使用显式passthru来使用该数据库的SQL实现进行编码?如果是后者,请查看窗口功能。大多数将有一个行数函数或等效函数。我实际上需要将其转换为红移SQL代码。我可以通过SAS处理所有组,但添加计数器位到目前为止我一直不知道。感谢您是否需要它在纯SAS代码中工作,或者您是否连接到某个远程数据库,在那里您可以使用显式passthru来使用该数据库的SQL实现进行编码?如果是后者,请查看窗口功能。大多数将有一个行数函数或等效函数。我实际上需要将其转换为红移SQL代码。我可以通过SAS处理所有组,但添加计数器位到目前为止我一直不知道。谢谢