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

HI Im having and issue with a time column.

the column is a time9. 14:04:00

WHat  i need to pull is the hour segment 14

when I did is a data step

data report;

set report;

hour= substr( left( time) ,2,4);

it gives the sas date of 50640. This is what it pulls in 0640  But the col I'm refer looks like this 14:04:00

thanks for your assistance

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

It must be that variable hour is already declared as a character variable prior to the hour=hour(time) statement. Look at SAS log, there must be a statement such as:

NOTE: Numeric values have been converted to character

      values at the places given by: (Line):(Column).

Try the following test:

data report;

length hourText $8;   /* Declare hourText as a character variable */

time = '14:30:00't;   /* time is a true SAS time value */

hourText = hour(time);  /* Implies a conversion to character */

hourNumber = hour(time);  /* Number result is assigned to a numeric variable (by default) */

run;

proc print; run;

If you want to get a number, assign the result of hour(time) to a variable that hasn't been declared as a character variable.

PG

PG

View solution in original post

4 REPLIES 4
PGStats
Opal | Level 21

If your time variable is a true SAS time value, you should use hour = hour(time) to get the hour number or hour=put(hour(time),best.) to get the character representation.

If time is a character representation of time then you should use hour = scan(time,1,":") . - PG

PG
BETO
Fluorite | Level 6

HI PGStats,

the hour did work it gave  me the hour. But it converted it to a character for example

14:04:00 it gave me a 14  as a character what can i do within the datastep to make it a number? THANKS

data report;

set report;

hour=hour(time):

run;

PGStats
Opal | Level 21

It must be that variable hour is already declared as a character variable prior to the hour=hour(time) statement. Look at SAS log, there must be a statement such as:

NOTE: Numeric values have been converted to character

      values at the places given by: (Line):(Column).

Try the following test:

data report;

length hourText $8;   /* Declare hourText as a character variable */

time = '14:30:00't;   /* time is a true SAS time value */

hourText = hour(time);  /* Implies a conversion to character */

hourNumber = hour(time);  /* Number result is assigned to a numeric variable (by default) */

run;

proc print; run;

If you want to get a number, assign the result of hour(time) to a variable that hasn't been declared as a character variable.

PG

PG

hi

if for some reason the variable is in a character format you can allways create a new variable and converet it  to a numeric format using input .

for example :

data report;

set report;

hour=hour(time);

hour1=input(hour,12.);

run;

good luck


hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 10246 views
  • 1 like
  • 3 in conversation