Format hh:mm:ss to hhmm

Accepted Solution Solved
Reply
Contributor
Posts: 70
Accepted Solution

Format hh:mm:ss to hhmm

[ Edited ]

Hi,

 

I'm trying to add the date and time to the end of an excel file name, with no colons, within a LIBNAME.

 

I currently have 

 

Data Null;

call SYMPUTX('Date',put(totday(),Date9.));

Call SYMPUTX('Hour',Hour(datetime()));

Call SYMPUTX('Minute',Minute(datetime()));

 

Run;

 

LIBNAME XL XLSX "/SASData/Dean/Export1_&Date-&Hour&Minute..xlsx";

 

This returns as

 

 LIBNAME XL XLSX "/SASData/Dean/Export1_11Jan2017-939..xls";

 

 

The 9 being the hour the 39 being the minutes. However if the minutes is a single number it only returns one digit.

 

eg if the time was 9:09 the output would be 99.

 

I want to get the hour and minute to display two digits each. - so in the above example the out put would be 0909.

 

I don't mind if the output is in 24hr clock

 

 

This is what I would like the output to be.

LIBNAME XL XLSX "/SASData/Dean/Export1_11Jan2017-0939..xlsx";

 

Any help[ appreciated


Accepted Solutions
Solution
‎01-10-2017 06:55 PM
Super User
Posts: 19,192

Re: Format hh:mm:ss to hhmm

[ Edited ]

Format it with Z2 similar to how you formatted the date to DATE9.

 

Or roll your own date time format that does both date and time in one shot. If you use this across multiple projects it's not a bad thing to have around.

 

 

proc format;
picture file_date (default=20)  low-high = '%0d%3B%000y_%0H%0M' (datatype=datetime);
run;

Data Null;
call SYMPUTX('Date',put(today(),Date9.));
Call SYMPUTX('Hour',put(Hour(datetime()), z2.));
Call SYMPUTX('Minute',put(Minute(datetime()), z2.));

Call SYMPUTX('file_date',put(datetime(), file_Date.));

Run;

%put &file_date;
  

View solution in original post


All Replies
Solution
‎01-10-2017 06:55 PM
Super User
Posts: 19,192

Re: Format hh:mm:ss to hhmm

[ Edited ]

Format it with Z2 similar to how you formatted the date to DATE9.

 

Or roll your own date time format that does both date and time in one shot. If you use this across multiple projects it's not a bad thing to have around.

 

 

proc format;
picture file_date (default=20)  low-high = '%0d%3B%000y_%0H%0M' (datatype=datetime);
run;

Data Null;
call SYMPUTX('Date',put(today(),Date9.));
Call SYMPUTX('Hour',put(Hour(datetime()), z2.));
Call SYMPUTX('Minute',put(Minute(datetime()), z2.));

Call SYMPUTX('file_date',put(datetime(), file_Date.));

Run;

%put &file_date;
  
Contributor
Posts: 70

Re: Format hh:mm:ss to hhmm

Thanks Reza. - helpful as always - works a treat.

 

Cheers

Respected Advisor
Posts: 4,138

Re: Format hh:mm:ss to hhmm

One way to go:

Data Null;
  call SYMPUTX('Date',put(today(),Date9.));
  Call SYMPUTX('Hour',put(Hour(datetime()),z2.));
  Call SYMPUTX('Minute',put(Minute(datetime()),z2.));
Run;
Contributor
Posts: 70

Re: Format hh:mm:ss to hhmm

Thanks Patrick - yours and Reeza's advice works a treat

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 258 views
  • 1 like
  • 3 in conversation