BookmarkSubscribeRSS Feed
braam
Quartz | Level 8

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;

 

8 REPLIES 8
PeterClemmensen
Tourmaline | Level 20

Like this?

 

data _null_;
   call symput('hh', hour(time()));
   call symput('mm', minute(time()));
run;

%put &hh.;
%put &mm.;
novinosrin
Tourmaline | Level 20

if you want the same in open code

 

%let hour=%sysfunc(hour(%sysfunc(time())));
%put &=hour;

%let minute=%sysfunc(minute(%sysfunc(time())));
%put &=minute;
braam
Quartz | Level 8
Thanks. So, do I have to use %sysfunc for every nested function?
novinosrin
Tourmaline | Level 20

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. 

PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller
Tom
Super User Tom
Super User

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?

braam
Quartz | Level 8
Thanks. I would like to have 09, but could you please let me know both?
Tom
Super User Tom
Super User

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.

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
  • 8 replies
  • 9585 views
  • 5 likes
  • 5 in conversation