【问题标题】:Do While, Do until all observation dates equal to today() sas proc sqlDo While,直到所有观察日期等于 today() sas proc sql
【发布时间】:2019-06-15 10:57:39
【问题描述】:

我必须每天早上生成一份报告,但直到我查询的所有表都已更新当天才能执行。

我想创建一个宏,在所有表都更新之前不会继续 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 来做到这一点?我一直在尝试找出某种宏,但是在确保所有表在继续之前都已更新时遇到了一些问题。提前致谢。

【问题讨论】:

  • WORK.INFO 表从何而来?既然它在您的 WORK 库中,它是如何更新的?这是伪代码吗?您需要知道如何查询数据集元数据吗? SSDM 是 libname 吗?
  • 我更新了我的代码,因为我看到了一些错误,但“信息”表只是给定库中更新的表和日期和时间的列表。我需要知道“信息”中列出的日期是否是今天才能知道它已更新,其余代码中的代码可以使用最新数据执行。

标签: loops date macros sas


【解决方案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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多