BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
vraj1
Quartz | Level 8

I have sas date time19.  and want to extract hour minutes and seconds, I can use hour() function but if it is 00 then it wont work

date

15NOV2022:00:00:00

hour  minute  second

00       00            00

 

Can anyone help me in this

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@vraj1 wrote:

Yes, but if hour is missing it only give "0" but i need "00"


Zero is not a missing value.  If the HOUR was missing then the whole datetime value would have had to have been missing.

If you have the value zero there is no difference between typing it as 0 or 00 or 0000 they all mean zero.

If you want to have a number printed using leading zeros then use the Z format.

Try this:

data want;
  set have;
  time=timepart(dtvar);
  hour=hour(dtvar);
  minute=minute(dtvar);
  second=second(dtvar);
  format time tod8. hour minute second z2. ;
run;

View solution in original post

5 REPLIES 5
PeterClemmensen
Tourmaline | Level 20

Yes. The Functions Hour, Minute and Second all support datetime values.

vraj1
Quartz | Level 8

Yes, but if hour is missing it only give "0" but i need "00"

PaigeMiller
Diamond | Level 26

@vraj1 wrote:

Yes, but if hour is missing it only give "0" but i need "00"


In SAS, a numeric variable can have a value of 0 but it cannot have a value of 00

 

If you really need a value of 00, assign a custom format to it.

 

This of course brings up the question, why do you need 00? Are you going to use it as a character string? Please explain this, as there are probably better ways to handle this.

--
Paige Miller
Tom
Super User Tom
Super User

@vraj1 wrote:

Yes, but if hour is missing it only give "0" but i need "00"


Zero is not a missing value.  If the HOUR was missing then the whole datetime value would have had to have been missing.

If you have the value zero there is no difference between typing it as 0 or 00 or 0000 they all mean zero.

If you want to have a number printed using leading zeros then use the Z format.

Try this:

data want;
  set have;
  time=timepart(dtvar);
  hour=hour(dtvar);
  minute=minute(dtvar);
  second=second(dtvar);
  format time tod8. hour minute second z2. ;
run;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 2096 views
  • 0 likes
  • 4 in conversation