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

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
  • 3 replies
  • 1379 views
  • 5 likes
  • 4 in conversation