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
@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;
Yes. The Functions Hour, Minute and Second all support datetime values.
Yes, but if hour is missing it only give "0" but i need "00"
@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.
Why do you need 00 ?
@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 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.