BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Gil_
Quartz | Level 8
I have a table
Pro sql;
Create table settle4 as
Id,
Datetime,
Substr(put(datetime,16.),12,21) as timestamp
From table1);
Run;

The output is
15544
15595
The data looks
13oct18:02:05:44
13oct18:02:06:35


I would want to extract the hour only
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@Gil_ wrote:
I have a table
Pro sql;
Create table settle4 as
Id,
Datetime,
Substr(put(datetime,16.),12,21) as timestamp
From table1);
Run;

The output is
15544
15595
The data looks
13oct18:02:05:44
13oct18:02:06:35


I would want to extract the hour only

 This line of code

Substr(put(datetime,16.),12,21) as timestamp

Should generate a message like:

 

NOTE: Invalid argument 3 to function SUBSTR. Missing values may be generated.

as you are asking for more characters, 21, than would exist (put(datetime,16.) would have a maximum of 16 characters).

 

Which should indicate that you are doing something incorrectly. Even using a DATETIME16. format you wouldn't want to start at position 12.

View solution in original post

3 REPLIES 3
novinosrin
Tourmaline | Level 20

have you tried hour function i,e

 

hour(datetime)

Kurt_Bremser
Super User

A datetime value is a count of seconds. Using a simple numeric format will display the raw number of seconds as a string, and the substr() then shows the values you posted. SAS provides functions to extract the date- and timepart, and functions to extract further sub-divisions (eg minutes or days) from those.

You find these functions in the documentation for data step programming on documentation.sas.com.

ballardw
Super User

@Gil_ wrote:
I have a table
Pro sql;
Create table settle4 as
Id,
Datetime,
Substr(put(datetime,16.),12,21) as timestamp
From table1);
Run;

The output is
15544
15595
The data looks
13oct18:02:05:44
13oct18:02:06:35


I would want to extract the hour only

 This line of code

Substr(put(datetime,16.),12,21) as timestamp

Should generate a message like:

 

NOTE: Invalid argument 3 to function SUBSTR. Missing values may be generated.

as you are asking for more characters, 21, than would exist (put(datetime,16.) would have a maximum of 16 characters).

 

Which should indicate that you are doing something incorrectly. Even using a DATETIME16. format you wouldn't want to start at position 12.

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
  • 1157 views
  • 0 likes
  • 4 in conversation