SAS 数据步骤:不使用 first/last 对多行进行子集化
SAS DATA STEP: subset multiple rows without using first/last
我正在尝试撤回给定子集数据的所有 MAX 实例....first.id 或 last.id 不起作用,因为我想保留同一事务的多行。例如:
在此示例中,我希望突出显示的行作为输出。我的数据有几个表格、季度和 CUST_ID 我想以编程方式让 SAS 根据表格、季度、CUST_ID
拉回最新
Last.DB_ID 只返回 1 行。我需要相同的所有行 DB_ID.
这也没有做任何事情:
data work.want;
set work.have;
by FORM Quarter Cust_ID DB_ID ;
if Max(DB_ID) then output;
run;
您需要两次遍历数据:一次确定该 ID 的最大值,另一次查找具有该最大值的行。
在数据步骤中执行此操作需要一个 DoW 循环,它针对每个 cust_id 值运行一个数据步骤迭代,但两次遍历数据集。
data want;
do _n_ = 1 by 1 until (last.cust_id);
set have;
by form quarter cust_id;
if last.cust_id then max_db_value=db_id;
end;
do _n_ = 1 by 1 until (last.cust_id);
set have;
by form quarter cust_id;
if db_id = max_db_Value then output;
end;
run;
如果 DB_ID 按照您的示例中的方式排序,则该方法有效。如果未排序,您可以将当前存储的 max_db_value 与当前的 db_id 进行比较,如果更高,则将 db_id 中的新值分配给它,例如
max_db_value = max(db_id, max_db_value);
而不是在 last.cust_id
为真时分配它。
我正在尝试撤回给定子集数据的所有 MAX 实例....first.id 或 last.id 不起作用,因为我想保留同一事务的多行。例如:
在此示例中,我希望突出显示的行作为输出。我的数据有几个表格、季度和 CUST_ID 我想以编程方式让 SAS 根据表格、季度、CUST_ID
拉回最新Last.DB_ID 只返回 1 行。我需要相同的所有行 DB_ID.
这也没有做任何事情:
data work.want;
set work.have;
by FORM Quarter Cust_ID DB_ID ;
if Max(DB_ID) then output;
run;
您需要两次遍历数据:一次确定该 ID 的最大值,另一次查找具有该最大值的行。
在数据步骤中执行此操作需要一个 DoW 循环,它针对每个 cust_id 值运行一个数据步骤迭代,但两次遍历数据集。
data want;
do _n_ = 1 by 1 until (last.cust_id);
set have;
by form quarter cust_id;
if last.cust_id then max_db_value=db_id;
end;
do _n_ = 1 by 1 until (last.cust_id);
set have;
by form quarter cust_id;
if db_id = max_db_Value then output;
end;
run;
如果 DB_ID 按照您的示例中的方式排序,则该方法有效。如果未排序,您可以将当前存储的 max_db_value 与当前的 db_id 进行比较,如果更高,则将 db_id 中的新值分配给它,例如
max_db_value = max(db_id, max_db_value);
而不是在 last.cust_id
为真时分配它。