Loop until specified datetime

Reply
Occasional Contributor
Posts: 16

Loop until specified datetime

Dear all, I would like to write a macro program, that will work until a given datetime (passed as a parameter). I wrote something like this: %global current; %macro test_loop(dt); data _null_; call symput('current', put(datetime(), best12.)); %let end_dt=%sysfunc(inputn(&dt., datetime.)); run; %do %until (&current.>&end_dt.); /* %include 'some_program.sas';*/ data _null_; call symput('current', put(datetime(), best10.0)); %put &current.; run; %end; %mend test_loop; %test_loop('27FEB2018:15:00:00'); But unfortunatelly it doesn't work. Can anyone help me please Smiley Happy ? Thanks in advance!
Occasional Contributor
Posts: 16

Re: Loop until specified datetime

 Hi again,

 

Something wrong went with formatting, so I paste my post again:

 

 Dear all,

 

I would like to write a macro program, that will work until a given datetime. I wrote something like this:

%global current;

%macro test_loop(dt);
data _null_;
call symput('current', put(datetime(), best12.));
%let end_dt=%sysfunc(inputn(&dt., datetime.));
run;

%do %until (&current.>&end_dt.);

/* %include 'some_program.sas';*/

data _null_;
call symput('current', put(datetime(), best10.0));
%put &current.;
run;
%end;
%mend test_loop;

%test_loop('27FEB2018:15:00:00');

 

But unfortunatelly it doesn't work. Can anyone help me please Smiley Happy ?

 

 Thanks in advance!

Super User
Posts: 9,280

Re: Loop until specified datetime

Please use the proper subwindow for posting code, and use visual formatting to make your code readable, like this:

%global current;

%macro test_loop(dt);

data _null_;
call symput('current', put(datetime(), best12.));
%let end_dt=%sysfunc(inputn(&dt., datetime.));
run;

%do %until (&current.>&end_dt.);

  /* %include 'some_program.sas';*/

  data _null_;
  call symput('current', put(datetime(), best10.0));
  %put &current.;
  run;

%end;

%mend test_loop;

%test_loop('27FEB2018:15:00:00');

And please define "doesn't work".

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 16

Re: Loop until specified datetime

Posted in reply to KurtBremser

 Hi!

 

Sure, I will use the proper subwindow (sorry, I didn't notice it before).

 

And about the definition of "doesn't work". I thought that when I run this code, in every second there will be current datetime in the log:

call symput('current', put(datetime(), best10.0));
%put &current.;

But unfortunately there is the same value for first, let's say, 5 second, another value of next 5 second and so on.

 

In general, I would like to achieve the following goal:

at 2PM I call this macro in this way:

%test_loop('27FEB2018:16:00:00');

and at 4PM it will stop work (because value of current variable will be grater that value of end_dt variable).

 

 Best,

 F.

Super User
Posts: 9,280

Re: Loop until specified datetime

Try this:

%global current;

%macro test_loop(dt);

%let end_dt=%sysfunc(inputn(&dt., datetime.));
%put end_dt=&end_dt.;

data _null_;
call symput('current', put(datetime(), best12.));
run;

%do %until (&current.>&end_dt.);

  /* %include 'some_program.sas';*/

  data _null_;
  x =sleep(5,1);
  run;

  data _null_;
  call symput('current', put(datetime(), best10.0));
  run;
  
  %put &current.;
%end;

%mend test_loop;

%test_loop(%sysfunc(intnx(minute,%sysfunc(datetime()),1),datetime19.));

it works for me.

The data _null_ with the sleep() is in there to prevent the creation of an oversized log (too many quick iterations).

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 16

Re: Loop until specified datetime

Posted in reply to KurtBremser

 Hi again,

 

It works for me as well. So thank you very much for your help, really appreciate it !!! Smiley Happy

 

 Best,

 F.

Ask a Question
Discussion stats
  • 5 replies
  • 110 views
  • 0 likes
  • 2 in conversation