- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hello,
I have a variable that is supposed to be a 24 hour time variable, but it comes in as 1445 or 936 or 40. Ultimately, I'll need a new column that is AM or PM and I need another column to read only the hour in a 12 hour clock (i.e. I need 1445 to change to 2, 936 to become 9, and 40 to become 12). I have been able to make the AM and PM column, but unable to get the time to convert to a time format, let alone down to only the hour from a 12 hour clock. This is what I have currently
Thanks!
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
You seem to be saying that you have simple integers instead of actual time of day values.
So you want to interpret a number like 1,234 as being 12:34 PM .
If so then convert it to a time value first.
Let's make a dataset with your example values and convert into actual TIME values. Then we can work on making your AMPM and HOUR variables.
data have;
input badtime;
cards;
9999
1445
936
40
;
data want;
set have;
if badtime=9999 then time=.;
else time=hms(int(badtime/100),mod(badtime,100),0);
format time tod5.;
if time > '12:00't then ampm='PM';
else if time >= 0 then ampm='AM';
else ampm=' ';
if not missing(time) then hour = hour(time) - 12*(ampm='PM');
run;
Result
But it might be better to print BADTIME using the COMMA format so it is more obvious those as just integer and not really time values at all.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I very strongly suspect that how ever you brought that value into SAS you did not actually create a time value.
For one thing if this appears to work properly, i.e. 1201 has AMPM as "PM" then TIME is not a time value. You have compared to a numeric 1200 which for a time value would be a number of seconds or only 20 minutes and represent a clock time of 00:20:01 on a 24 hour clock. So PUT(TIME,TIME8.) is creating the wrong value.
if Time < 1200 then AMPM='AM';
else AMPM='PM';
IF 1200 is supposed to be 12 noon then with your current values the HMS function to get the hours and minutes for a real time value is likely easiest.
data example; time=1257; timevalue= hms(int(time/100),mod(time,100),0); format timevalue time8.; run;
Once you have the correct time value then you have a number of options such as create custom format that shows only the hour with AM or PM added.
Such as:
proc format library=work; picture hour_ampm low-high='%I %p' (datatype=time) ; run; proc print data=example; format timevalue hour_ampm.; run;
The directives in the PICTURE statement in Proc format %I means show hour on a 12 hour clock, the %p shows AM or PM for the time. These must be in single quotes. If you don't want a space between the number and the AM/PM then remove it from the directives.
That let's you use all the expertise of the folks at SAS to avoid nasty logic.
Or with a valid time value use something like
if Hour(timevalue)> 12 then do;
myhour=hour(timevalue)-12;
ampm='PM';
run; else do;
myhour=hour(timevalue);
ampm='AM'
end;
I am afraid this makes no sense to me at all:(i.e. I need 1445 to change to 2, 936 to become 9, and 40 to become
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
You seem to be saying that you have simple integers instead of actual time of day values.
So you want to interpret a number like 1,234 as being 12:34 PM .
If so then convert it to a time value first.
Let's make a dataset with your example values and convert into actual TIME values. Then we can work on making your AMPM and HOUR variables.
data have;
input badtime;
cards;
9999
1445
936
40
;
data want;
set have;
if badtime=9999 then time=.;
else time=hms(int(badtime/100),mod(badtime,100),0);
format time tod5.;
if time > '12:00't then ampm='PM';
else if time >= 0 then ampm='AM';
else ampm=' ';
if not missing(time) then hour = hour(time) - 12*(ampm='PM');
run;
Result
But it might be better to print BADTIME using the COMMA format so it is more obvious those as just integer and not really time values at all.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content