group-by - SAS PROC SQL 按组的第一个值
问题描述
我有一个这样的数据集:
DATA try;
INPUT month :DATE9. account_id mob;
FORMAT month DATE9.;
DATALINES;
01JAN2004 11 9
01FEB2004 11 10
01MAR2004 11 11
01JUN2008 11 0
01JAN2005 12 -1
01FEB2005 12 -1
01MAR2005 12 0
01APR2005 12 1
01MAY2005 12 2
01JUN2005 12 3
01DEC2006 12 0
;
RUN;
我想为第一个(按 account_id,月份排序)高于0origination_date
的帐户创建一个变量,例如为第一个小于或等于 0的帐户创建一个最小month - mob
日期。mob
month
mob=0
mob
因此,预期输出将是:
DATA output;
INPUT month :DATE9. account_id mob origination_date :DATE9.;
FORMAT month DATE9. origination_date DATE9.;
DATALINES;
01JAN2004 11 9 01APR2003
01FEB2004 11 10 01APR2003
01MAR2004 11 11 01APR2003
01JUN2008 11 0 01APR2003
01JAN2005 12 -1 01MAR2005
01FEB2005 12 -1 01MAR2005
01MAR2005 12 0 01MAR2005
01APR2005 12 1 01MAR2005
01MAY2005 12 2 01MAR2005
01JUN2005 12 3 01MAR2005
01DEC2006 12 0 01MAR2005
;
RUN;
到目前为止,我有这个代码:
proc sql;
create table wanted as
select a.*,
coalesce( min(case when a.mob = 0 then a.month else . end),
min(intnx('month', a.month, -a.mob)) ) as origination_date format date9.
from try a
group by account_id
order by account_id, month;
quit;
但这不适用于 account_id = 11 的帐户,因为我不知道如何在 first 上添加条件mob
:首先mob
小于或等于零。你知道我怎样才能做到这一点proc sql
吗?基本上,我只想更改代码,使其min(case when a.mob = 0 then a.month else . end)
仅适用于第一个mob
小于或等于零的帐户。
解决方案
我不知道如何一步完成,first.
并且last.
在数据步骤中没有变量。可能在某些数据库中您可以做到(使用 Passthrough)。
我认为使用 sas 你应该分两步完成:
1)origination_date
根据条件计算每个观察值。
2) 使用 聚合变量proc sql
。
data want1;
set try;
format origination_date date9.;
by account_id month;
retain flag origination_date;
/*flag var description: 1-higher then 0, -1-less or equal 0*/
if first.account_id then do;
if mob > 0 then flag=1; else flag=-1;
origination_date="31DEC9999"d;
end;
if flag=1 then do;
if origination_date > intnx('month',month,-mob) then
origination_date=intnx('month',month,-mob);
end;
else do;
if flag = -1 and mob=0 then do;
if origination_date > month then
origination_date=month;
end;
end;
run;
proc sql;
create table want2 as
select account_id,month,mob,min(origination_date)as origination_date format=date9.
from want1
group by account_id
order by account_id,month;
quit;
输出:
+------------+-----------+-----+------------------+
| account_id | month | mob | origination_date |
+------------+-----------+-----+------------------+
| 11 | 01JAN2004 | 9 | 01APR2003 |
| 11 | 01FEB2004 | 10 | 01APR2003 |
| 11 | 01MAR2004 | 11 | 01APR2003 |
| 11 | 01JUN2008 | 0 | 01APR2003 |
| 12 | 01JAN2005 | -1 | 01MAR2005 |
| 12 | 01FEB2005 | -1 | 01MAR2005 |
| 12 | 01MAR2005 | 0 | 01MAR2005 |
| 12 | 01APR2005 | 1 | 01MAR2005 |
| 12 | 01MAY2005 | 2 | 01MAR2005 |
| 12 | 01JUN2005 | 3 | 01MAR2005 |
| 12 | 01DEC2006 | 0 | 01MAR2005 |
+------------+-----------+-----+------------------+
推荐阅读
- django - 如何在 Django 中使整行独一无二
- ruby-on-rails - 将 Rails 6 应用程序部署到 Google Cloud Platform App Engine 时出错
- python - 如何使用 BeautifulSoup 查找没有 id 或类名的 html 标签?
- c# - NFluent:流利地检查异常的属性
- python - Slack API Python客户端无法搜索频道
- php - Symfony 4 - 在我的表单中使用服务(在 query_builder 中)
- java - Tomcat SSL 增加了响应时间的开销 - 作为 Springboot 嵌入式容器运行
- html - div没有足够的高度
- html - 获取文本使用硒
- click - 按住另一个键时将鼠标滚轮重新映射到 LClick AHK