做一段时间,直到所有观察日期等于Today()sas proc sql

时间:2019-01-22 02:19:44

标签: loops date macros sas

我必须每天早上生成一份报告,但是直到那天要查询的所有表都已更新,我才能这样做。

我想创建一个宏,直到所有表都更新后才继续进行sas处理。 SSDM中有一个表,其中所有表以及它们的更新日期和时间。为了方便起见,我将此表称为Info,列名称为tablename和dateupdated。我要使用的表是n个表中的table1,table2和table3。

%macro Updated;

proc sql;
create table Data_ready as
select 
tablename,
dateupdated,
case when dateupdated=today() then 'Ready'
 else 'Not Ready'
 end as 'Status'
from Info
where tablename in (table1, table2, ..., tablen)
quit;


%if count(Data_ready.Status = 'Ready') ne count(Data_ready.tablename) %then %do;
proc sql;
drop table work.Data_ready
;quit;
sleep(60*30,1);
%end;
%else %do;
proc print data=Data_ready;
run;
%end
%mend;
*here I will have the rest of the code to produce the report knowing that the information is up to date

是否可以通过do while或do until做到这一点?我一直在尝试找出某种宏,但是在确保所有表在更新之前都遇到了一些问题。预先感谢。

1 个答案:

答案 0 :(得分:1)

以下是一些示例代码(未经测试),这些示例代码使用DICTIONARY.TABLES检查数据集的修改时间戳,并计算其中有多少与today()相对应。 try_limit也用于防止无限等待。

%macro wait_for_all_today (libname=);

  %local today_count all_count;
  %local try try_limit try_wait_s;
  %local rc;

  %let try = 0;
  %let try_limit = 10;
  %let try_wait_s = 60;

  %do %until (&today_count = &all_count or &try > &try_limit);
    %let try = %eval (&try + 1);
    %if &try > 1 %then %do;
      %let rc = %sysfunc(sleep(&try_wait_s, 1));
    %end;
    proc sql noprint;
      select count(*), sum(today()=datepart(moddate)) 
      into :all_count, :today_count
      from dictionary.tables
      where libname = "%sysfunc(upcase(&libname))"
        and memtype = "DATA"
      ;
    quit;

    %* at this point today_count and all_count 
    %* have values that will be used in the UNTIL evaluation;
  %end;

  %if &today_count ne &all_count %then %do;
    %put ERROR: Not all data sets in Library &libname were updated today. Waited a bunch of times;
    %abort cancel;
  %end;

%mend;