将SAS中的数据分组到特定存储桶中

将SAS中的数据分组到特定存储桶中,sas,proc-sql,Sas,Proc Sql,我需要一些帮助来满足以下要求 Currentdataset(数据集名称:SAS1): 因此,基本上,在上述数据集中,如果status2=1和status1='x'以及product_type3,那么对于这两行,status1应该是'nr'。如果status2=1且status1='x'和product_type=3,则对于这两行,status1应为'x+1'。如果status2=0和status1='x+1',则对于这两行,status1应为'x+1' 所需输出(数据集名称:SAS2): 代码已

我需要一些帮助来满足以下要求

Currentdataset(数据集名称:SAS1):

因此,基本上,在上述数据集中,如果
status2=1和status1='x'以及product_type3
,那么对于这两行,
status1
应该是
'nr'
。如果
status2=1且status1='x'和product_type=3
,则对于这两行,
status1
应为
'x+1'
。如果
status2=0和status1='x+1'
,则对于这两行,
status1
应为
'x+1'

所需输出(数据集名称:SAS2):

代码已尝试,但不起作用:

proc sql;create table sas2 as 
select 
    a.*,
    case
    when status2=0 and status1='x+1' then 'x+1'
    WHEN status2=1 and  status1='x' and product_type=3 then 'nr'
    WHEN status2=1 and  status1='x'  and product_type ne 3 then 'x+1'
    WHEN status2=1 and  status1='NotActive' then 4
END AS status3 FROM sas1 AS a;quit;

上面的代码不起作用。因此,例如,对于乘积_no=12345,只满足该特定行的条件,而不满足整个组的条件。因此,对于product_no=12345,应该为两行填充列status1='nr',而不仅仅是一行。

似乎需要进行一些分组,以便将计算出的值应用于“两”行。从样本数据来看,只有两行组基于
产品编号
。对组进行逻辑计算的总和将告诉您组中的任何行是否符合条件<在指定
group by
子句的同时,当进行非聚合选择时,code>Proc SQL查询也将进行自动重新合并。case语句将根据case语句的第一个when条件计算
status1

例如:

data have;input
product_no product_type status1 $ status2 ; datalines;
12345      3            x       0
12345      1            x       1
123456     3            x       1
123456     6            x       0
9876       3            x+1     0
9876       1            x+1     0
run;

proc sql;
  create table want as
  select 
    product_no
  , product_type
  , case 
      when sum(status2=1 and status1='x' and product_type ne 3) > 0 then 'nr'
      when sum(status2=1 and status1='x' and product_type eq 3) > 0 then 'x+1'
      when sum(status2=0 and status1='x+1') > 0 then 'x+1'
      else status1
    end as status1
  , status2
  from have
  group by product_no
  ;

请更新您的问题以包含您迄今为止尝试过的代码。@user667489我已将尝试过的代码添加到原始帖子中
proc sql;create table sas2 as 
select 
    a.*,
    case
    when status2=0 and status1='x+1' then 'x+1'
    WHEN status2=1 and  status1='x' and product_type=3 then 'nr'
    WHEN status2=1 and  status1='x'  and product_type ne 3 then 'x+1'
    WHEN status2=1 and  status1='NotActive' then 4
END AS status3 FROM sas1 AS a;quit;
data have;input
product_no product_type status1 $ status2 ; datalines;
12345      3            x       0
12345      1            x       1
123456     3            x       1
123456     6            x       0
9876       3            x+1     0
9876       1            x+1     0
run;

proc sql;
  create table want as
  select 
    product_no
  , product_type
  , case 
      when sum(status2=1 and status1='x' and product_type ne 3) > 0 then 'nr'
      when sum(status2=1 and status1='x' and product_type eq 3) > 0 then 'x+1'
      when sum(status2=0 and status1='x+1') > 0 then 'x+1'
      else status1
    end as status1
  , status2
  from have
  group by product_no
  ;