Hi,
I have a table in which time is in num 8 and format time8.. I am trying to convert it in to HH:MM:SS AM/OM format.
When I am using below code
data test1;
infile datalines;
length time 6. time1 6.;
input time 6. time1 6.;
format time time12. time1 timeampm.;
datalines;
464633 464633
464628 464628
464632 464632
464624 464624
464634 464634
464632 464632
;
run;
OUTPUT is:
Time Time1
129:03:53 | 12:54:23 PM |
129:03:48 | 12:54:22 PM |
129:03:52 | 12:54:23 PM |
129:03:44 | 12:54:22 PM |
129:03:54 | 12:54:23 PM |
129:03:52 | 12:54:23 PM |
For below code output is
data test1;
infile datalines;
length time 6.;
input time 6. ;
format time time8.;
datalines;
464633
464628
464632
464624
464634
464632
;
run;
OUTPUT
Time
129:03
129:03
129:03
129:03
129:03
129:03
and same variable is giving me below output
data test1;
infile datalines;
length time 6.;
input time 6. ;
format time timeampm.;
datalines;
464633
464628
464632
464624
464634
464632
;
run;
9:03:53 AM
9:03:48 AM
9:03:52 AM
9:03:44 AM
9:03:54 AM
9:03:52 AM
Can anyone let me know which one is the correct one and how to convert Time into HH:MM:SS format.
My objective is to concatenate date and time field together?
Is this a duration measure? Time is measured in seconds. So max value for a 24 hour period is 60*60*24 = 86400.
Your data indicates 6 days and 9 hours + some minutes.
data time;
do i=1 to 86400;
format i timeampm.;
output;
end;
run;
data test1;
infile datalines;
input time time1 ;
format time timeampm. time1 datetime21.;
datalines;
464633 464633
464628 464628
464632 464632
464624 464624
464634 464634
464632 464632
;
run;
Hi Reza,
I am also not sure just trying to figure it out. I put all analysis and out in my question.
Thanks
Well basically all are valid options so you probably need to consult the documentation or ask someone who works with that data what the field should represent.
Are you sure it is a time variable?
I have seen numbers like that be the number of days since 1Jan1900 - a system recorded all dates like that.
464633 + 1Jan1900 = 14 Feb 1972
Would that make more sense?
Or the number of seconds since a particular date? In which case you need to know the base date which time is calculated from.
Good Luck!
Hi,
It's time varaiable and time is calculated in hrs. as sas stored it in numerical value starting from 1Jan 1060.
SAS treats dates as days from 1 Jan 1960 and datetime values as seconds from 1 Jan 1960. Time is number of seconds since midnight. You are getting 129 hours because that's how hours are in 464633 seconds. The difference when you use the timeampm format is that it is discarding any extra hours of the 5 days worth and showing the clock time. If you use a datetime format you'll see that value could also be 06JAN60:09:03:53 which agrees with the clock time part. It looks like this variable may be a datetime. If you want just the time portion you can get that by TimVar=timepart(time);
I would recommend against setting a length on date, time and especially datetime variables as you may start truncating your data.
ballardw make a very good point about setting date/time lengths. The way you have the length statements written, e.g.:
length time 6. time1 6.;
suggests that you may be confusing format specifications with length specifications. In a length statement, do not include a period after the numerical length specified. If you actually wanted the time and time1 variables to be internally stored as 6 bytes, specify:
length time 6 time1 6;
without the periods.
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.