BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
hhchenfx
Barite | Level 11

Hi,

My main macro will run every minute and inside that code, I want to run a small code just one time when time in between 15:00 - 15:01.

Can you please help me to do it?

I thought the one below will do the job but it doesn't.

Thanks,

HHC

%macro try;
  %if  15:00:00 < %sysfunc(time(),.0) < 15:02:00 %then %do;
    data a; set _NULL_;
	run;
  %end;
  %else %do;
  	data b; set _NULL_;
  %end;
%mend;

%try;
1 ACCEPTED SOLUTION

Accepted Solutions
s_lassen
Meteorite | Level 14

I think this may work:

%macro try;
  %local now;
  %let now=%sysfunc(time(),time8.);
  %if "&now">"15:00:00" and "&now"<"15:02:00" %then %do;
 

Two points:

  1. It is better to use the formatted value for comparison, as it makes the code a lot easier to read. And the character comparison shown should work.
  2. a<b<c does not mean the same in SAS macro language as in the datastep. Instead it compares the boolean value (1 or 0) from the expression a<b with the value of c. You have to have two comparisons with AND here.

View solution in original post

5 REPLIES 5
ballardw
Super User

Set OPTIONS MPRINT; run your macro and share the result from the log.

Note: "I thought the one below will do the job but it doesn't." is not very informative.

 

I suspect, if that is your code that you posted is correct that one of two things is happening: you are getting a ERROR because the statement: time8 run; doesn't make any sense

216  data a; set _NULL_;time8
                        -----
                        180
ERROR 180-322: Statement is not valid or it is used out of proper order.

217     run;

OR that the second bit is running because you are missing a RUN to end the data step. Until a data step boundary occurs in the second case the data step is "running" but not finished.

 

Macro language treats everything as character so when you compare

 15:00:00 < %sysfunc(time(),.0)

You are comparing a string of 15:00:00 to a different string and you need to address what the %sysfunc i actually returning (Hint: remove the format as length 0 formats don't work well for anything). If the time actually were 15:00:00 the result of %sysfunc(time()) would be 54000. When you place that into a .0 format you get an * as SAS can't fit that into that format at all. Maybe you though that TIME8 was on a different line???

 

Note that any data step or procedure inside a macro really really needs an explicit RUN; as the timing for when a following boundary may be very problematic.

 


@hhchenfx wrote:

Hi,

My main macro will run every minute and inside that code, I want to run a small code just one time when time in between 15:00 - 15:01.

Can you please help me to do it?

I thought the one below will do the job but it doesn't.

Thanks,

HHC

%macro try;
  %if  15:00:00 < %sysfunc(time(),.0) < 15:02:00 %then %do;
    data a; set _NULL_;time8
	run;
  %end;
  %else %do;
  	data b; set _NULL_;
  %end;
%mend;

%try;

 

hhchenfx
Barite | Level 11

Hi,

That time8 is a typo.

I try to convert time to second but still the comparison fail, always return "data a" even current time is 68102.7880001068 , which is outside the range of 67800-68000

31280 %macro try;
31281 %if 67800 < %sysfunc(time())<68000
31282 %then %do;
31283 data a; set _NULL_;
31284 run;
31285 %end;
31286 %else %do;
31287 data b; set _NULL_;
31288 %end;
31289 %put %sysfunc(time());
31290 %mend;
31291
31292 %try;

NOTE: The data set WORK.A has 0 observations and 0 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds


68102.7880001068

 

hhchenfx
Barite | Level 11

So somehow I have to break that a<b<c into a<b and b<c to get the IF THEN works.


%macro try;
  %if 67800 < %sysfunc(time()) AND %sysfunc(time())<78000
 %then %do;
    data a; set _NULL_;
	run;
  %end;
  %else %do;
  	data b; set _NULL_;
  %end;
  %put %sysfunc(time());
%mend;

%try;
s_lassen
Meteorite | Level 14

I think this may work:

%macro try;
  %local now;
  %let now=%sysfunc(time(),time8.);
  %if "&now">"15:00:00" and "&now"<"15:02:00" %then %do;
 

Two points:

  1. It is better to use the formatted value for comparison, as it makes the code a lot easier to read. And the character comparison shown should work.
  2. a<b<c does not mean the same in SAS macro language as in the datastep. Instead it compares the boolean value (1 or 0) from the expression a<b with the value of c. You have to have two comparisons with AND here.
hhchenfx
Barite | Level 11

You code and explanation is very much helpful.

Thanks,

HHC

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 5 replies
  • 944 views
  • 5 likes
  • 3 in conversation