BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ijmoorejr
Calcite | Level 5

I am attempting to write some code, possibly a do until loop, that will keep running the following proc sql block until all of the conditions in the where clause are met. Once the conditions are met, I need to run an X command. What is the best way to code this? Please provide sample code. Thanks.

 

proc sql;

create table trigger as

select Name, TDate, Status

from TDB.TTABLE

where Name="Load" and TDate=today() and Status="Ready";

quit;

 

x 'ksh /path/to/file/kshell.ksh'
1 ACCEPTED SOLUTION

Accepted Solutions
MichaelLarsen
SAS Employee

I agree with RW9 that scheduling software may be the best solution.

 

But I did create an example for you to try, but tested on Windows. I would expect the same behaviour on Unix.

 

%macro waitfor_new_data(Waittime=1, Maxtries=5);
  %let Try=1;
  %let trigger=0;
  %* Loop until either new data is ready or max number of tries reached.;
  %do %until(&trigger = 1 or &Try > &Maxtries );
    proc sql noprint;
      select 
        count(*) > 0 format=8.
        into : trigger trimmed
      from TDB.TTABLE
      where Name="Load" and TDate=today() and Status="Ready";
    quit;
    %let Try=%eval(&Try+1);
    %if &trigger = 0 and &Try <= &Maxtries %then %do;
      %put NOTE: Waiting for &Waittime seconds...;
      %let rc=%sysfunc(sleep(&Waittime,1));
    %end;
  %end;

  %if &trigger = 0 %then %do;
    %put NOTE: Max tries executed and no new data found, execution stopped.;
  %end;
  %else %do;
    %put NOTE: New data found, executing script.;
/*x 'ksh /path/to/file/kshell.ksh';*/
  %end;
%mend;

/* Create a test table */
libname tdb "C:\Temp";
data tdb.TTABLE;
  Name = 'Load';
  TDate = today()-1;
  Status = 'Ready' ;
run;
%waitfor_new_data(Waittime=2, Maxtries=5);

View solution in original post

4 REPLIES 4
Peter_C
Rhodochrosite | Level 12
Sleep function provides a controlled delay until retesting to see if the filter returns a non-empty set.
I don't see how a data step could use the DO UNTIL() control as (iirc) a data step stops when the WHERE executes and returns an empty set.
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Use scheduling software, its what it is made for and will be better than anything you will likely write.

MichaelLarsen
SAS Employee

I agree with RW9 that scheduling software may be the best solution.

 

But I did create an example for you to try, but tested on Windows. I would expect the same behaviour on Unix.

 

%macro waitfor_new_data(Waittime=1, Maxtries=5);
  %let Try=1;
  %let trigger=0;
  %* Loop until either new data is ready or max number of tries reached.;
  %do %until(&trigger = 1 or &Try > &Maxtries );
    proc sql noprint;
      select 
        count(*) > 0 format=8.
        into : trigger trimmed
      from TDB.TTABLE
      where Name="Load" and TDate=today() and Status="Ready";
    quit;
    %let Try=%eval(&Try+1);
    %if &trigger = 0 and &Try <= &Maxtries %then %do;
      %put NOTE: Waiting for &Waittime seconds...;
      %let rc=%sysfunc(sleep(&Waittime,1));
    %end;
  %end;

  %if &trigger = 0 %then %do;
    %put NOTE: Max tries executed and no new data found, execution stopped.;
  %end;
  %else %do;
    %put NOTE: New data found, executing script.;
/*x 'ksh /path/to/file/kshell.ksh';*/
  %end;
%mend;

/* Create a test table */
libname tdb "C:\Temp";
data tdb.TTABLE;
  Name = 'Load';
  TDate = today()-1;
  Status = 'Ready' ;
run;
%waitfor_new_data(Waittime=2, Maxtries=5);
MichaelLarsen
SAS Employee
For the example to find new data you should remove the -1 in the data step that generates the test table.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1023 views
  • 1 like
  • 4 in conversation