@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.
have you tried hour function i,e
hour(datetime)
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.
@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.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
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!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.