在Do While或Do Until语句中执行Proc SQL语句

时间:2016-12-21 17:31:14

标签: sql loops proc-sql

我有一个sql数据库,可以在早上4:00到早上7:00之间进行更新。我想运行一个自动程序,一旦可用,就会提取数据。它目前每30分钟检查一次。我已经编写了一个可以完成此任务的SAS程序,但我希望压缩代码并在真正的循环中执行它。以下是我目前的代码。我重复代码块1总共七次。如果数据在第一次检查时可用,那么我的代码仍将执行7次。我希望它在第一次在表中找到数据后结束。我已经无情地尝试创建一个循环,但在所有努力中都失败了。

%Let RecordCount = 0;/*Sets initial Record Count to 0*/
%Let min = 30;

/ 确定SAS等待尝试的时间      收到0条记录后重新查询表 /

data _NULL_; /*Get the previous Working Day based on todays date*/
  DateCheck = weekday(Today());
  Select (DateCheck);
  When (1) Do; 
        call symputx('_ReportDt',intnx('day',Today(),-2));
  end;
  When (2) Do;
    call symputx('_ReportDt',intnx('day',Today(),-3));
  end;                                    
  otherwise do;
    call symputx('_ReportDt',intnx('day',Today(),-1));
  end;
  end;
run;

/****************************1***************************/

Proc Sql noprint; 
  Select Count(ACCOUNT_NUMBER)
  Into :RecordCount separated by ' '
  From Table1 
  WHere Date = &_ReportDt;
Quit;

data _null_;
if &RecordCount = 0 then do;
                            wait_sec= (60*&min);
                            time_slept = sleep(wait_sec,1);
                        end;
                    else do;
end; 
run;

1 个答案:

答案 0 :(得分:0)

我能够找到解决方案。下面是我能够开始工作的代码。我希望这可以帮助别人。

%MACRO SLEEP(MINUTES);
 DATA _NULL_;
 wait_sec= (60*&MINUTES);
 Var1 = sleep(wait_sec,1);
 RUN;
 %MEND SLEEP; 

 %MACRO DataCheck();
    Proc Sql noprint; 
        Select Count(ACCOUNT)
        Into :RecordCount separated by ' '
        From Table
        Where Date = &_ReportDt;
    Quit;
%DO %WHILE (&RecordCount = 0);
    Proc Sql noprint; 
        Select Count(ACCOUNT)
        Into :RecordCount separated by ' '
        From Table
        WHere Date = &_ReportDt;
    Quit;
    %SLEEP(15);/*Insert the number of Minutes that you want the program to sleep in between data checks.*/
%END;
 /*PROCESS*/
 %PUT "IT WORKED!";
%MEND DataCheck;

 data _null_;
 %DataCheck;
 run;