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

How to create hour,  minute, and second variables from datetimes formatted like 14may1998 05:43:32

1 ACCEPTED SOLUTION

Accepted Solutions
jimbarbour
Meteorite | Level 14

Something like this I think:


Hour = SUBSTR(result_dts, 11, 2);
Minute = SUBSTR(result_dts, 14, 2);
Second = SUBSTR(result_dts, 17, 2);

Jim

 

Results:

jimbarbour_0-1602460774399.png

 

P.S.  The above code will give you character values.  If you need to perform any type of numeric operations, you'll need to turn them into numeric variables by wrapping them in an INPUT() function.  Like this:

	Hour	=	INPUT(SUBSTR(result_dts, 11, 2), 2.);
	Minute	=	INPUT(SUBSTR(result_dts, 14, 2), 2.);
	Second	=	INPUT(SUBSTR(result_dts, 17, 2), 2.);

View solution in original post

3 REPLIES 3
jimbarbour
Meteorite | Level 14

Something like this I think:


Hour = SUBSTR(result_dts, 11, 2);
Minute = SUBSTR(result_dts, 14, 2);
Second = SUBSTR(result_dts, 17, 2);

Jim

 

Results:

jimbarbour_0-1602460774399.png

 

P.S.  The above code will give you character values.  If you need to perform any type of numeric operations, you'll need to turn them into numeric variables by wrapping them in an INPUT() function.  Like this:

	Hour	=	INPUT(SUBSTR(result_dts, 11, 2), 2.);
	Minute	=	INPUT(SUBSTR(result_dts, 14, 2), 2.);
	Second	=	INPUT(SUBSTR(result_dts, 17, 2), 2.);
PaigeMiller
Diamond | Level 26

 If result_dts is really a datetime value, then

 

hour=hour(timepart(result_dts));
minute=minute(timepart(result_dts));

Seconds follows the same pattern, so I leave it up to you as a homework assignment.

--
Paige Miller
jimbarbour
Meteorite | Level 14

@PaigeMiller,

 

I was wondering that myself, whether it was formatted text or a formatted SAS date. I guessed that it was text because of the lack of a colon between the date and the time.  I suppose that I guessed right, but your info is useful info either way since working with SAS dates is such a common need (as you well know I'm sure).

 

Jim

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!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 497 views
  • 1 like
  • 3 in conversation