I would like to have hour (hh format) and minute (mm format) parts of the current time into two macro variables. I know how to take date and time (as below), but how can I take hour and minute parts from &time?
Example 1) if time has 20:30, then I would like to have 20 for hh and 30 for mm.
Example 2) if time has 7:30, then I would like to have 7 for hh and 30 for mm.
%let date=%sysfunc(today(), yymmdd6.);
%let time=%sysfunc(time(), hhmm); %put &time;
Like this?
data _null_;
call symput('hh', hour(time()));
call symput('mm', minute(time()));
run;
%put &hh.;
%put &mm.;
if you want the same in open code
%let hour=%sysfunc(hour(%sysfunc(time())));
%put &=hour;
%let minute=%sysfunc(minute(%sysfunc(time())));
%put &=minute;
Unfortunately yes. For every SAS function to be executed in open code, you would need a %sysfunc wrapper exclusively. The idea is to basically pave way for the macro processor to execute SAS functions that are typically executed by the datastep compiler or SQL processor. However the use of %sysfunc is required to use this at compile time or in other words while compiler tokenizes words or texts in the input stack. A process that's done by word scanner. This basically processes one token at a time in a queue.
@braam wrote:
Thanks. So, do I have to use %sysfunc for every nested function?
No, you can use the solution from @PeterClemmensen and then zero %SYSFUNC calls are needed.
If you already have a macro variable with a string like:
16:45
Then just use %SCAN() function.
%let hh=%scan(&time,1,:);
%let mm=%scan(&time,2,:);
What do you want for HH or MM when the value is less than 10? Do you want 9 or 09?
The MONTH() or DAY() function return a number, so if you want the leading zero the use the Z format.
%let now=%sysfunc(time());
%let hh=%sysfunc(hour(&now),z2.);
%let mm=%sysfunc(minute(&now),z2.);
Note if you call TIME() twice there is small risk that you will accidentally convert 10:59 into 10:00 if the clock ticks over from 10:59 to 11:00 between the two calls. Better the save the value once and the use it twice.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.