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;
@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 I think you forgot to evaluate the expression. Otherwise, the - 20 part is just text.
%let now = %sysevalf(%sysfunc(time()) - 20);
%put &=now.;
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.)
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 , 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
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!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.