Help using Base SAS procedures

Determining the AM/PM based on time

Accepted Solution Solved
Reply
Regular Contributor
Posts: 240
Accepted Solution

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

thAnks for your assistance


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

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.

View solution in original post


All Replies
Super User
Posts: 19,770

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: 11,343

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: 11,343

Re: Determining the AM/PM based on time

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

Trusted Advisor
Posts: 1,228

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
Super User
Posts: 7,039

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.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 1014 views
  • 3 likes
  • 5 in conversation