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,

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
Barite | Level 11

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
BASUG is hosting free webinars Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
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

 

 

BASUG is hosting free webinars Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 668 views
  • 2 likes
  • 4 in conversation