Help using Base SAS procedures

Converting time to character

Accepted Solution Solved
Reply
Regular Contributor
Posts: 240
Accepted Solution

Converting time to character

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


Accepted Solutions
Solution
‎11-15-2013 10:14 PM
Respected Advisor
Posts: 4,925

Re: Converting time to character

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)Smiley SadColumn).

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


All Replies
Respected Advisor
Posts: 4,925

Re: Converting time to character

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
Regular Contributor
Posts: 240

Re: Converting time to character

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;

Solution
‎11-15-2013 10:14 PM
Respected Advisor
Posts: 4,925

Re: Converting time to character

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)Smiley SadColumn).

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
Occasional Contributor
Posts: 7

Re: Converting time to character

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


🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

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