基于条件结束的SAS宏循环

问题描述

我正在用SAS编写,主要使用proc sql进行数据清理。

我有一些代码在这代码中我从表中顺序提取记录(id)。我希望代码循环到表耗尽为止。我写了这个叫做catalina的宏。我希望它运行直到我在宏的最后一行上创建的count变量=等于0为止。

%macro catalina;  
proc sql;  
create table todelete as select max(id_todelete) as id_todelete from ids group by id_todelete;  
delete from pairs where id_a = any(select id_todelete from todelete);  
delete from pairs where id_b = any(select id_todelete from todelete);  
insert into matched select * from pairs where match_dist = (select min(match_dist) from pairs);  
insert into ids (id_todelete) select id_a from matched;  
insert into ids (id_todelete) select id_b from matched;  
select count(*) as count from pairs;  
%mend;  

pin是我唯一的查找值。
我的桌子上放有我要从父表中顺序删除的引脚,直到该表降为0。

感谢您的帮助!

解决方法

要使宏生成多个代码块,您需要一些宏逻辑。看起来您需要一个简单的%DO%UNTIL()构造。请注意,您将需要在最后一步中创建一个实际的宏变量,而不仅仅是像当前代码一样将结果打印到输出目标。您是否确定您的过程将始终达到零肥胖?如果不是,则添加更多逻辑以固定一些步骤数后停止。或者也许会根据将永远发生的其他标准停止运行,例如检测到零观测值被删除。

因此,请在%DO循环之前或之后放置不重复的部分。

%macro catalina;
%local count ;
%let count=-1;
proc sql;
%do %until(&count <= 0);
  create table todelete as 
    select max(pin_todelete) as pin_todelete from pins group by pin_todelete
  ;
  delete from pairs where pin_a = any(select pin_todelete from todelete);
  delete from pairs where pin_b = any(select pin_todelete from todelete);
  insert into matched 
     select * from pairs 
     where match_dist = (select min(match_dist) from pairs)
  ;
  insert into pins (pin_todelete) select pin_a from matched;
  insert into pins (pin_todelete) select pin_b from matched;
  select count(*) format=32. into :count trimmed from pairs;
%end;
quit;
%mend;

如果您进一步解释算法的作用,您可能会得到一个答案,该答案显示了一种更简单的方法。