## Determining the AM/PM based on time

Solved
Regular Contributor
Posts: 240

# Determining the AM/PM based on time

HI,

i have a report where I need to determine the time if its AM or PM .For example  I have

time.  ID

13:54 id1

10:33  Id2

i need to look at the time an determine if its AM  like id2 is or PM like id1 is... The time is at military time ... In addition the number is coming in as a character

Accepted Solutions
Solution
‎08-21-2014 05:54 PM
Super User
Posts: 13,583

## Re: Determining the AM/PM based on time

You don't mention the data source. It may be possible to modify the approach so that it is read as a SAS time value.

Currently as character:

if input(substr(time,1,2),f2) > 12 then AMPM='PM';

else AMPM='AM';

Assuming as military time you are getting 01:25 and not 1:25 for hours before 10.

All Replies
Super User
Posts: 23,774

## Re: Determining the AM/PM based on time

If it's a time formatted variable, you can switch the format to an appropriate one that displays AM PM.

Or you can look at the hour. If the hour is >=12 then its PM, otherwise AM

if hour(time)>=12 then test="PM"; else "AM";

Solution
‎08-21-2014 05:54 PM
Super User
Posts: 13,583

## Re: Determining the AM/PM based on time

You don't mention the data source. It may be possible to modify the approach so that it is read as a SAS time value.

Currently as character:

if input(substr(time,1,2),f2) > 12 then AMPM='PM';

else AMPM='AM';

Assuming as military time you are getting 01:25 and not 1:25 for hours before 10.

Regular Contributor
Posts: 240

## Re: Determining the AM/PM based on time

hi ballardw,

i put in the code and I'm getting a error on f2. Something about formatted error thanks again

Super User
Posts: 13,583

## Re: Determining the AM/PM based on time

f2 should be f2. to reference the informat. Sorry about the typo.

Posts: 1,270

## Re: Determining the AM/PM based on time

data have;

input time \$  ID \$;

time_AM_PM=input(time,anydtdtm.);

format time_AM_PM TIMEAMPM.;

datalines;

13:54 id1

10:33 Id2

;

Super User
Posts: 8,125

## Re: Determining the AM/PM based on time

If you have a TIME variable then you can compare the time to a time literal.

IF TIME < '12:00't then AMPM='AM';

ELSE AMPM='PM';

You can convert the string to a time variable by using a INFORMAT;

time = informat(timestr,time5.);

🔒 This topic is solved and locked.