BookmarkSubscribeRSS Feed
HiddenMuggle
New User | Level 1

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

 

data want;
set Have;
if Time < 1200
then AMPM='AM';
else AMPM='PM';
If Time = 9999       *this indicates no value available*
Then AMPM = ' ';
IF DINJHRMNG = 9999
then DINJHRMNG = .;
Run;
 
data want2;
set want;
format Time HHMM.;
Time2=put(input(Time, time8.),tod8.);
run;

 

Thanks!

 

2 REPLIES 2
ballardw
Super User

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 

Tom
Super User Tom
Super User

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

Tom_0-1721171445290.png

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.

Tom_1-1721171509053.png

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 40 views
  • 0 likes
  • 3 in conversation