基于条件结束的 SAS 宏循环

SAS Macro loop that ends based on criteria

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

我有一些代码可以从 table 中按顺序提取记录 (id)。我希望代码循环直到 table 耗尽。我写出了这个叫做catalina的宏。我希望它 运行 直到我在宏的最后一行创建的计数变量 = 到 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 是我唯一的查找值。
我的 table 包含我想从我的父级 table 中依次删除的引脚,直到 table 降至 0。

感谢您的帮助!

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

因此将不重复的部分放在 %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;

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