@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.
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!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: