使用SAS中的条件创建子表

时间:2014-04-07 11:27:19

标签: sas

我想检查操作符(OP_NAME)是否因SN而改变。如果Operater更改,则输出所有相应的SNOP_NAMEValueTable1:OP_CHANGE其他Table2:OP_UNQ

条件:如果任何1个操作符随SN更改,则将整行输出到表

SN      OP_Name Value
109029  SPAN    150
109029  SPAN    235
109032  SPAN    550
109033  SPAN    650
109033  SPAN    700
109036  FRAN    124
109036  SECURIT 224
109036  SECURIT 560
109036  SECURIT 752
109037  AOM      44
109037  SECA     58
109037  SECURIT  85
109037  SECURIT  98
109038  FRAN     45
109038  SECURIT  47
109038  SECURIT  58
109038  SECURIT  65
109039  GOVER    33
109039  GOVER    45
109039  GOVER    48
109041  SOREL    45
109041  SOREM    55
109041  INA      45
109043  SPAN     96
109044  SPAN     53
109045  SOREM    25
109045  SOREM    65

我希望看到像左派这样的输出表 Table1:OP_CHANGE

SN  OP Name value
109036  FRAN    124
109036  SECURIT 224
109036  SECURIT 560
109036  SECURIT 752
109037  AOM     44
109037  SECA    58
109037  SECURIT 85
109037  SECURIT 98
109038  FRAN    45
109038  SECURIT 47
109038  SECURIT 58
109038  SECURIT 65
109041  SOREM   45
109041  SOREM   55
109041  INA     45

表2:OP_UNQ

SN  OP Name value
109029  SPAN    150
109029  SPAN    235
109032  SPAN    550
109033  SPAN    650
109033  SPAN    700
109039  GOVER   33
109039  GOVER   45
109039  GOVER   48
109043  SPAN    96
109044  SPAN    53
109045  SOREM   25
109045  SOREM   65

2 个答案:

答案 0 :(得分:2)

您可以执行以下操作:

data op_change(drop=change) op_unq(drop=change);
  set have(in=h1) have;
  by sn;

  length change $1;
  retain change;

  if h1 then do;
     if lag(op_name) ne op_name then change = 'Y';
     if first.sn then change = 'N';
  end;
  else do;
     if change = 'Y' then output op_change;
     else output op_unq;
  end;
run;

答案 1 :(得分:1)

这应该有效,假设您的输入表被称为table

proc sql;
  /* Find SN values with different OP_NAMEs */
  create table op_change as
  select t.*
  from table t
  inner join
  (select SN, count(distinct OP_NAME)
  from table
  group by SN
  having count(distinct OP_NAME) > 1) x
  on t.SN = x.SN;

  /* Find SN values with unique OP_NAMEs */
  create table op_unq as
  select t.*
  from table t
  inner join
  (select SN, count(distinct OP_NAME)
  from table
  group by SN
  having count(distinct OP_NAME) = 1) x
  on t.SN = x.SN;
quit;