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

Hi,

I want to put hour and minute of the time 20 second before current time into macro variable.

The code below might convey my idea better.

Can you please help?

Thanks,

HHC

 

%let now=%sysfunc(time()       MINUS 20 SECOND);
%let hh=%sysfunc(hour(&now),z2.);
%let mm=%sysfunc(minute(&now),z2.);

%put &hh &mm;
1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

@Astounding I think you forgot to evaluate the expression. Otherwise, the - 20 part is just text.

 

%let now = %sysevalf(%sysfunc(time()) - 20);

%put &=now.;

View solution in original post

7 REPLIES 7
Astounding
PROC Star
Have you tried

%let now = %sysfunc(time() ) - 20;

The other statements probably figure out to perform the math without even adding %eval
PeterClemmensen
Tourmaline | Level 20

@Astounding I think you forgot to evaluate the expression. Otherwise, the - 20 part is just text.

 

%let now = %sysevalf(%sysfunc(time()) - 20);

%put &=now.;
Astounding
PROC Star
I didn't worry about the text, since I expected the subsequent functions could utilize that text. What I do worry about is the first 20 seconds after midnight, and whether greater care would be needed to calculate properly. I can't check this sort of detail since it has been 4 years now that I have used a computer that actually has SAS on it.
hhchenfx
Rhodochrosite | Level 12

I did try -20 and SAS give following error.

I should have included it in the original post.

1627  %let now=%sysfunc(time()       - 20);
ERROR: Expected close parenthesis after macro function invocation not found.
1628  %let hh=%sysfunc(hour(&now),z2.);
1629  %let mm=%sysfunc(minute(&now),z2.);
1630
1631  %put &hh &mm;
0,z2.) 0,z2.)
Quentin
Super User

As shown in the log, your parentheses are wrong.

 

You need to use parentheses around %sysfunc(time()).  That will return a number with a decimal in it (number of seconds since midnight).  Then if you want to subtract 20 from that value, you need %sysevalf, as shown by @PeterClemmensen .

 

%let now=%sysevalf( %sysfunc(time()) - 20);
%let hh=%sysfunc(hour(&now),z2.);
%let mm=%sysfunc(minute(&now),z2.);
%put &=now &=hh &=mm ;

Actually, as @Astounding pointed out the %sysevalf is not essential here, as the HOUR function is happy to take an expression as its argument, so you can do stuff like:

1    %put %sysfunc(hour(3600.5-20),z2.);
00
2    %put %sysfunc(minute(3600.5-20),z2.);
59

And to answer the question about negative values, they are not a problem.  A negative value for a SAS time is seconds before midnight:

4    %put %sysfunc(hour(5-20),z2.);
23
5    %put %sysfunc(minute(5-20),z2.);
59
Astounding
PROC Star

@Quentin 

 

When I posted the solution originally, I had the parentheses wrong.  Probably, @hhchenfx copied that original post and didn't get notified about the correction I had made.

 

Still, I'm not sure what happens when we hit just after midnight.

 

Quentin
Super User


@Astounding , 5 seconds after midnight time() will return 5, so 5-20 will be negative, but it works and returns 23:59.  That was what I was trying to show with this example:

 

 

4    %put %sysfunc(hour(5-20),z2.);
23
5    %put %sysfunc(minute(5-20),z2.);
59

 

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 7 replies
  • 1678 views
  • 2 likes
  • 4 in conversation