BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
BETO
Fluorite | Level 6

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

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

6 REPLIES 6
Reeza
Super User

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";

ballardw
Super User

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.

BETO
Fluorite | Level 6

hi ballardw,

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

ballardw
Super User

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

stat_sas
Ammonite | Level 13

data have;

input time $  ID $;

time_AM_PM=input(time,anydtdtm.);

format time_AM_PM TIMEAMPM.;

datalines;

13:54 id1

10:33 Id2

;

Tom
Super User Tom
Super User

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.);

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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