BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
lydiawawa
Lapis Lazuli | Level 10

Hi All,

 

I have a time variable that is in the format of timeampm11.

ex:12:48:06 AM

 

How do I extract the "AM" from the time value? I have tried the following code none of them worked:

 

 if index(timestamp, 'AM') then td = "AM"; 
 if scan(timestamp,3," ") eq "AM" then td = "AM";

Thank you!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
if index(vvalue(timestamp), 'AM') then td = "AM";

View solution in original post

3 REPLIES 3
Reeza
Super User
If you grab the hour using the hour function and check if that's less than 12 or greater than 12 then it will be pretty easy?

if hour(timestamp) <12 then td='AM';
else if hour(timestamp) >= 12 then td='PM';

ballardw
Super User

Or an approach with a custom format:

proc format library=work;
picture myampm
low - high ='%p' (datatype=time)
;
run;

data exammple;
 input x :time5.;
 td = put(x,myampm.);
datalines;
00:01
10:15
14:25
23:59
;
run;

Or just use the format as needed with your existing variable.

Ksharp
Super User
if index(vvalue(timestamp), 'AM') then td = "AM";

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 3 replies
  • 2202 views
  • 5 likes
  • 4 in conversation