sas - 将 SAS 中的数据分组到特定的存储桶中
问题描述
我需要一些帮助来满足以下要求
当前数据集(数据集名称:SAS1):
product_no product_type status1 status2
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
所以基本上,在上面的数据集中,如果status2=1 and status1='x' and product_type<>3
,那么对于两行,status1
应该是'nr'
。如果status2=1 and status1='x' and product_type=3
,那么对于这两行,status1
应该是'x+1'
。如果status2=0 and status1='x+1'
,那么对于这两行,status1
应该是'x+1'
所需输出(数据集名称:SAS2):
product_no product_type status1 status2
12345 3 nr 0
12345 1 nr 1
123456 3 x+1 1
123456 6 x+1 0
9876 3 x+1 0
9876 1 x+1 0
代码试过了,但没有用:
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;
上面的代码不起作用。因此,例如,对于 product_no=12345,该条件仅适用于该特定行,而不适用于整个组。所以对于 product_no=12345,列 status1='nr' 应该为两行填充,而不仅仅是一个。
解决方案
看来您需要一些分组才能将计算值应用于“两”行。从样本数据中,仅有的两行组将基于product_no
. 对组的逻辑评估的总和将告诉您组中的任何行是否符合标准。 当指定子句Proc SQL
时进行非聚合选择时,查询也将自动重新合并。group by
case 语句将根据 case 语句status1
的第一次出现 when 条件计算值
例子:
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
;
推荐阅读
- javascript - 全局变量不会改变
- function - react-native 中的小问题 活动指标 | 组件 | 组件内的道具 | 道具 | 设置状态
- php - Wordpress Bootstrap Accordion:一次打开一张卡片
- javascript - 如何将一组图像显示为列表并使其可点击以使用 javascript 显示一些文本?
- javascript - 使用 Express 保存 POST 请求的数据
- assembly - 汇编程序中的主程序是否需要堆栈帧?
- ruby-on-rails - Amazon s3:Gmail 上的图片链接已失效
- html - 我有一个代码,我需要将“a”标签更改为“li”。当我这样做时,css动画停止工作
- hyperledger-fabric - 如果 peer 调用相同的函数,execute-order-validate 架构会产生大量无效事务吗?
- woocommerce - Authorize.Net:他们将哪些数据发送回商家来结算订单?